-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCoinflip.js
102 lines (72 loc) · 2.68 KB
/
Coinflip.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const Coinflip = require("../settings/models/coinflip.js");
const Member = require("../settings/models/member.js");
const giveMoney = async (guildId, player, amount) => {
const db = await Member.findOne({ guild_id: guildId, user_id: player });
db.money += amount;
await db.save();
return giveMoney;
}
const pushArray = async (guildId, player, amount, space) => {
const db = await Coinflip.findOne({ guild_id: guildId });
db.data.push(`<@${player}> **+${amount}** | Place on: **${space}**`)
await db.save();
return pushArray;
}
const payoutWinners = async (guildId) => {
const db = await Coinflip.findOne({ guild_id: guildId });
for (let i = 0; i < db.history.length; i++) {
/// Print
const amount = db.history[i].bet;
const type = db.history[i].place;
const player = db.history[i].author;
const place = db.space;
if (type == place) { /// Support only red & black & green
// give x2 multipier
if (type == "heads") {
// give x2 multipier
const formatMoney = amount * 2;
await giveMoney(guildId, player, formatMoney);
await pushArray(guildId, player, formatMoney, type);
} else if (type == "tails") {
// give x2 multipier
const formatMoney = amount * 2;
await giveMoney(guildId, player, formatMoney);
await pushArray(guildId, player, formatMoney, type);
}
}
}
return payoutWinners;
}
const sendMsg = async (interaction, guildId) => {
const db = await Coinflip.findOne({ guild_id: guildId });
const place = db.space;
const str = db.data.join("\n")
interaction.channel.send({ content: `The coin landed on: **${place}** \n\n**Winners:**\n${str || "No body :("}` })
return sendMsg;
}
const getResult = async (guildId) => {
const db = await Coinflip.findOne({ guild_id: guildId });
const coins = ["heads", "tails"];
const result = coins[Math.floor(Math.random() * coins.length)];
db.space = result;
await db.save();
return getResult;
}
const betSave = async (guildId, space, money, userId) => {
const db = await Coinflip.findOne({ guild_id: guildId });
const data = {
place: space,
bet: money,
author: userId
};
db.history.push(data);
await db.save();
return betSave;
}
const revMoney = async (guildId, userId, money) => {
const db = await Member.findOne({ guild_id: guildId, user_id: userId });
db.money -= parseInt(money);
await db.save();
return revMoney;
}
module.exports = { betSave, revMoney, getResult, payoutWinners, sendMsg };