正在将消息发送到特定频道,无法正常工作

时间:2019-09-28 06:57:31

标签: node.js discord.js

在特定频道上发送漫游器消息时遇到问题。我只是想让该机器人在#general激活时向其发送一条消息,以使所有人都知道它再次正常工作。

在bot.on函数中,我尝试了经典的client.channels.get()。send(),但收到的错误消息表明它认为客户端未定义。 (它说“无法读取未定义的属性'channels'”)

bot.on('ready', client => {
    console.log("MACsim online")
    client.channels.get('#general').send("@here");
})

机器人立即崩溃,并说:无法读取未定义的属性“通道”

3 个答案:

答案 0 :(得分:1)

您需要按ID获取行会/服务器,然后按ID获取渠道,因此您的代码应类似于:

const discordjs = require("discord.js");
const client = new discordjs.Client();

bot.on('ready', client => {
console.log("MACsim online")
    client.guilds.get("1836402401348").channels.get('199993777289').send("@here");
})

编辑:添加了客户通话

答案 1 :(得分:0)

ready事件未传递参数。要获得机器人的客户端,只需使用创建客户端时分配的变量即可。
在您的代码中,它看起来像bot变量,您在其中进行了const bot = new Discord.Client();
从那里,使用bot(机器人的客户端),可以访问channels属性!

答案 2 :(得分:0)

ready事件未传递任何client参数。 要使用名称获取频道,请使用collection.find()

client.channels.find(channel => channel.name == "channel name here");

要按ID获取频道,您只需执行collection.get("ID"),因为频道是按其ID映射的。

client.channels.get("channel_id");

这是我为您制作的一个示例:

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

Client.on("ready", () => {
   let Channel = Client.channels.find(channel => channel.name == "my_channel");
   Channel.send("The bot is ready!");
});

Client.login("TOKEN");
相关问题