我为Discord机器人制作了一个重新加载命令,我希望能够重新加载新命令,而不是预先存在的命令



所以我制作了一个重新加载命令,它可以重新加载预先存在的命令,而不会使机器人离线。但现在我希望能够用我添加的新命令进行更新。这是我的代码,我现在有

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}``);
}
}
} 

最后,我想在不让机器人离线的情况下重新加载预先存在的命令和加载新命令。

也许这会奏效:

const Discord = require('discord.js');
const client = new Discord.Client();
var commands;
function requireUncached(module) {
delete require.cache[require.resolve(module)];
return require(module);
}
setInterval(() => {
commands = requireUncached(`whatever your file name is`);
}, 500)

所以我从来没有尝试过module.exports,但我知道,如果你把requireUnached((放在正确的文件中,你可以用这段代码中的变量"commands"访问命令

最新更新