我有一个工作正常的bot,具有正常运行的命令处理程序,并且有一个重载命令来更新代码以更新现有命令。每当我添加新命令时,我都必须重新启动整个机器人。由于此特定的bot在间隔运行时具有脚本,因此重新启动bot将终止所有运行间隔,从而迫使所有用户手动重新启动它们。我不想在添加新命令时就不得不重启我的机器人,所以我需要帮助。
这是我现在的重装命令:
const botconfig = require("../config.json");
module.exports = {
name: 'reload',
type: "Developer",
description: 'Reloads a command (developer only)',
cooldown: 1,
execute(message, args) {
if (message.author.id != botconfig.developerid) return message.channel.send("Only my developer can use this command...");
message.channel.send("Developer command confirmed!");
if (!args.length) return message.channel.send(`You didn't pass any command to reload!`);
const commandName = args[0].toLowerCase();
const command = message.client.commands.get(commandName) ||
message.client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return message.channel.send(`There is no command with name or alias \`${commandName}\`, ${message.author}!`);
delete require.cache[require.resolve(`./${command.name}.js`)];
try {
const newCommand = require(`./${command.name}.js`);
message.client.commands.set(newCommand.name, newCommand);
message.channel.send("Command `" + command.name + "` was reloaded!");
} catch (error) {
console.log(error);
message.channel.send("There was an error while reloading the `" + botconfig.prefix + command.name + "` command. \n\nError is as follows:\n``${error.message}`");
}
},
};
由于当前代码有效,我想在命令名称之前添加一个可选的“ new”参数以专门查找新命令,因为当前代码有效,但是它只能看到预先存在的命令。如果更改当前代码以另外查找新命令会更简单,但是如果找不到则仍然出错,那也可以。
答案 0 :(得分:0)
是的,您可以使用以下代码,以便如果命令已经加载,则将其删除。
const botconfig = require("../config.json");
module.exports = {
name: 'reload',
type: "Developer",
description: 'Reloads a command (developer only)',
cooldown: 1,
execute(message, args) {
if (message.author.id != botconfig.developerid) return message.channel.send("Only my developer can use this command...");
message.channel.send("Developer command confirmed!");
if (!args.length) return message.channel.send(`You didn't pass any command to reload!`);
const commandName = args[0].toLowerCase();
if(message.client.commands.get(commandName)){
const command = message.client.commands.get(commandName) ||
message.client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return message.channel.send(`There is no command with name or alias \`${commandName}\`, ${message.author}!`);
delete require.cache[require.resolve(`./${command.name}.js`)];
}
try {
const newCommand = require(`./${commandName}.js`);
message.client.commands.set(commandName, newCommand);
message.channel.send("Command `" + commandName+ "` was reloaded!");
} catch (error) {
console.log(error);
message.channel.send("There was an error while reloading the `" + botconfig.prefix + commandName + "` command. \n\nError is as follows:\n``${error.message}`");
}
},
};