-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (176 loc) · 6.33 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
// Imports
const { Client, GatewayIntentBits, Collection, InteractionType, Routes, ActivityType } = require("discord.js");
const fs = require("fs");
const Limit = require("./Limiter.js");
const { REST } = require("@discordjs/rest");
const fetch = (...args) => import("node-fetch").then(({default: fetch}) => fetch(...args));
const { MongoClient } = require("mongodb");
var cron = require("node-cron");
require("dotenv").config();
// Prefix to call the bot
const prefix = "src!";
let token = process.env.token;
let hypixel = process.env.hypixel;
let mongourl = process.env.mongourl;
let src = process.env.srcapi;
const limiter = new Limit(95, 70 * 1000);
const mojangLimiter = new Limit(600, 600 * 1000);
const hypixelLimiter = new Limit(120, 60 * 1000);
// Creates new Client
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.commands = new Collection();
client.msgCommands = new Collection();
client.guildCommands = new Collection();
client.buttonCommands = new Collection();
client.selectCommands = new Collection();
getCommands("./CommandInteractions", (command) => {
client.commands.set(command.data.name, command);
});
getCommands("./GuildCommandInteractions", (command) => {
client.guildCommands.set(command.data.name, command);
});
getCommands("./ButtonInteractions", (command) => {
client.buttonCommands.set(command.data, command);
});
getCommands("./SelectMenuInteractions", (command) => {
client.selectCommands.set(command.data, command);
});
getCommands("./MessageCommands", (command) => {
client.msgCommands.set(command.data.name, command);
});
getCommands("./ScheduledCommands", (command) => {
cron.schedule(command.data.interval, () => {
command.execute(client);
});
});
function getCommands(dir, callback) {
const files = fs.readdirSync(dir).filter(file => file.endsWith(".js"));
for (const file of files) {
const command = require(`${dir}/${file}`);
callback(command);
}
}
const rest = new REST({ version: "10" }).setToken(token);
// Sets bot activity and announces that bot is ready for use
client.once("ready", async () => {
client.user.setActivity("speedrun.com | /help", { type: ActivityType.Watching });
await dbclient.connect();
try {
console.log("Started refreshing application (/) commands.");
await rest.put(
Routes.applicationGuildCommands(process.env.id, process.env.guildid),
{ body: client.guildCommands.map(command => command.data.toJSON()) },
);
const commandValues = new Set((await client.application.commands.fetch()).map(command => command.name));
const commands = new Set(client.commands.map(command => command.data.name));
if(commandValues.size !== commands.size || ![...commandValues].every(command => commands.has(command))) {
await rest.put(
Routes.applicationCommands(process.env.id),
{ body: client.commands.map(command => command.data.toJSON()) },
);
console.log("Successfully reloaded application (/) commands.");
return;
}
console.log("No (/) commands to reload.");
} catch (error) {
console.error(error);
}
});
client.on("messageCreate", async message => {
// Checks if message starts with the given prefix
if (!message.content.toLowerCase().startsWith(prefix) || message.author.bot) return;
console.log(message.content);
const command = message.content.toLowerCase().slice(4).split(" ");
if (!client.msgCommands.has(command[0])) return;
try {
await client.msgCommands.get(command[0]).execute(command, message);
} catch (error) {
console.error(error);
}
});
client.on("interactionCreate", async interaction => {
if(interaction.type === InteractionType.ApplicationCommand) {
if (!client.commands.has(interaction.commandName) && !client.guildCommands.has(interaction.commandName)) return;
await interaction.deferReply();
try {
if(client.commands.has(interaction.commandName)) {
await client.commands.get(interaction.commandName).execute({
interaction,
client
});
} else {
await client.guildCommands.get(interaction.commandName).execute({
interaction,
client
});
}
} catch (error) {
console.error(error);
await interaction.editReply({ content: "There was an error while executing this command!", ephemeral: true });
}
} else if(interaction.isSelectMenu()) {
if(!client.selectCommands.has(interaction.customId)) return;
await client.selectCommands.get(interaction.customId).execute({
interaction,
client
});
} else if(interaction.isButton()) {
const customId = JSON.parse(interaction.customId).customId;
if(!client.buttonCommands.has(customId)) return;
await client.buttonCommands.get(customId).execute({
interaction,
client
});
}
});
client.login(token);
exports.tokens = token;
exports.hypixel = hypixel;
exports.src = src;
exports.limit = function getLimit() {
return limiter;
};
exports.fetch = async function limitFetch(text) {
let attemptCount = 0;
while(attemptCount++ < 20) {
await limiter.removePoints(1);
const response = await fetch(text);
if(response.status != 420) {
return await response.json();
}
await sleep(2000);
}
};
exports.post = async function limitPost(text, params) {
let attemptCount = 0;
while(attemptCount++ < 20) {
await limiter.removePoints(1);
const response = await fetch(text, params);
if(response.status != 420) {
return await response.json();
}
await sleep(2000);
}
};
exports.fetchMojang = async function limitMojangFetch(text) {
await mojangLimiter.removePoints(1);
const response = await fetch(text);
try {
return await response.json();
} catch (error) {
// Returns an empty response if the player doesn't exist
return;
}
};
exports.fetchHypixel = async function limitHypixelFetch(text) {
await hypixelLimiter.removePoints(1);
const response = await fetch(text);
return await response.json();
};
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const dbclient = new MongoClient(mongourl);
exports.db = dbclient;