-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageUtils.js
67 lines (64 loc) · 2.58 KB
/
messageUtils.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
const database = require("./database")
const discord = require("discord.js")
const utils = require("./utils")
let canRunGame = async (user, bet, checkBet = true) => {
let profile = database.getUser(user.id)
if (profile.madness > 0) return `${user} has madness!`
let parsed = parseInt(bet)
if (checkBet && (isNaN(parsed) || parsed <= 0 || !isFinite(parsed))) return "Incorrect bet!"
if (profile.money - bet < 0) return `${user} doesn't have enough money!`
return true
}
let advancedSearchUser = (message, args, client, bet, searchMessage, acceptMessage) => new Promise(async (resolve, reject) => {
let foundUser = await utils.searchUser(client, message, args.slice(1))
if (foundUser && foundUser.id != message.author.id) {
let canRun
!bet ? canRun = true : canRun = await canRunGame(foundUser, bet)
if (canRun != true) {resolve(canRun); return}
let myMessage = await message.channel.send(`${foundUser} ${acceptMessage}`)
await myMessage.react("✅")
await myMessage.react("❌")
let rc = new discord.ReactionCollector(myMessage, {filter: (r, u) => (["✅", "❌"].includes(r.emoji.name) && u.id == foundUser.id), time: 60000 })
rc.on("collect", (r) => {
switch (r.emoji.name) {
case "✅": {
rc.stop("Accepted")
resolve(foundUser)
}
case "❌": {
rc.stop("Cancelled")
myMessage.reactions.removeAll()
resolve("Cancelled")
}
}
})
rc.on("stop", (c, r) => {
if (r != "Accepted") {
resolve("Cancelled")
}
})
}
else {
if (searchMessage) {
let myMessage = await message.channel.send(searchMessage)
await myMessage.react("✅")
let rc = new discord.ReactionCollector(myMessage, {filter: (r, u) => (["✅"].includes(r.emoji.name) && !u.bot && u.id != message.author.id), time: 60000 })
rc.on("collect", async (r, u) => {
let canRun2 = await canRunGame(u, bet, true)
if (canRun2 != true) {
message.channel.send(canRun2)
}
else {
rc.stop("Accepted")
resolve(u)
}
})
rc.on("stop", (c, r) => {
if (r != "Accepted") {
resolve("Cancelled")
}
})
}
}
})
module.exports = {canRunGame, advancedSearchUser}