Skip to content

Commit d701a17

Browse files
committed
feat: improve slots game
1 parent 9193d2f commit d701a17

File tree

1 file changed

+108
-107
lines changed

1 file changed

+108
-107
lines changed

src/Slots.js

Lines changed: 108 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,110 @@
1-
const { EmbedBuilder } = require('discord.js');
2-
const events = require('events');
3-
1+
const { EmbedBuilder } = require("discord.js")
2+
const events = require("events")
43

54
module.exports = class Slots extends events {
6-
constructor(options = {}) {
7-
8-
if (!options.isSlashGame) options.isSlashGame = false;
9-
if (!options.message) throw new TypeError('NO_MESSAGE: No message option was provided.');
10-
if (typeof options.message !== 'object') throw new TypeError('INVALID_MESSAGE: message option must be an object.');
11-
if (typeof options.isSlashGame !== 'boolean') throw new TypeError('INVALID_COMMAND_TYPE: isSlashGame option must be a boolean.');
12-
13-
14-
if (!options.embed) options.embed = {};
15-
if (!options.embed.title) options.embed.title = 'Slot Machine';
16-
if (!options.embed.color) options.embed.color = '#5865F2';
17-
if (!options.slots) options.slots = ['🍇', '🍊', '🍋', '🍌'];
18-
19-
20-
if (typeof options.embed !== 'object') throw new TypeError('INVALID_EMBED: embed option must be an object.');
21-
if (typeof options.embed.title !== 'string') throw new TypeError('INVALID_EMBED: embed title must be a string.');
22-
if (typeof options.embed.color !== 'string') throw new TypeError('INVALID_EMBED: embed color must be a string.');
23-
if (!Array.isArray(options.slots)) throw new TypeError('INVALID_SLOTS: slots option must be an array.');
24-
25-
super();
26-
this.options = options;
27-
this.message = options.message;
28-
this.slot1 = this.slot2 = this.slot3 = 0;
29-
this.slots = options.slots;
30-
this.result = null;
31-
}
32-
33-
34-
getBoardContent(showResult) {
35-
let board = '```\n-------------------\n';
36-
board += `${this.wrap(this.slot1, false)} : ${this.wrap(this.slot2, false)} : ${this.wrap(this.slot3, false)}\n\n`;
37-
board += `${this.slots[this.slot1]} : ${this.slots[this.slot2]} : ${this.slots[this.slot3]} <\n\n`;
38-
board += `${this.wrap(this.slot1, true)} : ${this.wrap(this.slot2, true)} : ${this.wrap(this.slot3, true)}\n`;
39-
board += '-------------------\n';
40-
41-
if (showResult) board += `| : : ${(this.hasWon() ? 'WON ' : 'LOST')} : : |`;
42-
return (board + '```');
43-
}
44-
45-
46-
async sendMessage(content) {
47-
if (this.options.isSlashGame) return await this.message.editReply(content);
48-
else return await this.message.channel.send(content);
49-
}
50-
51-
52-
async startGame() {
53-
if (this.options.isSlashGame || !this.message.author) {
54-
if (!this.message.deferred) await this.message.deferReply().catch(e => {});
55-
this.message.author = this.message.user;
56-
this.options.isSlashGame = true;
57-
}
58-
this.slotMachine();
59-
60-
61-
const embed = new EmbedBuilder()
62-
.setColor(this.options.embed.color)
63-
.setTitle(this.options.embed.title)
64-
.setDescription(this.getBoardContent())
65-
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) })
66-
const msg = await this.sendMessage({ embeds: [embed] });
67-
68-
69-
setTimeout(async () => {
70-
this.slotMachine();
71-
embed.setDescription(this.getBoardContent());
72-
73-
this.slotMachine();
74-
await msg.edit({ embeds: [embed] });
75-
setTimeout(() => { this.gameOver(msg) }, 2000);
76-
}, 2000);
77-
}
78-
79-
80-
gameOver(msg) {
81-
const SlotsGame = { player: this.message.author, slots: [this.slot1, this.slot2, this.slot3].map(s => this.slots[s]) };
82-
this.emit('gameOver', { result: (this.hasWon() ? 'win':'lose'), ...SlotsGame });
83-
84-
85-
const embed = new EmbedBuilder()
86-
.setColor(this.options.embed.color)
87-
.setTitle(this.options.embed.title)
88-
.setDescription(this.getBoardContent(true))
89-
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) })
90-
91-
return msg.edit({ embeds: [embed] });
92-
}
93-
94-
95-
slotMachine() {
96-
this.slot1 = Math.floor(Math.random() * this.slots.length);
97-
this.slot2 = Math.floor(Math.random() * this.slots.length);
98-
this.slot3 = Math.floor(Math.random() * this.slots.length);
99-
}
100-
101-
hasWon() {
102-
return (this.slot1 === this.slot2 && this.slot1 === this.slot3);
103-
}
104-
105-
wrap(s, add) {
106-
if (add) return (s+1 > this.slots.length-1) ? this.slots[0] : this.slots[s+1];
107-
return (s-1 < 0) ? this.slots[this.slots.length-1] : this.slots[s-1];
108-
}
109-
}
5+
constructor(options = {}) {
6+
if (!options.isSlashGame) options.isSlashGame = false
7+
if (!options.message) throw new TypeError("NO_MESSAGE: No message option was provided.")
8+
if (typeof options.message !== "object") throw new TypeError("INVALID_MESSAGE: message option must be an object.")
9+
if (typeof options.isSlashGame !== "boolean")
10+
throw new TypeError("INVALID_COMMAND_TYPE: isSlashGame option must be a boolean.")
11+
12+
if (!options.embed) options.embed = {}
13+
if (!options.embed.title) options.embed.title = "Slot Machine"
14+
if (!options.embed.color) options.embed.color = "#5865F2"
15+
if (!options.slots) options.slots = ["🍇", "🍊", "🍋", "🍌"]
16+
17+
if (typeof options.embed !== "object") throw new TypeError("INVALID_EMBED: embed option must be an object.")
18+
if (typeof options.embed.title !== "string") throw new TypeError("INVALID_EMBED: embed title must be a string.")
19+
if (typeof options.embed.color !== "string") throw new TypeError("INVALID_EMBED: embed color must be a string.")
20+
if (!Array.isArray(options.slots)) throw new TypeError("INVALID_SLOTS: slots option must be an array.")
21+
22+
super()
23+
this.options = options
24+
this.message = options.message
25+
this.slot1 = this.slot2 = this.slot3 = 0
26+
this.slots = options.slots
27+
this.result = null
28+
}
29+
30+
getBoardContent(showResult) {
31+
let board = "```\n-------------------\n"
32+
board += `${this.wrap(this.slot1, false)} : ${this.wrap(this.slot2, false)} : ${this.wrap(
33+
this.slot3,
34+
false
35+
)}\n\n`
36+
board += `${this.slots[this.slot1]} : ${this.slots[this.slot2]} : ${this.slots[this.slot3]} <\n\n`
37+
board += `${this.wrap(this.slot1, true)} : ${this.wrap(this.slot2, true)} : ${this.wrap(this.slot3, true)}\n`
38+
board += "-------------------\n"
39+
40+
if (showResult) board += `| : : ${this.hasWon() ? "WON " : "LOST"} : : |`
41+
return board + "```"
42+
}
43+
44+
async sendMessage(content) {
45+
if (this.options.isSlashGame) return await this.message.editReply(content)
46+
else return await this.message.channel.send(content)
47+
}
48+
49+
async startGame() {
50+
if (this.options.isSlashGame || !this.message.author) {
51+
if (!this.message.deferred) await this.message.deferReply().catch((e) => {})
52+
this.message.author = this.message.user
53+
this.options.isSlashGame = true
54+
}
55+
this.slotMachine()
56+
57+
const embed = new EmbedBuilder()
58+
.setColor(this.options.embed.color)
59+
.setTitle(this.options.embed.title)
60+
.setDescription(this.getBoardContent())
61+
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) })
62+
const msg = await this.sendMessage({ embeds: [embed] })
63+
64+
const shuffler = setInterval(async () => {
65+
await this.shuffle(embed, msg)
66+
}, 600)
67+
68+
setTimeout(() => {
69+
clearInterval(shuffler)
70+
this.gameOver(msg)
71+
}, 5000)
72+
}
73+
74+
gameOver(msg) {
75+
const SlotsGame = {
76+
player: this.message.author,
77+
slots: [this.slot1, this.slot2, this.slot3].map((s) => this.slots[s]),
78+
}
79+
this.emit("gameOver", { result: this.hasWon() ? "win" : "lose", ...SlotsGame })
80+
81+
const embed = new EmbedBuilder()
82+
.setColor(this.options.embed.color)
83+
.setTitle(this.options.embed.title)
84+
.setDescription(this.getBoardContent(true))
85+
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) })
86+
87+
return msg.edit({ embeds: [embed] })
88+
}
89+
90+
slotMachine() {
91+
this.slot1 = Math.floor(Math.random() * this.slots.length)
92+
this.slot2 = Math.floor(Math.random() * this.slots.length)
93+
this.slot3 = Math.floor(Math.random() * this.slots.length)
94+
}
95+
96+
async shuffle(embed, msg) {
97+
this.slotMachine()
98+
embed.setDescription(this.getBoardContent())
99+
await msg.edit({ embeds: [embed] })
100+
}
101+
102+
hasWon() {
103+
return this.slot1 === this.slot2 && this.slot1 === this.slot3
104+
}
105+
106+
wrap(s, add) {
107+
if (add) return s + 1 > this.slots.length - 1 ? this.slots[0] : this.slots[s + 1]
108+
return s - 1 < 0 ? this.slots[this.slots.length - 1] : this.slots[s - 1]
109+
}
110+
}

0 commit comments

Comments
 (0)