简体   繁体   English

DiscordBot 在线但不响应命令 (js)

[英]DiscordBot online but doesn't respond to command (js)

I have no experience with dc bots so I followed a yt tutorial but discord.js he used was an older version (v12) so I fixed the code for my version v14.我没有使用 dc bot 的经验,所以我遵循了一个 yt 教程,但他使用的 discord.js 是旧版本(v12),所以我修复了我的版本 v14 的代码。 The Bot goes online but when I type a command it won't respond. Bot 上线,但是当我输入命令时它不会响应。 There are no errors it just goes online and gives the console message that's it.没有错误,它只是上线并给出控制台消息。 Maybe it's because of the version but I can't find any answers for this version.也许是因为版本,但我找不到这个版本的任何答案。 (Maybe it's something else as I said I've never done that before) This is my main.js: (也许这是我之前从未做过的事情)这是我的 main.js:


const { Client, GatewayIntentBits } = require('discord.js');

const client = new Discord.Client({
    intents: [
      GatewayIntentBits.Guilds,
      GatewayIntentBits.GuildMessages,
    ]
  })

  const prefix ='!';

  const fs = require('fs');
  client.commands = new Discord.Collection();

  const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
  for (const file of commandFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
  };

client.once('ready', () => {
    console.log('Bot is online!');
});


client.on('message', message =>{
  console.log("1")
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    console.log("2")

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    }
});

client.login('TOKEN');

The console.log() 1 and 2 are to check if it works but it won't show in the console just the "Bot is online". console.log() 1 和 2 用于检查它是否有效,但它不会在控制台中仅显示“机器人在线”。 Then I have a folder /commands with ping.js:然后我有一个带有 ping.js 的文件夹 /commands:

module.exports = {
    name: 'ping',
    description: "this is a ping command!",
    execute(message, args){
        message.channel.send('pong!')
    }
}

You should request the message content intent.您应该请求消息内容意图。 See how:怎么看:

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Discord.Client({
    intents: [
      GatewayIntentBits.Guilds,
      GatewayIntentBits.GuildMessages,
      GatewayIntentBits.MessageContent
    ]
  })

Note that you may also need to enable it in the Discord Developer Portal:请注意,您可能还需要在 Discord 开发者门户中启用它: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM