Interaction Channel MessageCollector, Discord.js

Viewed 183

I'm trying to make a interaction based questionnaire, the user gets a DM with a button, by clicking the button, the user starts a message collector and the bot asks questions, however, the message collector seems to be setup wrong, it fails to read.

I haven't worked with interactions much, so I stick to classic commands, I'm trying to read the interaction channel and start a message collector in the DM, I hope I explained everything, any questions please ask.

Error: Cannot read properties of null (reading 'createMessageCollector')

Code:

// Main requirements
const { CHANNEL_SSU, CHANNEL_ROLESEL, BOT_PREFIX, ROLE_DEV } = require('../../config.json')
const commandName = 'gba'

// Optional Requirements
const { MessageEmbed, MessageActionRow, MessageButton, MessageCollector } = require('discord.js')

// Main Code
module.exports = client => {
    client.on('messageCreate', async message => {
        if (message.author.bot) return;
        if (!message.content.toLowerCase().startsWith(BOT_PREFIX)) return;

        const command = message.content.split(' ')
        const args = message.content.substring(1).split(' ')

        if (command.toString().toLowerCase().replace(BOT_PREFIX, '').startsWith(commandName)) {
            if (!message.member.roles.cache.has(ROLE_DEV)) return message.reply("Only Developers can use this command!")

            const buttonsRow1 = new MessageActionRow().addComponents(
                new MessageButton()
                .setCustomId('GBAempty1')
                .setLabel('  ')
                .setStyle('SECONDARY')
                .setDisabled(true),
                new MessageButton()
                .setCustomId('GBA')
                .setLabel('Appeal')
                .setStyle('DANGER')
                .setDisabled(false),
                new MessageButton()
                .setCustomId('GBAempty2')
                .setLabel('  ')
                .setStyle('SECONDARY')
                .setDisabled(true)
            )

            const GBAEmbed = new MessageEmbed()
            .setTitle('Appeal Your Game Ban!')
            .setColor('DARK_BLUE')
            // .addFields(
            //     {name: 'Goal:', value: 'Help with development of the **System Bot**.'},
            //     {name: "Notice:", value: `If you're found **Inactive**, you **will** be removed.`}
            // )
            .setFooter({ text: 'ASRP | System Bot' })
            .setThumbnail(message.guild.iconURL({ dynamic: true, size: 512 }))

            message.delete()
            message.channel.send({ embeds: [GBAEmbed], components: [buttonsRow1] })
        }
    })
    client.on(`interactionCreate`, async (interaction) => {
        if (!interaction.isButton) return
        
        if (interaction.customId == 'GBA') {
            await interaction.deferReply({
                ephemeral: true
            })

            const appealEmbed = new MessageEmbed()
                .setTitle(`Started Appeal.`)
                .setDescription(`Please check your DM for the appeal.`)
                // .addFields(
                //     {name: 'User', value: `${interaction.user} \`(${interaction.user.id})\``},
                //     {name: 'Action:', value: `\`\`\`${actionDone}\`\`\``}
                // )
                .setFooter({ text: 'ASRP | System Bot' })
                .setTimestamp()
                .setColor('ORANGE')

            await interaction.editReply({ embeds: [appealEmbed], ephemeral: true })

            let user = client.users.cache.get(interaction.member.user.id);

            const buttonsRow2 = new MessageActionRow().addComponents(
                new MessageButton()
                .setCustomId('GBAempty3')
                .setLabel('  ')
                .setStyle('SECONDARY')
                .setDisabled(true),
                new MessageButton()
                .setCustomId('GBAStart')
                .setLabel('Appeal')
                .setStyle('DANGER')
                .setDisabled(false),
                new MessageButton()
                .setCustomId('GBAempty4')
                .setLabel('  ')
                .setStyle('SECONDARY')
                .setDisabled(true)
            )


            const GBAstart = new MessageEmbed()
                .setTitle(`Start Appeal.`)
                .setDescription(`Press 'Start' to start appealing your game ban.`)
                .setFooter({ text: 'ASRP | System Bot' })
                .setThumbnail(interaction.guild.iconURL({ dynamic: true, size: 512 }))

            user.send({ embeds: [GBAstart], components: [buttonsRow2] })
        }

        if (interaction.customId == 'GBAStart') {
            await interaction.deferReply({
                ephemeral: true
            })

            const questions = [
                "How old are you?",
                "How are you?",
                "What time is for you?"
            ]

            let counter = 0

            const filter = m => m.author.id === message.author.id
            const collector = await interaction.channel.createMessageCollector({ filter, time: 1000 * 1200, max: 3, min: 3 });
            await interaction.channel.send(questions[counter++])
            collector.on('collect', (m) => {
            if (counter < questions.length) {
                m.reply(questions[counter++])
            }
            if (counter == questions.length + 1) {
                collector.stop() 
            }
            })
            collector.on('end', collected => {
                console.log(`Collected ${collected.size} messages`)

                if (collected.size < questions.length) {
                  interaction.channel.send('You didn\'t answer in time, please restart the appeal.')
                  return
                }
          
                let counter = 0
                collected.forEach((value) => {
                  console.log(questions[counter++], value.content)
                })
            });
        }
    })
}
Extra Information:
Node Version: 'v16.14.2'
NPM Version: '8.5.0'
Discord.js Version: '^13.7.0'

Thank you in advance.

0 Answers
Related