【问题标题】:Return the channel ID of a channel mentioned in a command返回命令中提到的频道的频道 ID
【发布时间】:2019-02-13 07:48:50
【问题描述】:

标题说明了一切。我正在 node.js 中制作一个 Discord 机器人,我试图添加的一部分是 .setup 命令,以使机器人减少对手动更改 client.channels.get().send() 的值的依赖,使其更多将来很容易设置。

无论如何,就我而言,我试图让用户回复一条带有频道提及的消息(例如#welcome),并让机器人返回该 ID 并将其保存到变量中。

目前,我有这个:

function setupChannel() {
  client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
  client.on('message', message => {
    checkChannel = message.mentions.channels.first().id;
    client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to that channel.');
  });
}

message.mentions.channels 返回未定义。
我知道message.mentions.channels.first().id 位在它是用户而不是频道时起作用,是否有不同的方式在消息中获得频道提及?

【问题讨论】:

  • 通常message.mentions.channels.first().id 应该可以工作
  • 首先,我会检查是否有提及频道,如果是则发送消息,如果没有则什么也不做或任何您想要的
  • @GillesHeinesch 但是,如果您希望在消息中提及多个频道,这不是一个好主意,因为它们不是按提及顺序而是按频道年龄排序的。

标签: javascript discord.js


【解决方案1】:

这是我发现的最简单易懂的方法:

message.guild.channels.cache.get(args[0].replace('<#','').replace('>',''))

但这并不是我们真正想要的,我们可以使用&lt;#####9874829874##&gt;&lt;&lt;&lt;547372&gt;&gt;,它会起作用。所以我想通了:

message.guild.channels.cache.get(args[0].substring(2).substring(0,18))

【讨论】:

    【解决方案2】:

    我喜欢从消息中删除所有非整数,因为这样可以轻松使用 ID 和提及项,它会从中返回 ID,因为提及项只是带有 的 ID在他们旁边。为此,您将使用

    message.content.replace(/\D/g,'')
    

    这样,您的代码将如下所示:

    function setupChannel() {
      client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
      client.on('message', message => {
        checkChannel = message.content.replace(/\D/g,'');
        client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to <#' + checkChannel + '>.');
      });
    }
    

    从此您将始终获得一个 ID。我们可以围绕 ID 将其转化为提及。

    【讨论】:

      猜你喜欢
      • 2022-07-12
      • 1970-01-01
      • 2018-12-12
      • 2019-02-19
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      相关资源
      最近更新 更多