Discord Bot 无法按名称获取频道

2022-01-10 00:00:00 discord bots javascript discord.js

我一直在制作一个不和谐的机器人,并想让它向特定的欢迎"频道发送消息.不幸的是,我一直无法这样做.我试过了.

I have been making a discord bot and wanted to make it send a message to a specific "Welcome" channel. Unfortunately, I have been unable to do so. I tried this.

const welcomeChannel = bot.channels.get("name", "welcome")
welcomeChannel.sendMessage("Welcome
"+member.user.username);

但是在这个welcomeChannel 未定义"中.

However in this "welcomeChannel is undefined".

我尝试过使用

const welcomeChannel = bot.channels.get("id", "18NUMBERIDHERE")
welcomeChannel.sendMessage("Welcome
"+member.user.username);

但这仍然是未定义的,奇怪的是

but this is still undefined, strangely

推荐答案

您应该使用频道 id 而不是频道名称.

You should use the channnel id instead of it's name.

如何获取频道的频道ID:

How to get the channel id of a channel:

  1. 打开您的 Discord 设置

  1. Open up your Discord Settings

转到外观

勾选开发者模式(并关闭Discord设置)

Tick Developer Mode (And close the Discord settings)

右键点击你想要的频道

现在有一个选项Copy ID来复制频道ID

Now there's an option Copy ID to copy the channel id

还可以查看 discord.js 文档 用于(频道)收藏

Also checkout the discord.js documentation for (channel) collections

此外,您的方法将不起作用,因为 .get 需要频道 ID(请参阅上面的链接文档).如果您真的想通过名称获取频道,请改用 .find.
但是,如果您的机器人在多个服务器上运行,这是一个非常糟糕的主意,因为频道名称现在可以出现多次.

Furthermore your approach won't work because .get wants a channel id (see the linked documentation above). In case you REALLY want to get an channel by its name, use .find instead for that.
This is however a really bad idea in case your bot runs on more than one server since channel names can now occur multiple times.

相关文章