【问题标题】:How to send discord message to another channel? (Discord JS)如何将不和谐消息发送到另一个频道? (不和谐 JS)
【发布时间】:2020-05-07 02:24:27
【问题描述】:

所以流程是,我向#general 频道发送!beep 命令,然后我希望机器人向#announcement-channel 发送回复,所以假设我的频道ID 是668*****8*********。但是当我试图找到频道并将其发送到那里时,它会响应错误:

TypeError: 无法读取未定义的属性“发送”

这是我写的代码

const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
  name: 'beep',
  description: 'Beep!',
  execute(message) {
    const channel = client.channels.get('668*****8*********');
    channel.send('Yahoo');
  }
};

我从另一个问题的多个答案中尝试了几次,但都没有奏效。从字符串更改为整数也不起作用。我也试过console.log(client.channels.get('668*****8*********')),但它什么也没返回。

【问题讨论】:

  • 您导入的客户端尚未初始化,因此频道集合为空。

标签: javascript node.js discord.js


【解决方案1】:

您可以使用以下代码:

你不能在这里使用客户端参数函数,因为不要传递它。也不可能在每个命令文件中创建一个客户端。 1 个令牌 - 1 个客户端

const Discord = require('discord.js');

module.exports = {
  name: 'beep',
  description: 'Beep!',
  execute(message) {
      if(message.channel.type === 'dm') return
    const channel = message.guild.channels.get('668*****8*********');
    if(!channel) return
    channel.send('Yahoo');
  }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2021-01-19
    • 2019-04-11
    • 2021-08-14
    • 2021-05-04
    • 2021-03-13
    • 2020-07-14
    相关资源
    最近更新 更多