Search code examples
javascriptnode.jsdiscorddiscord.js

Slash command number game collector section won't respond to inputs from user Discord.js


I've assigned myself a little challenge to translate a bit of python I brewed up in a python learning course into javascript for my discord bot to handle. I was thinking of using a slash command builder and embeds along with subcommands.

I have come up with this..

const { EmbedBuilder, SlashCommandBuilder } = require("discord.js");

function randInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

module.exports = {
  data: new SlashCommandBuilder()
    .setName("numbergame")
    .setDescription("Play the number game!"),

  async execute(client, interaction) {
    const { member } = interaction;
    
    const correct_number = randInt(1, 100);
    let guess_count = 1;
  
    const embed = new EmbedBuilder()
      .setDescription(`Hey ${member.user.tag}! Welcome to my guessing game! I'm going to pick a number between 1 and 100. You have 1 minute to guess...`)
      .setColor('#2f3136')
      .setTimestamp()
      .setFooter({ text: `Requested by ${member.user.tag}`, iconURL: member.displayAvatarURL() });

    const filter = m => !isNaN(m.content) && m.author.id === interaction.user.id;
    const collector = interaction.channel.createMessageCollector({ filter, time: 60000 });

    await interaction.reply({ embeds: [embed]});

    collector.on('collect', async m => {
      const guess = randInt(m.content);
      if (guess < correct_number) {
        embed.setDescription(`Wrong. You need to guess higher. You have guessed ${guess_count} times. What is your guess?`);
        guess_count++;
      } else if (guess > correct_number) {
        embed.setDescription(`Wrong. You need to guess lower. You have guessed ${guess_count} times. What is your guess?`);
        guess_count++;
      } else {
        collector.stop();
        embed.setDescription(`Congrats! The right answer was ${correct_number}. It took you ${guess_count} guesses.`);
      }
      await interaction.editReply({ embeds: [embed] });
    });
      collector.on('end', collected => {
      if (collected.size === 0) {
        embed.setDescription(`Time's up! The right answer was ${correct_number}.`);
        interaction.editReply({ embeds: [embed] });
      }
    });
  }
}

It manages to run however i'm going through an issue where the bot isn't taking any of my inputs and after a minute it reveals the answer as it should and the number game ends.

EDIT: Tried checking if the collector itself was working by setting the filter to () => true however nothing happens and it still stayed the same after setting the filter value to true.

Anyone got any ideas on what i'm missing?? Thanks in advance !


Solution

  • found the issue and made changes from:

    collector.on('collect', async m => {
      const guess = randInt(m.content);
    

    to:

    collector.on('collect', async filter => {
      const guess = parseInt(filter.content);