我有一个机器人,它有一个斜杠命令,可以将消息发送到与命令所在的频道不同的频道。该消息有两个按钮,当按下按钮时,应该console.log
点击按钮的用户的名称。以下是整个斜杠命令文件:
const { SlashCommandBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, ComponentType } = require('discord.js'); const { teamList } = require('../teamList'); const { Client, GatewayIntentBits } = require('discord.js'); const { token } = require('../config.json'); // 创建一个新的客户端实例 const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions] }); client.login(token); module.exports = { data: new SlashCommandBuilder() .setName('newaddgame') .setDescription('设置下注游戏。') .addStringOption(option => option.setName('team1') .setDescription('队伍1') .setRequired(true) .addChoices( { name: 'Bilibili Gaming', value: 'Bilibili Gaming'}, { name: 'JDG Gaming', value: 'JDG Gaming'}, { name: 'Gen.G', value: 'Gen.G'}, { name: 'T1', value: 'T1'}, { name: 'Cloud9', value: 'Cloud9'}, { name: 'Golden Guardians', value: 'Golden Guardians'}, { name: 'G2 Esports', value: 'G2 Esports'}, )) .addStringOption(option => option.setName('team2') .setDescription('队伍2') .setRequired(true) .addChoices( { name: 'Bilibili Gaming', value: 'Bilibili Gaming'}, { name: 'JDG Gaming', value: 'JDG Gaming'}, { name: 'Gen.G', value: 'Gen.G'}, { name: 'T1', value: 'T1'}, { name: 'Cloud9', value: 'Cloud9'}, { name: 'Golden Guardians', value: 'Golden Guardians'}, { name: 'G2 Esports', value: 'G2 Esports'}, )), async execute(interaction) { // 设置队伍信息 const team1 = interaction.options.getString('team1'); const team2 = interaction.options.getString('team2'); const teamArray = [team1, team2]; let teamMessage1 = ''; let teamMessage2 = ''; const team1Info = teamList.find(team => team.name === teamArray[0]); const team2Info = teamList.find(team => team.name === teamArray[1]); teamMessage1 = team1Info.emoji + ' ' + team1Info.name; teamMessage2 = team2Info.name + ' ' + team2Info.emoji; // 创建按钮 const team1Button = new ButtonBuilder() .setCustomId('team1Button') // .setLabel(team1Info.name) .setStyle(ButtonStyle.Secondary) .setEmoji(team1Info.emoji); const team2Button = new ButtonBuilder() .setCustomId('team2Button') // .setLabel(team2Info.name) .setStyle(ButtonStyle.Secondary) .setEmoji(team2Info.emoji); const row = new ActionRowBuilder() .addComponents(team1Button, team2Button); // 发送消息并添加按钮 await interaction.reply({content: '游戏已发布。', fetchReply: true}); const message2 = await client.channels.cache.get('1077612967639666738').send({ content: teamMessage1 + ' 对 ' + teamMessage2, components: [row], }); const collector = message2.createMessageComponentCollector({ componentType: ComponentType.StringSelect, time: 3_600_000 }); collector.on('collect', async i => { const selection = i.values[0]; console.log(`${i.user} 选择了 ${selection}!`); }); }, };
然而,重点在这里:
// 发送消息并添加按钮 await interaction.reply({content: '游戏已发布。', fetchReply: true}); const message2 = await client.channels.cache.get('1077612967639666738').send({ content: teamMessage1 + ' 对 ' + teamMessage2, components: [row], }); const collector = message2.createMessageComponentCollector({ componentType: ComponentType.StringSelect, time: 3_600_000 }); collector.on('collect', async i => { const selection = i.values[0]; console.log(`${i.user} 选择了 ${selection}!`); });
现在,当我按下消息按钮之一时,在Discord中它只会显示"This interaction failed",但控制台没有错误,机器人也不会崩溃。它只是什么都不做。我一直在按照这里的文档进行操作:https://discordjs.guide/message-components/interactions.html#awaiting-components 。
我想知道是否因为我在一个message
上进行收集,而不是像文档中一样在一个response
上进行收集。但是你真的只能在响应上进行收集吗?这似乎不对。我做错了什么?
您的交互失败,因为您正在收集一个StringSelectMenu组件,而不是一个Button。
请更改以下行:
更改为:
要收集按钮,请使用
i.customId
。参考:ComponentType