Pengumpul interaktif Discord.js gagal berjalan dengan betul dan kekurangan mesej ralat
P粉974462439
P粉974462439 2023-09-16 09:22:06
0
1
607

Saya mempunyai bot yang mempunyai perintah slash yang menghantar mesej ke saluran yang berbeza daripada yang dihidupkan oleh arahan itu. Mesej itu mempunyai dua butang dan apabila butang itu ditekan, ia harusconsole.logmenamakan pengguna yang mengklik butang tersebut. Berikut ialah keseluruhan fail arahan slash:

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}!`); }); }, };

Namun, inilah maksudnya:

// 发送消息并添加按钮 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}!`); });

Kini apabila saya menekan salah satu butang mesej, dalam Discord ia hanya mengatakan "Interaksi ini gagal" tetapi tiada ralat dalam konsol dan bot tidak ranap. Ia hanya melakukan apa-apa. Saya telah mengikuti dokumentasi di sini: https://discordjs.guide/message-components/interactions.html#awaiting-components.

Saya tertanya-tanya sama ada kerana saya mengumpul padamessage上进行收集,而不是像文档中一样在一个response. Tetapi bolehkah anda benar-benar hanya mengumpul jawapan? Ini nampaknya tidak betul. Apa yang saya buat salah?

P粉974462439
P粉974462439

membalas semua (1)
P粉513318114

Interaksi anda gagal kerana anda mengumpulkan komponenStringSelectMenu, bukanButton.

Sila tukar baris berikut:

const collector = message2.createMessageComponentCollector({ componentType: ComponentType.StringSelect, time: 3_600_000 });

Ditukar kepada:

const collector = message2.createMessageComponentCollector({ componentType: ComponentType.Button, time: 3_600_000 });

Untuk mengumpul butang, gunakani.customId.

Rujukan:ComponentType

    Muat turun terkini
    Lagi>
    kesan web
    Kod sumber laman web
    Bahan laman web
    Templat hujung hadapan
    Tentang kita Penafian Sitemap
    Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!