有没有办法在不终止进程的情况下更新我的不一致机器人的代码?
例如,如果我更改了命令代码中的单个字母,我是否必须完全重新启动机器人才能更新它?那么非命令代码呢?
我最近重启了我的机器人,以更新/测试代码,Discorde给我发了一条消息,说我的机器人重启太频繁了.
我应该忽略该消息,或者您有无需重新启动即可更新的解决方案吗?
谢谢.
有没有办法在不终止进程的情况下更新我的不一致机器人的代码?
例如,如果我更改了命令代码中的单个字母,我是否必须完全重新启动机器人才能更新它?那么非命令代码呢?
我最近重启了我的机器人,以更新/测试代码,Discorde给我发了一条消息,说我的机器人重启太频繁了.
我应该忽略该消息,或者您有无需重新启动即可更新的解决方案吗?
谢谢.
例如,如果命令处理程序是这样创建的,
index.js
个
const { Collection } = require("discord.js");
client.slash = new Collection();
const slashCommandFolders = fs.readdirSync("./slash");
for (const folder of slashCommandFolders) {
const commandFiles = fs
.readdirSync(`./slash/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./slash/${folder}/${file}`);
client.slash.set(command.name, command);
}
}
(命令文件在slash/<category>/<command_name>.js
中)
slash/general/ping.js
个
const { CommandInteraction } = require("discord.js");
module.exports = {
name: "ping",
/**
*
* @param {CommandInteraction} i
*/
execute(i) {
i.reply({ content: "Pong" });
},
};
(interactionCreate事件需要正确处理,但我们在这里将跳过这一点)
通过创建这样的重新加载命令:
src/admin/reload.js
个
const {
CommandInteraction,
Client,
CommandInteractionOptionResolver,
} = require("discord.js");
const fs = require("fs");
module.exports = {
name: "reload",
/**
*
* @param {CommandInteraction} i
* @param {Client} client
* @param {CommandInteractionOptionResolver} options
*/
async execute(i, client, options) {
const commandName = options.getString("command").toLowerCase();
const command =
i.client.slash.get(commandName) ||
i.client.slash.find(
(cmd) => cmd.aliases && cmd.aliases.includes(commandName)
);
if (!command)
return i.reply({ content: "No command found", ephemeral: true });
const commandFolders = fs.readdirSync("./slash");
const folderName = commandFolders.find((folder) =>
fs.readdirSync(`./slash/${folder}`).includes(`${command.name}.js`)
);
delete require.cache[
require.resolve(`../${folderName}/${command.name}.js`)
];
try {
const newCommand = require(`../${folderName}/${command.name}.js`);
i.client.slash.set(newCommand.name, newCommand);
i.reply({ content: "reloaded", ephemeral: true });
} catch (error) {
console.error(error);
i.reply({ content: "reload failed", ephemeral: true });
}
},
};
我们可以应用更改,而无需重启bot.所以衣冠楚楚的系统