等待消息,通过给定的ID获取频道

时间:2020-05-23 09:56:28

标签: javascript async-await bots discord discord.js

所以我希望我的民意测验命令在对话中询问频道和问题,但是当用户只提供ID时,我还没有弄清楚如何获得频道,我已经弄清楚我必须使用.content,但我仍然不知道如何实现。

我的代码:

run: async(message, client, args) => {

  // Channel where the poll should take palce
  await message.channel.send(`Please provide a channel where the poll should take place or cancel this command with "cancel"!`)
  const response1 = await message.channel.awaitMessages(m => m.author.id === message.author.id, {max: 1});
  const channel = response1.first().mentions.channels.first() || response1.content.guild.channels.cache.get()

  if (!channel) {
    return message.channel.send(`You did not mention or provide the ID of a channel where the poll should take place!`)
  }

  // Channel where the poll should take palce
  await message.channel.send(`Please provide a question for the poll!`)
  const response2 = await message.channel.awaitMessages(m => m.author.id === message.author.id, {max: 1});
  let question = response2.first();

  if (!question) {
    return message.channel.send(`You did not specify your question!`)
  }

  const Embed = new Discord.MessageEmbed()
    .setTitle(`New poll!`)
    .setDescription(`${question}`)
    .setFooter(`${message.author.username} created this poll.`)
    .setColor(`0x0099ff`)
  let msg = await client.channels.cache.get(channel.id).send(Embed)
  await msg.react("?")
  await msg.react("?")
}

以下是这行:response1.content.guild.channels.cache.get()是我写错了,但idk我必须更改的内容/在何处添加.content才能起作用。

如果有人可以帮助我,那会很好。

我的args消息事件:

module.exports = async (client, message) => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    if (!message.guild) return;
    if (!message.member) message.member = await message.guild.fetchMember(message);
    const args = message.content.slice(prefix.length).split(/ +/g);
    const cmd = args.shift().toLowerCase();
    if (cmd.length == 0) return;
    let command = client.commands.get(cmd)
    if (!command) command = client.commands.get(client.aliases.get(cmd));
    if (command) {
        try {
            command.run(message, client, args)
        } catch (error) {
            console.error(error);
            message.reply('There was an error trying to execute that command!');
        } 
    }
}

2 个答案:

答案 0 :(得分:2)

您需要获取ID的内容,但是我认为生成args参数的代码已经对该内容进行了处理。
要获得公会,您可以使用Message.guild,然后使用Guild.channels.cache.get()

这意味着您的代码将如下所示:

const channel = response1.first().mentions.channels.first() 
  || response1.first().guild.channels.cache.get(args[0]) // Assuming args[0] is your id

答案 1 :(得分:0)

所以我通过制作另一个构造函数来解决它:

常量ID = client.channels.cache.get(response1.first()。content) const channel = response1.first()。mentions.channels.first()|| ID

现在可以正常使用