Discord.js - Discord机器人不再响应命令

5

我试图给我的机器人添加一些新功能,但最终没有成功。所以我删除了新代码,现在它不会响应任何命令。它仍然在线,但就像我之前说的,完全没有反应。我已经查看了代码,但找不到问题所在。作为一个编写机器人的新手,我可能忽略了某些东西。

以下是我的主要代码文件:

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) {
    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'
        }!`,
      );
    },
  };```

你能给我们提供一个命令文件的例子吗? - MrMythical
返回数组[...]出了什么问题? - MrMythical
它会在控制台中打印“Ready!”吗?你可以在client.on('message', message => {之后添加console.log(message)吗?你应该尝试git,这样当你添加东西时,就可以简单地回滚而不会出现时间或错误问题 :-) - Daniel W.
1
曾听说过 GitHub 吗?它非常棒:github - a1cd
@Evergreen 不一定要是 Github,任何 Git 都可以。 - Daniel W.
显示剩余3条评论
2个回答

0
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 了。


1
也许他们的执行函数没有使用那个。 - MrMythical
我很感激你的帮助,但它仍然没有起作用。 - Sans4days
在你的命令文件中,在execute(message,args)中添加client,这样就可以使你的命令运行。 - Patriot

0

看了你的代码后,我发现你没有导入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);

所以我已经添加了这行代码,但问题仍然存在。 - Sans4days

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接