javascript - 如何在不重新启动机器人的情况下加载新命令
问题描述
我有一个工作机器人,具有功能正常的命令处理程序,并且我有一个重新加载命令来更新我的代码以获取预先存在的命令。每当我添加新命令时,我都必须重新启动整个机器人。由于这个特定的机器人有运行间隔的脚本,重新启动我的机器人将终止所有运行间隔,迫使所有用户手动重新启动它们。我不想每次添加新命令时都不得不重新启动我的机器人,所以我需要帮助。
这是我现在的重新加载命令:
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}`");
}
},
};
我想在命令名称之前添加一个可选的“新”参数以专门查找新命令,因为当前代码有效,但它只看到预先存在的命令。如果更改当前代码以另外查找新命令会更简单,但如果没有找到仍然会出错,那也很好。
解决方案
是的,您可以使用以下代码,因此如果该命令已加载,则会将其删除。
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}`");
}
},
};
推荐阅读
- r - 如何将矩阵转换为 R 中的不同矩阵?
- java - 受信任的 SSL 证书显示为自签名
- angular - 表单更新后,反应式表单验证未在 UI 上更新
- python-pptx - python-pptx:控制文本轮廓颜色和宽度
- angular - 在 for 循环中使用 Angular HttpClient
- java - QuickSort(分区Hoare)的修改,先偶数降序,再奇数降序
- elasticsearch - 如何将我的 Elastic Search 服务器公开到 Internet?
- node.js - 节点中的 .done() 和 .end() 函数有什么区别以及何时使用它们?
- css - 在 Safari 中延迟飙升的错误。以哪种方式解决问题?
- python - 使用 exec 运行脚本时如何将 args 隐藏到 argparse?