我有一個機器人,它有一個斜線命令,可以將訊息發送到與命令所在的頻道不同的頻道。這個訊息有兩個按鈕,當按下按鈕時,應該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