-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
256 lines (224 loc) · 10.5 KB
/
bot.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* eslint no-unreachable: 0 */
require('dotenv/config')
const fs = require('fs')
const express = require('express')
const Discord = require('discord.js')
const bodyParser = require('body-parser')
const Enmap = require('enmap')
// --- init stuff
const app = express()
const router = express.Router()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(process.env.PREFIX, router)
app.listen(process.env.PORT, () => {
console.log('Listening on port:', process.env.PORT)
})
const client = new Discord.Client({
presence: {
activity: {
name: 'people vote for Suggester! - v!help',
type: 'WATCHING'
},
status: 'online'
}
})
if (!client.stats) {
client.stats = new Enmap({
name: 'stats'
})
}
const defaults = {
total: 0,
user: '',
topgg: [0, [], false],
botlistspace: [0, [], false],
bfd: [0, [], false],
dbl: [0, [], false],
dboats: [0, [], false],
gbl: [0, [], false],
arcane: [0, [], false],
bod: false
}
function makeDefault (current, id) {
let obj = defaults
if (current.topgg) {
obj = current
obj.user = id
} else {
obj.total = current.votes
obj.user = id
}
return obj
};
async function handleVote (user, type, valueToAdd, client, timeforday) {
await client.stats.defer
const timeforstreak = timeforday * 5
client.stats.ensure(user, defaults)
let stats = client.stats.get(user)
if (stats.id || stats.votes || !stats.user) stats = makeDefault(stats, user)
stats[type][0] += valueToAdd
stats[type][1].push(Date.now())
stats.total += valueToAdd
stats[type][2] = false
const last5 = stats[type][1].filter(time => Date.now() - time <= timeforstreak)
const emotes = ['<:streak1:690376809761734706>', '<:streak2:690377235978256384>', '<:streak3:690377163827970049>', '<:streak4:690377107817103380>', '<:streak5:690377450877747221>']
if (last5.length >= 5 && last5.filter(time => Date.now() - time > timeforday && Date.now() - time < timeforday * 2)) {
stats[type][1] = [Date.now()]
stats[type][0]++
client.stats.set(user, stats)
return `\n> Streak ${emotes[4]} - +1 Extra Vote Applied for Completed Streak! :tada:`
} else if (last5.length > 0 && last5.filter(time => Date.now() - time > timeforday && Date.now() - time < timeforday * 2)) {
client.stats.set(user, stats)
return `\n> Streak ${emotes[last5.length - 1]}`
} else {
client.stats.set(user, stats)
return null
}
}
async function fetchUser (id, client) {
function fetchUnknownUser (uid) {
return client.users.fetch(uid, true)
.then(() => {
return client.users.cache.get(uid)
})
.catch(() => {
return null
})
}
return client.users.cache.get(id) ||
fetchUnknownUser(id) ||
null
}
// --- routes ---
router.get('/', (request, response) => {
response.sendStatus(200)
})
router.get('/ping', (req, res) => {
res.status(200).send('OK')
})
router.post('/topgg', async (req, res) => {
if (!req.headers.authorization || req.headers.authorization !== process.env.TOPGG_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.user, 'topgg', req.body.isWeekend ? 2 : 1, client, 43200000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.user, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [top.gg](https://top.gg/bot/564426594144354315/vote)!\n> ${req.body.isWeekend ? '+2 Votes (Weekend Bonus)' : '+1 Vote'}${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2Fa_eadf00405ab375668c76da10ee95f646.png?v=1584652104384' })
return res.sendStatus(200)
})
router.post('/botlistspace', async (req, res) => {
if (!req.headers.authorization || req.headers.authorization !== process.env.BLS_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.user.id, 'botlistspace', 1, client, 86400000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.user.id, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [botlist.space](https://botlist.space/bot/564426594144354315/upvote)!\n> +1 Vote${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2F70ec291cf1dcaa33f8c50d5b3333a521.png?v=1584671121819' })
return res.sendStatus(200)
})
router.post('/bfd', async (req, res) => {
if (!req.headers.authorization || req.headers.authorization !== process.env.BFD_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.user, 'bfd', 1, client, 86400000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.user, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [Bots for Discord](https://botsfordiscord.com/bot/564426594144354315/vote)!\n> +1 Vote${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2Fe1e1eb9ed8c0c3c67dd62e282bc5a704.png?v=1584714973147' })
return res.sendStatus(200)
})
router.post('/dbl', async (req, res) => {
if (!req.headers['x-dbl-signature'] || req.headers['x-dbl-signature'].split(' ')[0] !== process.env.DBL_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.id, 'dbl', 1, client, 86400000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.id, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [Discord Bot List](https://discordbotlist.com/bots/564426594144354315/upvote)!\n> +1 Vote${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2F278624a3684793369f3055ba26f7292e.png?v=1584717271727' })
return res.sendStatus(200)
})
router.post('/dboats', async (req, res) => {
if (!req.headers.authorization || req.headers.authorization !== process.env.DBOATS_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.user.id, 'dboats', 1, client, 43200000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.user.id, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [Discord Boats](https://discord.boats/bot/564426594144354315/vote)!\n> +1 Vote${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2Fa_50718fbcb8030a76b775540db66bd0b8.png?v=1584717712520' })
return res.sendStatus(200)
})
router.post('/gbl', async (req, res) => {
return
if (!req.headers.authorization || req.headers.authorization !== process.env.GBL_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.id, 'gbl', 1, client, 43200000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.id, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [Glenn Bot List](https://glennbotlist.xyz/bot/suggester/vote)!\n> +1 Vote${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2Fa_361d2318b5a519ab805c9ca089ddb56c.png?v=1584718647042' })
return res.sendStatus(200)
})
router.post('/arcane', async (req, res) => {
if (!req.headers.authorization || req.headers.authorization !== process.env.ARCANE_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.user.id, 'arcane', 1, client, 43200000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.user.id, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [Arcane Center](https://arcane-center.xyz/bot/564426594144354315)!\n> +1 Vote${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2F48ff7211df98c58c2f16b73a758a9be7.png?v=1591277063736' })
return res.sendStatus(200)
})
router.post('/spacebl', async (req, res) => {
return
if (!req.headers.authorization || req.headers.authorization !== process.env.ARCANE_PWD) return res.sendStatus(403)
const voteResponse = await handleVote(req.body.user.id, 'arcane', 1, client, 43200000)
const hook = new Discord.WebhookClient(process.env.WEBHOOK_ID, process.env.WEBHOOK_TOKEN)
const user = await fetchUser(req.body.user.id, client)
if (!user) return res.sendStatus(200)
const embed = new Discord.MessageEmbed()
.setDescription(`${user.tag} (\`${user.id}\`) voted for Suggester on [Arcane Center](https://arcane-center.xyz/bot/564426594144354315)!\n> +1 Vote${voteResponse || ''}`)
.setFooter('Thanks for voting!')
.setColor('#4663ec')
hook.send({ embeds: [embed], avatarURL: 'https://cdn.glitch.com/e10a63e8-6b5d-4d37-a694-5dfd1332828c%2F48ff7211df98c58c2f16b73a758a9be7.png?v=1591277063736' })
return res.sendStatus(200)
})
// --- discord.js stuff ---
client.login(process.env.TOKEN)
.catch(console.error)
fs.readdir('./events/', (err, files) => {
if (err) {
console.error(err)
}
files.forEach(file => {
const eventHandler = require(`./events/${file}`)
const eventName = file.split('.')[0]
client.on(eventName, (...args) => {
try {
eventHandler(Discord, client, ...args)
} catch (e) {
console.log(e)
}
})
})
})