英文:
my bot is working properly but still not responding to my commands
问题
我的代码:
const Discord = require('discord.js');
const { Client, GatewayIntentBits } = require('discord.js');
const prefix = '$';
const client = new Discord.Client({
'intents': [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
]
});
client.once('ready', () => {
console.log('- - - - -');
console.log('机器人在线!');
console.log('- - - - -');
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.channel.send('pong');
}
});
client.login('TOKEN');
英文:
my code
const Discord = require('discord.js');
const { Client, GatewayIntentBits } = require('discord.js');
const prefix = '$';
const client = new Discord.Client({
'intents': [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
]
});
client.once('ready', () => {
console.log('- - - - -');
console.log('bot is online!');
console.log('- - - - -');
});
client.on('message', message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.split(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong');
}
});
client.login('TOKEN');
before i even got my bot online i had a problem with gatewayintentbits and after hours of figuring it out and running my bot it finally got online but when i added my first command it doesnt respond even after chaning prefix and adding multiple gateways i need help
答案1
得分: 0
Slash命令通常比文本命令更受推荐。事件应为messageCreate
,而不是message
。您可能没有在Discord开发者门户中将消息内容意图设置为true。我强烈建议您遵循官方Discord.js指南。
如果您在消息内容意图中设置了此代码应该可以工作。
const { Client, GatewayIntentBits } = require('discord.js');
const prefix = '$';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
]
});
client.once('ready', () => {
console.log('- - - - -');
console.log('机器人在线了!');
console.log('- - - - -');
});
client.on('messageCreate', message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping'){
message.channel.send('pong');
}
});
client.login('TOKEN');
英文:
Slash commands are generally recommended instead of text commands nowadays. The event is messageCreate
, not message
. You probably don't have the message content intent set to true in your Discord Developer Portal. I strongly recommend you follow the official Discord.js guide.
This code should work if you have the message content intent.
const { Client, GatewayIntentBits } = require('discord.js');
const prefix = '$';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages
]
});
client.once('ready', () => {
console.log('- - - - -');
console.log('bot is online!');
console.log('- - - - -');
});
client.on('messageCreate', message => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.split(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping'){
message.channel.send('pong');
}
});
client.login('TOKEN');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论