【发布时间】: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