我试图让我的机器人回复消息,但它不起作用。有人知道如何解决这个问题吗?
Code:
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ['GuildMessages', 'GuildMessageReactions', 'GuildMessageTyping', 'MessageContent', ''] })
const prefix = '!';
client.once('ready', () => {
console.log('MONKE BOT RISES');
});
client.on('messageCreate', message => {
message.channel.send('pong');
console.log('hoi');
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong');
}
});
client.login('<token>');
您需要启用MessageContent
网关意图Discord 开发者门户 https://discord.com/developers/applications由于其特权性质。
查看一篇关于此的好文章docs https://discordjs.guide/popular-topics/intents.html#privileged-intents.
我已经测试了你的代码,一切正常。
let message = {
content: "!ping"
};
const prefix = '!';
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
console.log('pong!');
};
因此,此行为只能是由错误分配的意图或客户端实例化期间代码中的错误引起的。
正如我之前所说,请仔细检查MessageContent
已在开发门户中启用。然后尝试像这样实例化 Client
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
如果这不起作用,请检查您使用的是最新版本的 Node 和 Discord.js (npm list discord.js
, node --version
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)