-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
172 lines (154 loc) · 6.35 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
require('dotenv').config();
const Discord = require('discord.js');
const Database = require('./database/Database');
const Server = require('./items/Server');
const ServerManager = require('./managers/ServerManager');
const fs = require('fs');
const { Logger } = require('./utils/Utils');
const formatDiscordRegion = require('./utils/formatDiscordRegion');
const express = require('express');
const { scheduleJob, RecurrenceRule } = require('node-schedule');
const client = new Discord.Client({
partials: ["MESSAGE", "REACTION", "GUILD_MEMBER", "CHANNEL", "USER"],
ws: {
intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MEMBERS', 'DIRECT_MESSAGES' ,'GUILD_MESSAGE_REACTIONS', 'GUILD_PRESENCES', 'DIRECT_MESSAGE_REACTIONS', 'GUILD_BANS']
}
});
const client2 = new Discord.Client({
partials: ["MESSAGE", "REACTION", "GUILD_MEMBER", "CHANNEL", "USER"],
ws: {
intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MEMBERS', 'DIRECT_MESSAGES' ,'GUILD_MESSAGE_REACTIONS', 'GUILD_PRESENCES', 'DIRECT_MESSAGE_REACTIONS', 'GUILD_BANS']
}
});
require('discord-buttons')(client, client2);
client.client2 = client2;
const dateNow = new Date();
const now = `./logs/${dateNow.getTime()}.log`.replace(':', '.');
const log = console.log;
console.log = function(object) {
fs.appendFile('./logs/latest.log', `${object}\n`, 'utf-8', () => {
log(object);
});
return object;
}
client.setMaxListeners(15);
client.client2.setMaxListeners(15);
/**
* @type {express.Application}
*/
client.app = express();
client.app.use(express.json());
client.manager = new ServerManager(client);
client.app.listen(process.env.PORT);
client.client2.app = client.app;
client.client2.manager = client.manager;
var client1_loaded = false;
var client2_loaded = false;
client.once('ready', () => {
try {
const loadServer = new Promise((resolve, reject) => {
client.guilds.cache.forEach((guild, id) => {
if (client.guilds.cache.size === 0)
resolve();
const server = client.manager.servers.get(guild.id);
if (!server) {
const server = new Server(client, guild, undefined, '-', 0, client.manager);
client.manager.servers.set(server.id, server);
}
if (id === client.guilds.cache.last().id)
resolve();
});
});
loadServer.then(async () => {
client1_loaded = true;
setup();
});
} catch (err) {
client.manager.debug(err.message);
}
});
client.client2.once('ready', () => {
try {
const loadServer = new Promise((resolve, reject) => {
if (client.client2.guilds.cache.size === 0)
resolve();
client.client2.guilds.cache.forEach((guild, id) => {
const server = client.manager.servers.get(guild.id);
if (!server) {
const server = new Server(client.client2, guild, undefined, '-', 0, client.manager);
client.manager.servers.set(server.id, server);
}
if (id === client.client2.guilds.cache.last().id)
resolve();
});
});
loadServer.then(() => {
client2_loaded = true;
setup();
});
} catch(err) {
client.manager.debug(err.message);
}
});
const setup = () => {
return new Promise(async (resolve, reject) => {
if (client1_loaded && client2_loaded) {
const rows = await Database.allNewDB('SELECT * FROM servers')
for (const row of rows) {
try {
client.manager.all.push(row.id);
var guild = await client.guilds.fetch(row.id);
if (!guild) {
guild = await client.client2.guilds.fetch(row.id);
}
if (guild) {
const server = await client.manager.fetch(guild.id)
server.loadData(JSON.parse(row.data));
}
} catch(err) {}
}
client.user.setActivity(`destial.xyz | ${client.manager.all.length} leagues`);
Logger.log('danktial is now online!');
client.client2.user.setActivity(`destial.xyz | ${client.manager.all.length} leagues`);
Logger.log('danktial2 is now online!');
client.manager.servers.forEach(server => {
if (server.modlog) {
const locale = formatDiscordRegion(server.guild.region);
const d = new Date();
const date = d.toLocaleDateString('en-US', { timeZone: locale, weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' });
const time = d.toLocaleTimeString('en-US', { timeZone: locale, hour12: true, hour: '2-digit', minute: '2-digit' }).replace(' ', '').toLowerCase();
try {
server.modlog.setTopic(`danktial has been online since ${date} ${(time.startsWith('0') ? time.substring(1) : time)} ${server.guild.region.toLocaleUpperCase()}`);
} catch(err) {}
}
});
const rule = new RecurrenceRule();
rule.minute = 30;
scheduleJob(rule, () => {
client.user.setActivity(`destial.xyz | ${client.manager.all.length} leagues`);
client.client2.user.setActivity(`destial.xyz | ${client.manager.all.length} leagues`);
});
resolve();
}
});
}
const listenerFiles = fs.readdirSync('./listeners').filter(file => file.endsWith('.js'));
for (const file of listenerFiles) {
const listener = require(`./listeners/${file}`);
listener.run(client, client.manager);
listener.run(client.client2, client.manager);
Logger.boot(`[LISTENER] Registered ${file.replace('.js', '')} for both clients`);
}
client.login(process.env.DISCORD_TOKEN);
client.client2.login(process.env.DISCORD_TOKEN2);
process.on('uncaughtException', (err) => {
console.log(err);
client.manager.debug(err ? err.message : `Uncaught exception! ${err}`);
});
process.on('unhandledRejection', (err) => {
console.log(err);
client.manager.debug(`Uncaught rejection! ${err}`);
});
process.on('beforeExit', (code) => {
fs.renameSync('./logs/latest.log', now);
});