-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (120 loc) · 4.11 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
// 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);
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 mongopw = process.env.mongopw;
// 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 msgCommands = [];
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.limit = function getLimit() {
return limiter;
}
exports.queue = function getQueue() {
return queue;
}
exports.fetch = async function limitFetch(text) {
let data;
while(1) {
await limiter.removePoints(1).then(data = await fetch(text).then(response => response.json()));
if(data.status != 420) {
return data;
}
await sleep(2000);
}
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const uri =
`mongodb+srv://penguin9541:${mongopw}@cluster0.hyvwb.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
const dbclient = new MongoClient(uri);
exports.db = dbclient;