-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
262 lines (247 loc) · 6.74 KB
/
index.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
257
258
259
260
261
262
// Imports
const { Client, MessageEmbed} = require('discord.js');
const commands = require('./commands');
const api = require('./api');
const fs = require('fs');
const cron = require("node-cron");
const Limit = require('./Limiter.js');
const Queue = require('queue-promise');
const limiter = new Limit(95);
const queue = new Queue({
concurrent: 1
});
// Prefix to call the bot
const prefix = 'src!';
// Determines the token for bot
let token = '';
if (fs.existsSync('./token.json')) {
const tokenFile = require('./token.json');
token = tokenFile.token;
} else {
token = process.env.token;
}
// Creates new Client
const client = new Client();
// Sets bot activity and announces that bot is ready for use
client.once('ready', () => {
client.user.setActivity('speedrun.com | src!help', { type: 'WATCHING' })
console.log('Ready!');
});
client.on('message', async message => {
// Checks if message starts with the given prefix
if (!message.content.toLowerCase().startsWith(prefix) || message.author.bot) return;
console.log(message.content);
// Removes prefix from the message
const args = message.content.slice(prefix.length).split(/ +/);
// Sets the command to the next text after the prefix
const command = args.shift().toLowerCase();
queue.enqueue(async () => {
try {
switch(command) {
case 'help':
await commands.Help.help(message);
break;
case 'link':
await commands.Link.link(message, args);
break;
case 'c':
case 'categories':
await commands.Categories.categories(message, args);
break;
case 's':
case 'search':
await commands.Search.search(message, args);
break;
/**case 'wr':
await commands.Wr.wr(message, args);
break;
case 'time':
await commands.Time.time(message, args);
break;
case 'lb':
await commands.Leaderboard.lb(message, args, 'Message');
break;*/
case 'verified':
case 'v':
await commands.Verified.verified(message, args);
break;
case 'unverified':
case 'uv':
await commands.Unverified.unverified(message, args);
break;
case 'dream':
await commands.Dream.dream(message, args);
break;
case 'lb':
await commands.NewLeaderboard.newlb(message, args, 'Message');
break;
case 'ping':
await commands.Ping.ping(client, message);
break;
case 'randomcommand':
if(message.author.id == '168420049462362112') {
grouplb(client.channels.cache.get(args[0]), args.slice(1));
} else {
message.channel.send('No permission.');
}
break;
}
} catch(err) {
message.channel.send("An unexpected error occurred.");
console.log(message.content);
console.log(err);
}
});
});
client.login(token).then(() => {
// Schedules the automatic daily leaderboards (Time in GMT)
cron.schedule("15 0 5 * * *", async function() {
const daily = [
'hypixel_sb',
'hypixel_sbce'
];
const daily2 = [
'hypixel_ce',
'hypixel_bw',
'hypixel_sw',
'hypixel_ag',
'hypixel_cg',
'tkr',
'mcm_za',
'mcm_wotf',
'mcm_hr',
'mcm_sw',
'mcm_hm',
'mcm_cd'
];
await grouplb(client.channels.cache.get('792473904391651369'), daily);
await grouplb(client.channels.cache.get('782073727881183304'), daily2);
});
grouplb = async function grouplb(channel, games) {
let topPlayers = [];
let totalScores = [];
let scores;
for(game of games) {
await dailyLB(channel, game).then(function(data) {
scores = data;
});
if(topPlayers.indexOf(scores[0][0]) == -1) {
topPlayers.push(scores[0][0]);
}
k:
for(player of scores) {
for(score of totalScores) {
if(player[0] == score[0]) {
score[1] += player[1];
continue k;
}
}
totalScores.push(player);
}
await wait(60000);
}
let date = new Date().toISOString().slice(0, 10);
let embed = new MessageEmbed()
.setColor('118855')
.setTitle('Top Players for Group:')
.setFooter(date)
for(player of topPlayers) {
embed.addField(player.replace(/[*_~]/g, "\\$&"), '\u200b', true);
}
await channel.send(embed);
embed = new MessageEmbed()
.setColor('118855')
.setTitle('Top WRs for Group:')
.setFooter(date)
totalScores.sort(function(a, b) {
return b[1] - a[1];
});
let place = 1;
let iterator = 0;
let countPlayer = 0;
for(player of totalScores) {
embed.addField('#' + place + ' ' + player[0].replace(/[*_~]/g, "\\$&"), `WRs:${player[1]}`, true)
countPlayer++;
if(totalScores[iterator + 1] && totalScores[iterator + 1][1] != totalScores[iterator][1]) {
place++;
}
if(countPlayer > 30) {
break;
}
iterator++;
}
await channel.send(embed);
}
function dailyLB(channel, game) {
return new Promise((resolve) => {
queue.enqueue(async () => {
let data = await commands.NewLeaderboard.newlb(channel, game, 'Channel');
resolve(data);
});
});
}
function wait(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
});
// Returns a list of the players in a given run.
exports.players = async function players(args) {
let str = "";
for(const element of args) {
if(element.rel == "user") {
let temp = await api.Player.getPlayer(element.id);
str += ', ' + temp.data.names.international;
} else if (element.rel == "guest") {
str += ', ' + element.name;
}
}
return str.substr(2);
}
// Searches the variables in a game
exports.searchVariables = async function searchVariables(game, category, variable) {
const {data} = await api.Categories.getCategories(game);
let id = "";
let id2 = "";
let num1 = 0;
let num2 = 0;
if(!data[0]){
return["",""];
}
a:
for(item of data[0].categories.data) {
if(item.name.toLowerCase().trim() === category.toLowerCase().trim()) {
for(item2 of item.variables.data) {
for(item3 in item2.values.values) {
const check = data[0].categories.data[num1].variables.data[num2].values.values[item3].label;
if(check.toLowerCase().trim() === variable.toLowerCase().trim()) {
id = item.id;
id2 = item2.id;
break a;
}
}
num2++;
}
}
num1++;
}
let newdata = await api.Variables.getVariables(id);
if(!newdata.data) {
return ["",""];
}
for(const data1 of newdata.data) {
if(data1.id === id2) {
for(const element in data1.values.values) {
if(data1.values.values[element].label.toLowerCase().trim() === variable.toLowerCase().trim()) {
return [data1.id, element];
}
}
}
}
}
exports.tokens = token;
exports.limit = function getLimit() {
return limiter;
}
exports.queue = function getQueue() {
return queue;
}