【问题标题】: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我为 Discord 机器人创建了一个重新加载命令,我希望能够重新加载新命令而不是预先存在的命令
【发布时间】: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


【解决方案1】:

也许这会起作用:

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,但我知道如果您为 requireUncached() 放入正确的文件,您可以在此代码中使用变量“commands”访问命令

【讨论】:

    猜你喜欢
    • 2021-12-10
    • 1970-01-01
    • 2017-04-01
    • 2021-12-08
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2017-02-24
    • 2017-03-18
    相关资源
    最近更新 更多