【问题标题】:Discord.js - Discord bot stopped responding to commandsDiscord.js - Discord 机器人停止响应命令
【发布时间】:2021-09-04 04:40:29
【问题描述】:

我试图向我的机器人添加一些新功能,但它最终没有工作,所以我删除了新代码,现在它不会响应任何命令。它仍然在线,但正如我之前所说,它完全没有响应。我查看了代码,但找不到问题。我对编码机器人还是很陌生,所以我可能遗漏了一些东西。这是我的主要文件代码:

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
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('Ready!');
});

client.on('message', message => {
console.log(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 (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login(token);

这是一个命令文件:

    function pickOneFrom(array) {
    return array[Math.floor(Math.random() * array.length)];
  }
  
  module.exports = {
    name: 'throw',
    description: 'this is a throw command!',
    execute(message, args) {
      const responses = ['a apple', 'a melon', 'a potato', 'a snowball', 'a spanner', 'a computer'];
  
      message.channel.send(
        `${message.author} threw ${pickOneFrom(responses)} at ${
          message.mentions.users.first() ?? 'everyone'
        }!`,
      );
    },
  };```

【问题讨论】:

  • 我们可以举一个你的命令文件的例子吗?
  • 我在帖子中添加了一个命令文件,希望对您有所帮助。
  • 返回数组[...]怎么了?
  • 它是否在控制台中打印Ready!?你可以在client.on('message', message => { 之后添加console.log(message) 吗?你应该试试git,所以当你添加一些东西时,你可以简单地回滚,没有时间或错误:-)
  • 听说过 github 吗?太好了:[github](github.com/home)

标签: discord.js


【解决方案1】:
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
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('Ready!');
});

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 (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(client , message , args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login(token);

你忘记在client.commands.get(command).execute(message , args)添加client

【讨论】:

  • 也许他们的执行功能不使用那个
  • 感谢您的帮助,但仍然无法正常工作。
  • 在你的命令文件中添加client execute(message,args) 应该让你的命令运行
【解决方案2】:

查看您的代码后,我发现您没有导入 fs 文件系统来读取要导入的命令文件,因此您应该将其添加到您的代码中

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
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('Ready!');
});

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 (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }
});

client.login(token);

【讨论】:

  • 所以我已经添加了该行,但问题仍然存在。
猜你喜欢
  • 2021-03-11
  • 2021-07-22
  • 2021-11-21
  • 2020-10-30
  • 2021-11-10
  • 2021-08-30
  • 2021-07-30
  • 2022-10-24
相关资源
最近更新 更多