javascript - Discord JS命令未处理
问题描述
我最近一直在 Discord.JS 中开发一个审核机器人,并且我已经完成了许多命令,例如 Purge、Mute 等。当我在处理“ban”命令时,我注意到我没有得到任何回应机器人。我开始调试,甚至在命令上重新启动了几次开发,然后,我想将命令更改为基本命令,以检查机器人是否接收到命令。所以命令现在刚刚有了message.channel.send('hello world')
,我运行了命令,仍然没有输出,就在我不使用参数的时候,一条Please provide arguments to execute this command
消息。然后我尝试删除命令文件并重新启动机器人,然后运行命令,输出相同。我将在下面包含我的 bot.js:
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);
}
const cooldowns = new Discord.Collection();
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 commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.permissions) {
const authorPerms = message.channel.permissionsFor(message.author);
if (!authorPerms || !authorPerms.has(command.permissions)) {
return message.reply('You can not do this!');
}
}
if (command.args && !args.length) {
let reply = `\`Please provide arguments to execute this command\``;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.login(token);
解决方案
如果您尝试在短时间内多次登录机器人,这可能是不和谐速率限制的问题。如果问题仅出在该禁令命令上,则可能是禁令命令本身的问题,也可能是您的命令处理程序的问题。如果所有命令都存在问题,则ready
可能不会发出事件,这将表明我上面提到的内容。尝试使用不同的机器人令牌或重新生成当前的令牌并再次运行程序。
推荐阅读
- mongodb - 如何删除 MongoDB 中的重复项?
- ios - Apple 应用内沙箱收据验证有时会返回空 JSON 响应
- android - Osmdroid 尝试在 Fragment.onStart() 期间重绘已删除的多边形
- java - 如何在 Spring-Boot 项目中为电话号码身份验证创建 REST API?
- amazon-s3 - 如何从 aws javascript sdk 中的 selectObjectContent 获取可读流?
- javascript - antD 模型 API - 背景颜色变化
- python - 如何使用 python 错误在 Google colab 中读取更大的镶木地板文件:ValueError("engine must be one of 'pyarrow', 'fastparquet'")
- javascript - 使用 Angular 通用的 ngx-translate 在转换参数化值时遇到问题
- python - numpy 数组到 Pandas 表
- jmeter - 调试采样器不显示正则表达式提取器变量