Discord Bot 无法按名称获取频道

Discord Bot Can#39;t Get Channel by Name(Discord Bot 无法按名称获取频道)
本文介绍了Discord Bot 无法按名称获取频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

这篇关于Discord Bot 无法按名称获取频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Using discord.js to detect image and respond(使用 discord.js 检测图像并响应)
Check if user ID exists in Discord server(检查 Discord 服务器中是否存在用户 ID)
Guild Member Add does not work (discordjs)(公会成员添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何为我的 Discord.js 机器人编写事件/命令处理程序?)
How to find a User ID from a Username in Discord.js?(如何从 Discord.js 中的用户名中查找用户 ID?)