我试图给我的机器人添加一些新功能,但最终没有成功。所以我删除了新代码,现在它不会响应任何命令。它仍然在线,但就像我之前说的,完全没有反应。我已经查看了代码,但找不到问题所在。作为一个编写机器人的新手,我可能忽略了某些东西。
以下是我的主要代码文件:
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'
}!`,
);
},
};```
client.on('message', message => {
之后添加console.log(message)
吗?你应该尝试git
,这样当你添加东西时,就可以简单地回滚而不会出现时间或错误问题 :-) - Daniel W.