問題描述
我的 discord.js 機器人版本是 13.1.0,我的節點版本是 16.7.0.我在終端中輸入了這些命令:npm init
創建 package.json 和 npm install discord.js
安裝 discord 包.
my discord.js bot has version 13.1.0, and my node's version is 16.7.0. I entered these commands in the terminal : npm init
to create package.json and npm install discord.js
to install discord package.
我在 index.js 中編寫了代碼,并創建了 config.json 將令牌放在那里.
I writed the code in index.js, and I created config.json to put the token there.
當我運行代碼時,它會顯示準備就緒!"在控制臺中并在 Discord 中在線.我還可以毫無問題地更改機器人的狀態和活動.這里的問題是機器人不發送或回復消息.
When I run then the code it shows me 'Ready!' in the console and being online in Discord. I also can change the status and the activity of the bot without a problem. The problem here is the bot doesn't send or reply to a message.
下面是代碼
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once('ready', () => {
console.log('Ready!');
});
client.on ('messageCreate', (message) => {
if (message.content === 'hello') {
message.reply('Hello')
};
});
client.login(token);
這是 config.json 的代碼,以防萬一.
this's config.json's code just in case.
{
"token": "my_bot_token!"
}
推薦答案
問題是由于缺少意圖引起的.要收聽消息,您需要指定 GUILD_MESSAGES
標志:
The issue is caused by missing intents. To listen to messages, you need to specify the GUILD_MESSAGES
flag:
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
這篇關于即使沒有任何錯誤,我的 discord.js 機器人也不會回復用戶消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!