我一直在尝试自己编写新的机器人,查看用 python 编写的不同项目。由于它们是用 python 编写的,我不知道如何将它们迁移到 javascript,因此我正在搜索文档。然而,我在这些文档中找不到类似
tree
或 CommandTree
的内容。我使用discord.js .
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
client.on('ready', async () => {
await client.user.setActivity({ type: 'WATCHING', name: loadConfigs.activity_name });
if (!fs.existsSync(`${process.env.DATA_PATH}tracked_accounts.db`)) {
initDB();
}
// TODO: Handle tree.on_error and load cogs
log.info(`${client.user.tag} is online`);
const slashCommands = **await client.tree.sync(); //?!?!??**
log.info(`Synced ${slashCommands.length} slash commands`);
});
基本上,这里的 python 代码与 javascript 的等价物是什么。我想同步斜杠命令。
discord.js 不以这种方式对待命令,由用户来处理它们如何加载以及如何监听事件。
我建议您阅读本指南,其中展示了如何实现命令: https://discordjs.guide/creating-your-bot/slash-commands.html