-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (150 loc) · 5.85 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
// 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();
getCommands("./commands", (command) => {
client.commands.set(command.data.name, command);
});
getCommands("./messagecommands", (command) => {
client.msgCommands.set(command.data.name, command);
});
getCommands("./guildcommands", (command) => {
client.guildCommands.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 });
console.log("Ready!");
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()) {
await client.guildCommands.get("hypixel").execute(interaction);
}
});
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 data;
while(true) {
await limiter.removePoints(1).then(data = await fetch(text).then(response => response.json()));
if(data.status != 420) {
return data;
}
await sleep(2000);
}
};
exports.post = async function limitPost(text, params) {
let data;
while(true) {
await limiter.removePoints(1).then(data = await fetch(text, params).then(response => response.json()));
if(data.status != 420) {
return data;
}
await sleep(2000);
}
};
exports.fetchMojang = async function limitMojangFetch(text) {
let data;
await mojangLimiter.removePoints(1).then(data = await fetch(text).then(response => response.json()).catch(() => ""));
return data;
};
exports.fetchHypixel = async function limitHypixelFetch(text) {
let data;
await hypixelLimiter.removePoints(1).then(data = await fetch(text).then(response => response.json()));
return data;
};
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const dbclient = new MongoClient(mongourl);
exports.db = dbclient;