我的机器人运行正常,但仍然不响应我的命令。

huangapple go评论72阅读模式
英文:

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');

huangapple
  • 本文由 发表于 2023年4月20日 07:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定