如何使命令随机失败



好吧,在我的机器人上,我有一个beg命令,但问题是我希望它有时会失败。

例如,有一次它可能会说"有人给了你5美元",但如果失败了,它会说"名人:NOPE">

下面的代码从所有文本所在的文件中随机选择一个文本。有没有办法让它随机失败,这样用户就不会得到钱?

const { RichEmbed } = require("discord.js");
const { stripIndents } = require("common-tags");
const { prefix } = require("../../botconfig.json");
const db = require('quick.db')
let bal = require("../../database/balance.json");
const fs = require('fs');
const cooldowns = new Map();
const humanizeDuration = require('humanize-duration');
//Set cooldown

module.exports = {
name: "beg",
aliases: [],
category: "economy",
description: "Gets you money",
usage: "[command | alias]",
run: async (client, message, args) => {
const cooldown = cooldowns.get(message.author.id);
if (cooldown) {
const remaining = humanizeDuration(cooldown - Date.now(),{ units: ['s'],round: true });
let cEmbed = new RichEmbed()
.setColor("RANDOM")
.setTitle("Slow down, cmon!")
.setDescription(`You dont want to be like a cry baby! You will be able to beg in `${remaining}` just you wait!nnWhile you wait why not follow our [Twitter](https://twitter.com/switchoffical)`)
return message.channel.send(cEmbed)
.catch(console.error);
} else {
if(!bal[message.author.id]){
bal[message.author.id] = {
balance: 0
};
} 
const Jwork = require('../../beg.json');
const JworkR = Jwork[Math.floor(Math.random() * Jwork.length)];
var random = Math.floor(Math.random() * 20) + 3;
let curBal = bal[message.author.id].balance 
bal[message.author.id].balance = curBal + random;
fs.writeFile('././database/balance.json', JSON.stringify(bal, null, 2), (err) => {
let embed = new RichEmbed() 
.setColor("RANDOM") 
.setDescription(`** ${message.author.username}**, ${JworkR} 💵 **${random}**`) 
message.channel.send(embed)
if (err) console.log(err)
});
//Adds the user to the set so that they can't talk for a minute
cooldowns.set(message.author.id, Date.now() + 10000);
setTimeout(() => cooldowns.delete(message.author.id), 10000);
}
}
}

我只是不知道如何让失败

因此,您可以运行一个Math.floor((Math.random()-0.001)*4),将其存储到一个变量中。现在您有一个从0到3的随机数(4个不同的数字/结果(。然后检查您的新变量是否等于0。if(failChance === 0)如果它是真的,只是不要执行add-bal-cmd。

示例:

...
} else {
var failChance = Math.floor((Math.random()-0.001)*4);
if(failChance === 0){
message.channel.send('FAILURE');
return;
}
if(!bal[message.author.id]){
...

最新更新