Discord Bot Can#39;t Get Channel by Name(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:
打开您的 Discord 设置
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 无法按名称获取频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord Bot 无法按名称获取频道
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06