如何让Discord机器人Y监听机器人X发布的旧按钮点击?
如何配置机器人Y监听机器人X发布的含按钮Embed的点击事件
我已经创建了机器人Y及相关按钮文件,并将button.data.name设置为原按钮的custom id,现有代码如下:
button.js
const { Events, ButtonInteraction } = require('discord.js'); module.exports = { data: { name: '{1:1004078810158866463}' }, /** * * @param {ButtonInteraction} interaction * @param {Client} client */ async execute(interaction, client) { if ( interaction.channel.id !== '1004066286101344376') return; // if (!interaction.isButton) return; interaction.reply({ ephemeral: true, content: `Button clicked: ${interaction.customId}` }) } }
handleComponents.js
const { loadFiles } = require('../functions/fileLoader'); module.exports = (client) => { client.handleComponents = async () => { componentFiles = await loadFiles('components'); const { buttons } = client; const buttonsTable = [] await componentFiles.forEach(async file => { try { const button = await require(file); buttons.set(button.data.name, button); buttonsTable.push({Button: button.data.name, Status: '✅'}); } catch (e) { buttonsTable.push({Button: file.split('/').pop().slice(0,-3), Status: '❗️'}); console.error(e); } }) console.table(buttonsTable); } }
interactionCreate.js
const { Events, ChatInputCommandInteraction, codeBlock } = require('discord.js') module.exports = { name: Events.InteractionCreate, /** * @param {ChatInputCommandInteraction} interaction * @param {Client} client */ async execute(interaction,client) { if(interaction.isChatInputCommand()) {...} else if (interaction.isButton) { const replyMessages = { noCode: 'There is no code for this button.', error: 'Error trying to execute button' } const { buttons } = client; const { customId } = interaction; const button = await buttons.get(customId); if (!button) { interaction.reply({ ephemeral: true, content: replyMessages.noCode }) return new Error('There is no code for this button.') }; try { await button.execute(interaction, client) } catch (e) { console.error(e); if(interaction.user.id === '1234') replyMessages.error.concat('\n', codeBlock(e)); interaction.reply({ ephemeral: true, content: replyMessages.error }); } } } }
解决方案
1. 匹配正确的Custom ID
检查机器人X发布的按钮实际custom id,确保button.data.name和它完全一致。如果原按钮的custom id是1:1004078810158866463(不带大括号),则把name的值改为该字符串,去掉多余的大括号。
2. 修正类型注解错误
interactionCreate.js中,按钮交互的类型注解应为ButtonInteraction,而非ChatInputCommandInteraction,同时补充导入该类型:
const { Events, ChatInputCommandInteraction, ButtonInteraction, codeBlock } = require('discord.js') // ... /** * @param {ButtonInteraction} interaction * @param {Client} client */
3. 修复组件加载的异步逻辑
forEach不支持异步操作,改用for...of遍历组件文件,避免按钮注册异常:
// 替换原forEach代码 for (const file of componentFiles) { try { const button = require(file); // require为同步操作,无需await buttons.set(button.data.name, button); buttonsTable.push({Button: button.data.name, Status: '✅'}); } catch (e) { buttonsTable.push({Button: file.split('/').pop().slice(0,-3), Status: '❗️'}); console.error(e); } }
4. 确保机器人Y的权限
机器人Y需要在目标服务器拥有发送消息、嵌入链接权限,并且能访问按钮所在频道。
5. 其他细节优化
button.js中无需导入Events,可删除该导入项buttons.get(customId)是同步操作,无需await,直接改为const button = buttons.get(customId);- 错误处理中,字符串拼接需重新赋值,否则无法生效:
let errorMsg = replyMessages.error; if(interaction.user.id === '1234') errorMsg += '\n' + codeBlock(e); interaction.reply({ ephemeral: true, content: errorMsg });
内容的提问来源于stack exchange,提问作者Felipe9
相关产品推荐
相关产品推荐

