斜线命令数字游戏收集器部分不会响应用户 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 的输入
发布评论