Discord /pin命令报错求助:应用无响应及MESSAGE_SEND_FAILED
修复Discord.js /pin命令的"The application did not respond"问题
问题根源
你的代码存在几个关键错误,直接导致命令执行超时,返回“应用未响应”:
- 漏了必要的导入:代码里用了
SlashCommandBuilder和Events,但没从discord.js中导入,运行时直接报错,命令无法正常执行。 - 消息链接处理错误:直接把完整的Discord消息URL传入
messages.fetch()无效,该方法需要的是消息ID而非完整链接,得先从URL里拆分出频道ID和消息ID,还要验证频道是否与当前命令执行频道一致。 - 未定义变量:代码中使用了
messageLink但从未定义,导致后续回复失败,触发未处理错误。 - 网关意图不足:仅启用
Guilds意图不够,获取频道消息需要额外添加GuildMessages和MessageContent意图(Discord.js v14及以上版本要求)。 - 权限缺失:钉选/取消钉选消息必须要有
ManageMessages权限,这是操作的必要条件,无法绕过。
修复后的完整代码
const { Client, Collection, GatewayIntentBits, SlashCommandBuilder, Events } = require('discord.js'); const { token } = require('./config.json'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); client.commands = new Collection(); client.commands.set('pin', { data: new SlashCommandBuilder() .setName('pin') .setDescription('钉选或取消钉选文本频道内的消息') .addStringOption(option => option.setName('message') .setDescription('要钉选/取消钉选的消息链接') .setRequired(true)), async execute(interaction) { try { await interaction.deferReply({ ephemeral: true }); const inputLink = interaction.options.getString('message'); // 用正则解析消息链接,提取关键ID const linkPattern = /https?:\/\/discord\.com\/channels\/(\d+)\/(\d+)\/(\d+)/; const matchResult = inputLink.match(linkPattern); if (!matchResult) { return await interaction.followUp('消息链接格式不对,请检查'); } const [, , targetChannelId, targetMessageId] = matchResult; // 验证消息是否在当前频道 if (targetChannelId !== interaction.channel.id) { return await interaction.followUp('只能操作当前频道里的消息哦'); } // 获取目标消息 const targetMessage = await interaction.channel.messages.fetch(targetMessageId); if (!targetMessage) { return await interaction.followUp('找不到这条消息'); } // 执行钉选/取消钉选 if (targetMessage.pinned) { await targetMessage.unpin({ reason: `由${interaction.user.tag}取消钉选` }); await interaction.followUp(`已取消钉选:${targetMessage.url}`); } else { await targetMessage.pin({ reason: `由${interaction.user.tag}钉选` }); await interaction.followUp(`已钉选:${targetMessage.url}`); } } catch(error) { console.error(error); await interaction.followUp('操作出错了,请检查消息链接或机器人权限'); } }, }); client.once(Events.ClientReady, readyClient => { console.log(`就绪!登录账号:${readyClient.user.tag}`); }); client.on(Events.InteractionCreate, async interaction => { if (!interaction.isChatInputCommand()) return; const command = interaction.client.commands.get(interaction.commandName); if (!command) { console.error(`没找到匹配${interaction.commandName}的命令`); return; } try { await command.execute(interaction); } catch (error) { console.error(error); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: '执行命令时出错了!', ephemeral: true }); } else { await interaction.reply({ content: '执行命令时出错了!', ephemeral: true }); } } }); client.login(token);
额外注意事项
- 给机器人配置
ManageMessages权限,否则钉选操作会失败; - 修改命令定义后,重新运行部署脚本更新全局命令;
- 在Discord开发者后台的机器人页面,务必开启
Message Content Intent,否则无法解析用户输入的消息链接。
内容的提问来源于stack exchange,提问作者TGekko
相关产品推荐
相关产品推荐

