我为 Discord 机器人创建了一个重新加载命令,我希望能够重新加载新命令而不是现有命令
I made a reload command for a Discord bot and I want to be able to reload commands that are a new command instead of pre existing commands
所以我做了一个重新加载命令,重新加载预先存在的命令,而无需将机器人拉下线。但现在我希望能够使用我添加的新命令进行更新。这是我现在的代码
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 但我知道你可以在这段代码中使用变量“commands”访问命令,如果你为 requireUncached() 输入正确的文件
所以我做了一个重新加载命令,重新加载预先存在的命令,而无需将机器人拉下线。但现在我希望能够使用我添加的新命令进行更新。这是我现在的代码
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 但我知道你可以在这段代码中使用变量“commands”访问命令,如果你为 requireUncached() 输入正确的文件