【问题标题】:Bot not responding to commands (discord.js v12)机器人不响应命令(discord.js v12)
【发布时间】:2021-11-21 19:06:19
【问题描述】:

我尝试使用client.on 执行测试命令,该命令有效,但我的命令处理程序中的其他所有内容均无效。什么都没有返回,是不是我做错了什么?

问题:我的 ping 命令不执行任何操作。

Index.js 文件

require('dotenv').config();
const Discord = require('discord.js');
const { Client, Collection, Intents } = require('discord.js');
const config = require('./config.json');
const fs = require("fs");
const client = new Client({ disableMentions: 'everyone', partials: ['MESSAGE', 'CHANNEL', 'REACTION'], ws: { intents: Intents.ALL } });
const PREFIX = config.PREFIX;
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.commands = new Collection();



Ping 文件

let config = require('../config.json');
const Discord = require('discord.js');
const { MessageEmbed } = require('discord.js');

module.exports = {
    name: 'ping',
    category: 'Info',
    description: 'Returns the bot\'s latency and API ping.',
    aliases: ['latency'],
    usage: 'ping',
    userperms: [],
    botperms: [],
    run: async (client, message, args) => {
        message.channel.send('???? Pinging....').then((msg) => {
            const pEmbed = new MessageEmbed()
                .setTitle('???? Pong!')
                .setColor('BLUE')
                .setDescription(
                    `Latency: ${Math.floor(
                        msg.createdTimestamp - message.createdTimestamp,
                    )}ms\nAPI Latency: ${client.ws.ping}ms`,
                );
            msg.edit(pEmbed);
        });
    },
};

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    您需要添加事件,该事件将在每次创建消息时触发并且机器人可以看到它。

    版本 12 的示例是(使用 message :

    client.on('message', message => {
      if (message.content.startsWith("!ping")) {
        message.channel.send('Pong!');
      }
    });
    

    在 v13 中,message 已弃用,因此请使用 messageCreate:

    client.on('messageCreate', message => {
      if (message.content.startsWith("!ping")) {
        message.channel.send('Pong!');
      }
    });
    

    【讨论】:

    • 这不是问题,我事先做了这个,效果很好。我的问题是机器人没有为module.exports 返回任何内容
    • @ignshifts 你的意思是不能访问命令文件上的模块?-?
    • 是的,它什么也没做
    • ping 文件,当我运行命令时没有任何反应。我不是在谈论在事件或消息事件上创建命令;就像我之前说的那样,我事先就这样做了,并且奏效了。
    猜你喜欢
    • 2021-11-10
    • 2021-08-30
    • 2021-09-04
    • 2021-07-23
    • 2020-10-11
    • 2020-08-21
    • 2021-01-01
    • 2021-02-18
    • 1970-01-01
    相关资源
    最近更新 更多