【问题标题】:Messaging with discord.js使用 discord.js 进行消息传递
【发布时间】:2020-01-23 13:52:54
【问题描述】:

我正在尝试弄清楚如何在 #channel1 中键入命令并让 discord bot#channel2 中发送消息(并计划在未来也明确)

这是我的代码:

const Discord = require('discord.js');
Bot = new Discord.Client();
gbot = new Discord.Guild();
const token = '....';
bot.on('message', msg =>{
    if(msg.content === 'Hello'){
        var channel2 = 
        gbot.channels.find(val => 
        val.name === "channel2")             
        msg.channel2.send("hey");
    }
}
bot.login(token); 

这是我得到的错误:

无法读取未定义的属性“发送” 在客户端。

【问题讨论】:

  • 恐怕 rafael duarte 的回答没有帮助。撞
  • 您正在尝试在新开设的课程中查找频道...?

标签: javascript discord discord.js


【解决方案1】:

我认为你在msg.channel2.send("hey") 有一个错误,它应该是channel2.send("hey"),另外,正如 Kareem 所提到的,你不应该初始化一个新的 Guild 对象,你应该使用消息中的那个。

const Discord = require('discord.js');
bot = new Discord.Client();
const token = '....';
bot.on('message', msg =>{
    if(msg.content === 'Hello'){           
        var channel2 =  
        msg.guild.channels.find(val => 
        val.name === "channel2");          
        channel2.send("hey");
    }
}
bot.login(token); 

【讨论】:

  • 我已经更新了我的答案,你不应该初始化一个新的 Guild 对象,你需要使用与消息关联的那个
【解决方案2】:

就像 Rafeal 说你想使用 channel2.send() , 此外,您获取频道的方式有点奇怪。似乎您正在创建一个新的公会,而不是获取发送消息的那个公会。你可以在消息对象中找到它。

试试这个代码。

const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '....';
bot.on('message', msg =>{
    if(msg.content === 'Hello'){
        var channel2 = 
        msg.guild.channels.find(val => 
        val.name === "channel2")             
        channel2.send("hey");
    }
}
bot.login(token); 

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 2014-11-27
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多