Discord.js V13有一些更改的事件类型。在这种情况下,用于接收消息的事件已从
我正试图让我的第一个Discord机器人工作,并在检测到消息时登录控制台,但什么也没发生。这是我的密码,怎么了?
const Discord = require("discord.js");
const client = new Discord.Client({ intents: [ 'DIRECT_MESSAGES', 'GUILD_MESSAGES' ] });
const token = '<token here>';
const PREFIX = '!';
client.on('ready', () =>{
console.log('Bot Online! Woohoo!');
});
client.on('message', message =>{
console.log('Message registered!');
});
client.login(token);
message
变为messageCreate
。
请在此处查看升级更改。
!
请注意,使用message
事件将抛出一条不推荐使用的错误消息,该消息将在将来删除。
更新:如果你想让你的机器人在直接消息上工作,你也需要添加partials
:
partials: ["MESSAGE", "CHANNEL"]
如果您希望同时接收服务器和直接消息,请添加intentGUILDS
。总之:
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
/*
Intents 'GUILDS' is required
if you wish to receive (message) events
from guilds as well.
If you don't want that, do not add it.
Your bot will only receive events
from Direct Messages only.
*/
'GUILDS',
'DIRECT_MESSAGES',
'GUILD_MESSAGES'
],
partials: ['MESSAGE', 'CHANNEL'] // Needed to get messages from DM's as well
});
const token = 'YourBotToken';
const PREFIX = '!';
client.on('ready', () =>{
console.log('Bot Online! Woohoo!');
});
client.on('messageCreate', message =>{
console.log('Message registered!');
});
client.login(token);
附言:在开发者面板上重新生成你的机器人代币。
如果你刚开始使用discord.js库,我强烈建议你查看discord.jss文档。我也建议不要在这里显示你的机器人令牌,因为人们可以随心所欲地使用它,你想要的可能是像这个一样的东西
const Discord = require("discord.js");
const client = new Discord.Client({ intents:['DIRECT_MESSAGES', 'GUILD_MESSAGES' ],
partials: ['MESSAGE', 'CHANNEL'] });
const token = 'Your_token_Here';
const PREFIX = '!';
client.on('ready', () =>{
console.log('Bot Online!'); //bot successfully logged in
});
client.on('message', message => {
console.log(message.content); //log messages
});
client.login(token);