問題描述
我一直在制作一個不和諧的機器人,并想讓它向特定的歡迎"頻道發送消息.不幸的是,我一直無法這樣做.我試過了.
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 無法按名稱獲取頻道的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!