discord.js v13命令处理器无法注册/显示斜杠命令问题求助
问题描述
当前使用discord.js开发的机器人无法注册新创建的kick斜杠命令,重新邀请机器人入服、等待超过1小时后命令仍未出现在指令列表中,当前kick命令逻辑尚未编写完成,处于discord.js v13学习阶段。
相关代码
slash-register.js 文件代码
const { Routes } = require('discord-api-types/v9'); const { token } = require('./config.json'); const fs = require('node:fs'); const commands = []; const commandList = new Map() module.exports = (updateCommands) => { const commands = []; const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); // Place your client and guild ids here const clientId = '908818107093622814'; const guildId = '961765341451665498'; for (const file of commandFiles) { const command = require(`./commands/${file}`); commands.push(command.data.toJSON()); commandList.set(command.data.name, command.run) } const rest = new REST({ version: '9' }).setToken(token); if (updateCommands) { (async () => { try { console.log('Started refreshing application (/) commands.'); console.log(commands) /* await rest.put( Routes.applicationCommands(clientId), { body: commands }, ); */ await rest.put( Routes.applicationGuildCommands(clientId, guildId), { body: commands }, ); console.log('Successfully reloaded application (/) commands.'); } catch (error) { console.error(error); } })(); } } module.exports.commands = commandList;
kick.js 文件代码
module.exports.data = new SlashCommandBuilder() .setName("kick") .setDescription("Kick the user selected.") .addUserOption(option => option.setName("person").setDescription("The person to kick.")); module.exports.run = (bot,interaction) => { let permissions = interaction.member.permissions; if(!permissions.has("MANAGE_MESSAGES")) return interaction.editReply({content: "You don't have the correct permissions."}) interaction.editReply({ content: "You are trying to kick a member." }) }
index.js 文件代码
const Discord = require('discord.js'); const Bot = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] }); const { token } = require('./config.json') require("./slash-register.js")() let commands = require("./slash-register.js").commands; // Bot actions Bot.on('ready', () => { console.log(`Logged in as ${Bot.user.tag}!`); let commands = Bot.application.commands }); Bot.on('interactionCreate', async interaction => { if(!interaction.isCommand()) return; let name = interaction.commandName; let options = interaction.options; let commandMethod = commands.get(name); if(!commandMethod) return; await interaction.deferReply(); commandMethod(Bot, interaction) }) // Token Bot.login(token);
附:我听说斜杠命令加载最长需要等待约1小时,但截至撰写问题时机器人已经运行超过1小时,命令仍然没有显示。
错误原因及修复方案
注册逻辑根本未触发
index.js中调用注册函数时写的是require("./slash-register.js")(),没有传入任何参数,而slash-register.js导出的函数只有在入参updateCommands为真值时才会执行命令上传逻辑,相当于从未发起过命令注册请求。
修复:调用时传入true,修改为require("./slash-register.js")(true)。缺少必要依赖导入,代码运行直接报错
slash-register.js中使用了REST类但未导入,需要在文件顶部添加:
const { REST } = require('@discordjs/rest');kick.js中使用了SlashCommandBuilder但未导入,需要在文件顶部添加:
const { SlashCommandBuilder } = require('@discordjs/builders');如果未安装这两个依赖,先在项目目录执行
npm install @discordjs/rest @discordjs/builders discord-api-types@v9安装适配v13版本的对应依赖。缓存认知错误
修复上述问题后重启机器人,查看控制台是否打印Successfully reloaded application (/) commands.日志,打印成功后直接按Ctrl+R刷新Discord客户端即可。公会级斜杠命令是即时生效的,不需要等待1小时,只有全局斜杠命令才存在最长1小时的缓存延迟。冗余代码清理(不影响功能,建议删除)
index.js的ready事件回调中写的let commands = Bot.application.commands会重新声明和外部同名的变量,无实际作用,直接删除即可。
修复完成后重启机器人,即可在对应服务器的斜杠命令列表中看到kick命令。
内容的提问来源于stack exchange,提问作者h3lped

