斜线命令数字游戏收集器部分不会响应用户 Discord.js 的输入

编程入门 行业动态 更新时间:2024-10-03 12:40:11

<a href=https://www.elefans.com/category/jswz/34/1737661.html style=斜线命令数字游戏收集器部分不会响应用户 Discord.js 的输入"/>

斜线命令数字游戏收集器部分不会响应用户 Discord.js 的输入

我给自己分配了一个小挑战,将我在 python 学习课程中酝酿的一些 python 翻译成 javascript,供我的 discord 机器人处理。我正在考虑使用斜杠命令生成器并嵌入子命令。

我想出了这个..

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] });
      }
    });
  }
}

它设法运行但是我遇到了一个问题,即机器人没有接受我的任何输入,一分钟后它显示了它应该的答案并且数字游戏结束了。

EDIT:尝试通过将过滤器设置为

() => true
来检查收集器本身是否正在工作,但是没有任何反应,并且在将过滤器值设置为 true 后它仍然保持不变。

有人对我缺少的东西有任何想法吗?提前致谢!

回答如下:

发现问题并进行更改:

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

至:

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

更多推荐

斜线命令数字游戏收集器部分不会响应用户 Discord.js 的输入

本文发布于:2024-05-31 05:25:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771323.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜线   命令   数字   收集器   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!