我一直在制作一个不和谐机器人,并希望让它向特定的“欢迎”频道发送消息。不幸的是,我无法这样做。我试过这个。
const welcomeChannel = bot.channels.get("name", "welcome")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);
然而,在此“welcomeChannel未定义”。
编辑:
我尝试使用
const welcomeChannel = bot.channels.get("id", "18NUMBERIDHERE")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);
但这仍然是未定义的,奇怪的是
答案 0 :(得分:17)
你应该使用channnel id而不是它的名字。
如何获取频道的频道ID:
打开您的不和谐设置
转到std::vector<SOCKET>
勾选Appearance
(并关闭Discord设置)
右键点击您想要的频道
现在有一个选项Developer Mode
来复制频道ID
同时查看discord.js documentation(频道)馆藏
此外,您的方法无法正常工作,因为Copy ID
需要频道ID(请参阅上面的链接文档)。如果您真的希望按名称获取频道,请使用.get
代替。
但是,如果您的机器人在多个服务器上运行,这是一个非常糟糕的主意,因为通道名称现在可以多次出现。
答案 1 :(得分:6)
您也可以使用
bot.channels.find("name","welcome").send("Welcome!")
答案 2 :(得分:1)
我尝试了很多相同的错误,这就是我修复它的方法。我使用client作为我的Client()。
client.channels.cache.get("18NUMBERIDHERE").send("Welcome!");
答案 3 :(得分:0)
您的错误可能来自您正在使用bot.channels.get()
的事实,这不是最好的主意,因为在多个地方使用.send()
时discord.js不太友好项目。
如果可能,请尝试使用member.guild.channels.find("name", "channel").send();
。如果在client.on("message")
中,则只需使用message.member.channels.find("name", "channel").send();
旁注:我的记忆混乱,因此,如果不起作用,请尝试使用.get()
而非find
。