forked from tedztar/mcstatusbot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.js
executable file
·88 lines (74 loc) · 2.85 KB
/
bot.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
'use strict';
import 'dotenv/config';
import { ClusterClient, getInfo } from 'discord-hybrid-sharding';
import { ActivityType, Client, Collection, GatewayIntentBits } from 'discord.js';
import mongoose from 'mongoose';
import { readdirSync } from 'node:fs';
import path, { basename } from 'node:path';
import { pathToFileURL } from 'node:url';
import { beaver } from './functions/consoleLogging.js';
import { updateServers } from './functions/updateServers.js';
let clientOptions = {
shards: getInfo().SHARD_LIST,
shardCount: getInfo().TOTAL_SHARDS,
intents: [GatewayIntentBits.Guilds],
presence: { activities: [{ name: '/help', type: ActivityType.Watching }] }
};
if (process.env.NODE_ENV == 'production') {
clientOptions.rest = { api: `${process.env.PROXY_URL}/api`, globalRequestsPerSecond: Infinity, timeout: 6 * 60 * 1000, retries: 1 };
}
export let client = new Client(clientOptions);
client.cluster = new ClusterClient(client);
client.cooldowns = new Collection();
client.commands = new Collection();
let clientReady = false;
let clusterReady = false;
client.cluster.once('ready', async () => {
clusterReady = true;
if (clientReady) init();
});
client.once('ready', async () => {
clientReady = true;
if (clusterReady) init();
});
client.on('error', (msg) => beaver.log('client', msg));
// Finally, login
client.login(process.env.TOKEN);
async function init() {
// Database Handler
mongoose.set('strictQuery', true);
try {
await mongoose.connect(process.env.DATABASE_URL, { dbName: process.env.DATABASE_NAME });
} catch (error) {
beaver.log('database', error);
return;
}
// Command Handler
const commandsPath = path.resolve(process.cwd(), './commands');
const commandFiles = readdirSync(commandsPath).filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.resolve(commandsPath, file);
const command = await import(pathToFileURL(filePath).toString());
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
beaver.log('command-registration', `Error registering /${basename(file, '.js')} command: missing a required "data" or "execute" property.`);
}
}
// Event Handler
const eventsPath = path.resolve(process.cwd(), './events');
const eventFiles = readdirSync(eventsPath).filter((file) => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.resolve(eventsPath, file);
const event = await import(pathToFileURL(filePath).toString());
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
// Update Servers
if (process.env.NODE_ENV != 'production') await updateServers(client);
// Delay the update based on cluster id
setTimeout(() => setInterval(updateServers, 6 * 60 * 1000, client), client.cluster.id * 7 * 1000);
}