-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (142 loc) · 5.08 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
// Imports
const { Client, Intents, Collection } = require("discord.js");
const fs = require("fs");
const Limit = require("./Limiter.js");
const Queue = require("queue-promise");
const limiter = new Limit(95, 70000);
const mojangLimiter = new Limit(600, 600000);
const hypixelLimiter = new Limit(120, 60000);
//const { REST } = require("@discordjs/rest");
//const { Routes } = require("discord-api-types/v9");
const fetch = (...args) => import("node-fetch").then(({default: fetch}) => fetch(...args));
const { MongoClient } = require("mongodb");
var cron = require("node-cron");
require("dotenv").config();
// Creates a rate limiting queue
const queue = new Queue({
concurrent: 1
});
// Prefix to call the bot
const prefix = "src!";
// Determines the token for bot
let token = process.env.token;
let hypixel = process.env.hypixel;
let mongourl = process.env.mongourl;
let src = process.env.srcapi;
// Creates new Client
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.commands = new Collection();
client.msgCommands = new Collection();
const commands = [];
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}
const msgCommandFiles = fs.readdirSync("./messagecommands").filter(file => file.endsWith(".js"));
for (const file of msgCommandFiles) {
const command = require(`./messagecommands/${file}`);
client.msgCommands.set(command.data.name, command);
}
/**
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(process.env.id),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
*/
const scheduledCommandFiles = fs.readdirSync("./scheduledcommands").filter(file => file.endsWith(".js"));
for (const file of scheduledCommandFiles) {
const command = require(`./scheduledcommands/${file}`);
cron.schedule(command.data.interval, () => {
command.execute(client);
});
}
// Sets bot activity and announces that bot is ready for use
client.once("ready", async () => {
client.user.setActivity("speedrun.com | /help", { type: "WATCHING" });
console.log("Ready!");
});
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.isCommand()) {
if (!client.commands.has(interaction.commandName)) return;
await interaction.deferReply();
try {
await client.commands.get(interaction.commandName).execute(interaction);
} 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.commands.get("hypixel").execute(interaction);
}
});
client.login(token);
exports.tokens = token;
exports.hypixel = hypixel;
exports.src = src;
exports.limit = function getLimit() {
return limiter;
};
exports.queue = function getQueue() {
return queue;
};
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 uri = mongourl;
const dbclient = new MongoClient(uri);
exports.db = dbclient;