簡體   English   中英

DiscordBot 在線但不響應命令 (js)

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

我沒有使用 dc bot 的經驗,所以我遵循了一個 yt 教程,但他使用的 discord.js 是舊版本(v12),所以我修復了我的版本 v14 的代碼。 Bot 上線,但是當我輸入命令時它不會響應。 沒有錯誤,它只是上線並給出控制台消息。 也許是因為版本,但我找不到這個版本的任何答案。 (也許這是我之前從未做過的事情)這是我的 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');

console.log() 1 和 2 用於檢查它是否有效,但它不會在控制台中僅顯示“機器人在線”。 然后我有一個帶有 ping.js 的文件夾 /commands:

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

您應該請求消息內容意圖。 怎么看:

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

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

請注意,您可能還需要在 Discord 開發者門戶中啟用它: 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM