cari

Rumah  >  Soal Jawab  >  teks badan

Pengumpul interaktif Discord.js gagal berjalan dengan betul dan kekurangan mesej ralat

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 harus console.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 pada message上进行收集,而不是像文档中一样在一个response. Tetapi bolehkah anda benar-benar hanya mengumpul jawapan? Ini nampaknya tidak betul. Apa yang saya buat salah?

P粉974462439P粉974462439440 hari yang lalu726

membalas semua(1)saya akan balas

  • P粉513318114

    P粉5133181142023-09-17 00:43:45

    Interaksi anda gagal kerana anda mengumpulkan komponen StringSelectMenu, bukan Button.

    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, gunakan i.customId.

    Rujukan: ComponentType

    balas
    0
  • Batalbalas