This repository was archived by the owner on Jan 19, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathrockpaperscissors.js
More file actions
75 lines (64 loc) · 2.5 KB
/
rockpaperscissors.js
File metadata and controls
75 lines (64 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { Command, CommandError, SwitchbladeEmbed, Constants } = require('../../')
const handEmojis = ['✊', '✋', '✌']
const objectEmojis = ['🪨', '📄', '✂️']
module.exports = class RockPaperScissorsCommand extends Command {
constructor (client) {
super({
name: 'rockpaperscissors',
aliases: ['rps'],
category: 'games',
parameters: [{
type: 'string',
clean: true,
required: true,
whitelist: ['rock', 'r', 'paper', 'p', 'scissors', 's'],
missingError: 'commands:rockpaperscissors.noChoice'
},
{
type: 'number', min: 1, required: false
},
[{
type: 'booleanFlag', name: 'hand', aliases: ['h', 'hands']
}]]
}, client)
}
async run ({ t, author, channel, flags }, choice, betValue) {
console.log(betValue)
const embed = new SwitchbladeEmbed(author)
const emojis = flags.hand ? handEmojis : objectEmojis
const botSelected = ['r', 'p', 's'][Math.floor(Math.random() * 3)]
const botSelectedEmoji = this.getEmoji(botSelected, emojis)
const result = this.calculateResult(choice, botSelected)
if (betValue) {
const balance = await this.client.controllers.economy.balance(author.id)
if (betValue > balance) throw new CommandError(t('commands:rockpaperscissors.notEnoughMoney'))
embed.setDescription(t('commands:rockpaperscissors.bet.' + result, { count: betValue }))
if (result === 'win') {
await this.client.controllers.economy.give(author.id, betValue)
} else if (result === 'lose') {
await this.client.controllers.economy.take(author.id, betValue)
}
}
embed.setTitle(`${this.getEmoji(choice, emojis)} 🆚 ${botSelectedEmoji}`)
embed.setAuthor(t('commands:rockpaperscissors.' + result))
if (result !== 'draw') {
embed.setColor(
result === 'win' ? Constants.GENERIC_SUCCESS : Constants.GENERIC_FAILURE
)
}
channel.send(embed)
}
getEmoji (choice, emojis) {
const /* valve */index = ['r', 'p', 's'].indexOf(choice[0].toLowerCase())
return emojis[index]
}
// Calculates the result on the choice1's perspective
calculateResult (_choice1, _choice2) {
const choice1 = _choice1[0].toLowerCase()
const choice2 = _choice2[0].toLowerCase()
if (choice1 === choice2) return 'draw'
if (choice1 === 'r') return choice2 === 'p' ? 'lose' : 'win'
if (choice1 === 'p') return choice2 === 's' ? 'lose' : 'win'
if (choice1 === 's') return choice2 === 'r' ? 'lose' : 'win'
}
}