【问题标题】:await messages, get a channel by the given ID等待消息,通过给定 ID 获取频道
【发布时间】:2020-09-10 04:42:08
【问题描述】:

所以我希望我的投票命令在对话中询问频道和问题,但我还没有弄清楚当用户只提供 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() 我写错了,但我知道我必须更改什么/在哪里添加 .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!');
        } 
    }
}

【问题讨论】:

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


    【解决方案1】:

    所以我绕过它,制作了另一个构造方法:

    常量 ID = client.channels.cache.get(response1.first().content) 常量通道 = response1.first().mentions.channels.first() ||身份证

    现在可以正常使用了

    【讨论】:

      【解决方案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
      

      【讨论】:

      • 谢谢,但“通道”是未定义的,这就是错误所说的,是的,内容是由 args 处理的,我忘记了
      • response1.first().guild.channels.cache.get() 有人推荐但随后转到 if(!channel)
      • 我已经更新了我的答案(我忘了在response1 之后添加.first()。关于您的问题:请注意它不仅仅是.get(),但您需要传入id (.get(args[0])
      • 我现在拥有它,就像您的评论(与您的编辑)一样,但它仍然不接受 Id(它是一个真正的频道 Id,我确定)并直接进入 if (!频道),真的很烦我......
      • 您应该尝试进行一些调试以确保正确提取参数...尝试使用console.log 来查看response1args[0] 以及您用于@ 的两个语句987654332@ 是正确的。
      猜你喜欢
      • 1970-01-01
      • 2022-07-10
      • 2013-04-02
      • 2018-09-01
      • 2021-09-18
      • 2021-03-17
      • 2020-06-29
      • 1970-01-01
      • 2020-11-26
      相关资源
      最近更新 更多