【发布时间】:2021-05-20 12:39:49
【问题描述】:
所以我做了一个重新加载命令,它重新加载预先存在的命令,而不会使机器人脱机。但现在我希望能够使用我添加的新命令进行更新。这是我现在拥有的代码
module.exports = {
name: 'reload',
aliasses: ['restart', 'rl'],
cooldown: 0,
usage: `reload <category> <command>`,
description: 'Reloads a command',
async execute(client, message, args, Discord, user, text){
if(message.author.id !== `DevID`) return message.channel.send('You are not a Dev');
if(!args[0]) return message.channel.send('You need to include the name of the command!');
let command = args[0].toLowerCase();
try {
delete require.cache[require.resolve(`../commands/${command}.js`)]
client.commands.delete(command);
const pull = require(`../commands/${command}.js`);
client.commands.set(command, pull);
return message.channel.send(`**${command}** was reloaded succesfully!`);
} catch (error) {
return message.channel.send(`There was an error trying to reload **${command}**: \`${error.message}\``);
}
}
}
最后我想重新加载预先存在的命令并加载新命令而不使机器人离线。
【问题讨论】:
-
那么您遇到的问题是什么?
-
我不确定如何在命令文件夹中检查是否有新命令已创建并在机器人不脱机的情况下对其进行更新
-
我自己制作了一个带有“热插拔”模块的系统,有点像这样。我使用 chokidar 监视文件更改并重新加载它们。每个文件都有一个加载和卸载函数来创建和清除监听器。如果要使用命令,可以为每个文件及其校验和创建一个注册表,并在输入命令时检查校验和的变化。您还可以重新加载所有命令。
-
这看起来对我有用,感谢@Seblor的帮助
标签: javascript node.js raspberry-pi discord.js