使用户失败他们的抢劫尝试不和谐.js



好的,所以我为抢劫尝试失败的用户编写了一个小代码。我只是不知道把它放在哪里。它的工作方式是,如果有人说;rob它返回一条消息TestUser, you were caught and paid 500 coins to the user you were robbing from!当该消息显示时,作者将失去 500 个硬币,目标将获得 500 个硬币。

当前 rob 命令的代码为

run: async (client, message, args) => {
const cooldown = cooldowns.get(message.author.id);
if (cooldown) {
const remaining = humanizeDuration(cooldown - Date.now(),{ units: ['m', 's'],round: true });
let cEmbed = new MessageEmbed()
.setColor("RANDOM")
.setTitle("Slow down, cmon!")
.setDescription(`You will be able to work 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 {
let member = message.mentions.users.first() || client.users.cache.get(args[0]);
let user = message.mentions.members.first()
if (!user) return message.channel.send('Sorry, you forgot to mention somebody.')
let targetuser = await db.fetch(`money_${user.id}`) // fetch mentioned users balance
let author = await db.fetch(`money_${message.author.id}`) // fetch authors balance
let uBalance = balance[member.id].balance;
let TuBalance = balance[user.id].balance;
let random = Math.floor(Math.random() * 200) + 1; // random number 200-1, you can change 200 to whatever you'd like
let curBal = balance[message.author.id].balance 
balance[message.author.id].balance = curBal + random;
let crurBal = balance[message.author.id].balance 
balance[user.id].balance = crurBal - random;
if (uBalance < 500) { // if the authors balance is less than 250, return this.
return message.channel.send(':x: You need at least 500$ to rob somebody.')
}
if (TuBalance < 0) { // if mentioned user has 0 or less, it will return this.
return message.channel.send(`:x: ${user.user.username} does not have anything to rob.`)
}

message.channel.send(`${message.author} you robbed ${user} and got away with ${random}!`)
cooldowns.set(message.author.id, Date.now() + 900000);
setTimeout(() => cooldowns.delete(message.author.id), 900000);
db.subtract(`money_${user.id}`, random)
db.add(`money_${message.author.id}`, random)
} 
}

我已经设置了失败机会,剩下的消息是我把它放在当前代码中的什么地方,以及如何让它从作者那里减去 500 个硬币,然后给目标 500 个硬币。我也希望他们成功抢劫的机会非常低。这是失败代码

var failChance = Math.floor((Math.random()500)*4);
if(failChance === 0){
message.channel.send(`**${message.author.username}** you were caught so you paied 500 coins to the user you were robbing from.`);
return;
}

这是我建议你处理成功/失败场景的方法:

...
else {
...
if (TuBalance < 0) { // if mentioned user has 0 or less, it will return this.
return message.channel.send(`:x: ${user.user.username} does not have anything to rob.`)
}
let testChance = Math.random() * 100;
// 5 here is percentage of success.
if ((testChance -= 5) < 0) {
// Success!
message.channel.send(`${message.author} you robbed ${user} and got away with ${random}!`);
db.subtract(`money_${user.id}`, random);
db.add(`money_${message.author.id}`, random);
} else {
// Fail :(
message.channel.send(`**${message.author.username}** you were caught so you paied 500 coins to the user you were robbing from.`);
db.add(`money_${user.id}`, random);
db.subtract(`money_${message.author.id}`, random);
}
cooldowns.set(message.author.id, Date.now() + 900000);
setTimeout(() => cooldowns.delete(message.author.id), 900000);
}

这里有一个小的实用功能给你:

/**
* Test chance out of 100.
* @param {Number} succesPercentage Percentage of chance to succes.
* @returns {Boolean} Whenever the test is a succes or not. 
*/
function testChance(successPercentage) {
let random = Math.random() * 100;
return ((random -= successPercentage) < 0);
}

用法:

if (testChance(5)) {
// Success scenario
} else {
// Fail scenario
}

最新更新