Discord抽奖报options.winnerCount非正整数错误求助
问题原因
执行命令!gstart 30s 1 nitro时抛出options.winnerCount is not a positive integer. (val=1)报错,核心原因是类型不匹配:
- 你从命令参数
args[2]中拿到的中奖人数是字符串类型的"1",不是数字类型的1 giveawaysManager对winnerCount字段做了严格校验,要求必须传入Number类型的正整数,字符串格式的数字即使值符合要求,也无法通过类型校验- 你原有代码只做了值的合法性判断,没有将参数从字符串转为数字类型,传入start方法时仍然是字符串,因此触发报错。
修复方法
只需要在读取中奖人数参数时直接将其转为整数类型,后续校验和传参都使用转换后的数字值即可,修改对应代码段如下:
- 修改中奖人数变量的初始化和校验逻辑:
// Number of winners let giveawayNumberWinners = parseInt(args[2]); // If the specified number of winners is not a number if(isNaN(giveawayNumberWinners) || giveawayNumberWinners <= 0){ return message.channel.send(':x: You have to specify a valid number of winners!'); }
- 启动抽奖的配置部分不需要额外改动,
winnerCount直接传入转换后的变量即可,此时传入的是合法的数字类型正整数,不会再触发校验报错,其余原有配置保持不变:
bot.giveawaysManager.start(giveawayChannel, { // The giveaway duration time: ms(giveawayDuration), // The giveaway prize prize: giveawayPrize, // The giveaway winner count winnerCount: giveawayNumberWinners, // Who hosts this giveaway hostedBy: message.author, // Messages messages: { giveaway: "🎉🎉 <strong>GIVEAWAY</strong> 🎉🎉", giveawayEnded: "🎉🎉 <strong>GIVEAWAY ENDED</strong> 🎉🎉", timeRemaining: "Time remaining: <strong>{duration}</strong>!", inviteToParticipate: "React with 🎉 to participate!", winMessage: "Congratulations, {winners}! You won <strong>{prize}</strong>!", embedFooter: "Giveaways", noWinner: "Giveaway cancelled, no valid participations.", hostedBy: "Hosted by: {user}", winners: "winner(s)", endedAt: "Ended at", units: { seconds: "seconds", minutes: "minutes", hours: "hours", days: "days", pluralS: true } } }); message.channel.send(`Giveaway started in ${giveawayChannel}!`);
内容的提问来源于stack exchange,提问作者Nguyễn Đắc Long
相关产品推荐
相关产品推荐

