-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
225 lines (191 loc) · 7.26 KB
/
index.js
File metadata and controls
225 lines (191 loc) · 7.26 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const { Client, Collection, GatewayIntentBits, Partials, ChannelType } = require('discord.js');
const config = require('./config.json');
const fs = require("fs");
const lol = require("./util/lol_functions.js");
const index2 = require('./index2.js');
const logger = require('./util/logger');
const Redis = require('ioredis');
const dns = require('node:dns');
dns.setDefaultResultOrder('ipv4first');
logger.log("Starting...");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction
]
});
// -------------- LOL -----------------
client.lol = lol;
client.lol.setup(client);
// -------------- Redis -----------------
const redisSubClient = new Redis(process.env.REDIS_URL || 'redis://redis:6379', {
maxRetriesPerRequest: null
});
redisSubClient.on('connect', () => logger.log('Redis Subscriber connected'));
redisSubClient.on('error', err => logger.error(err, 'Redis Subscriber error'));
redisSubClient.subscribe('updater:events', (err, count) => {
if (err) logger.error(err, 'Failed to subscribe to updater:events');
else logger.log('Subscribed to updater:events');
});
redisSubClient.subscribe('infra:commands', (err, count) => {
if (err) logger.error(err, 'Failed to subscribe to infra:commands');
else logger.log('Subscribed to infra:commands');
});
redisSubClient.on('message', async (channel, message) => {
const data = JSON.parse(message);
if (channel === 'updater:events') {
switch (data.type) {
case 'RANK_UPDATE':
client.lol.lol_rank_manager.send_tracker_message(
data.puuid,
data.old_rank,
data.new_rank,
data.last_game
);
break;
case 'SUMMONER_ADDED':
client.lol.lol_rank_manager.feedback_summoner_added(
data.channel_id,
data.message_id,
data.message,
);
break;
}
return;
}
if (channel === 'infra:commands') {
if (data.type === 'RESTART_BOT') {
logger.warn('Received RESTART_BOT command, exiting process for container restart');
if (client.redisPubClient) {
await client.redisPubClient.set('bot:stats:connected', 'false');
}
setTimeout(() => process.exit(0), 500);
}
}
});
client.redisSubClient = redisSubClient;
const redisPubClient = new Redis(process.env.REDIS_URL || 'redis://redis:6379', {
maxRetriesPerRequest: null
});
redisPubClient.on('connect', () => logger.log('Redis Publisher connected'));
redisPubClient.on('error', err => logger.error(err, 'Redis Publisher error'));
client.redisPubClient = redisPubClient;
// -------------- Commandes -----------------
client.commands = new Collection();
client.context_menu = new Collection();
client.buttons = new Collection();
client.modals = new Collection();
client.groups = new Collection();
client.timers = new Collection();
client.listeners = new Collection();
client.amonglegends = new Collection();
client.owners = config["owner"];
const ListenerFiles = fs.readdirSync('./listeners').filter(file => file.endsWith('.js'));
for (const file of ListenerFiles) {
const listener = require(`./listeners/${file}`);
client.listeners.set(listener.name, listener);
}
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
const contextFiles = fs.readdirSync('./context-menu').filter(file => file.endsWith('.js'));
for (const file of contextFiles) {
const context = require(`./context-menu/${file}`);
client.context_menu.set(context.name, context);
}
const buttonFiles = fs.readdirSync('./buttons').filter(file => file.endsWith('.js'));
for (const file of buttonFiles) {
const button = require(`./buttons/${file}`);
client.buttons.set(button.name, button);
}
const modalsFiles = fs.readdirSync('./modals').filter(file => file.endsWith('.js'));
for (const file of modalsFiles) {
const modal = require(`./modals/${file}`);
client.modals.set(modal.name, modal);
}
const TimersFiles = fs.readdirSync('./timers').filter(file => file.endsWith('.js'));
for (const file of TimersFiles) {
const timer = require(`./timers/${file}`);
client.timers.set(timer.name, timer);
}
client.commands.forEach((item) => {
if (!client.groups.has(item.group)) {
const a = {
name: item.group,
commands: new Collection()
};
client.groups.set(item.group, a);
}
client.groups.get(item.group).commands.set(item.name, item);
});
client.isOwner = function (user) {
return client.owners.includes(user.id);
};
// -------------- Utils -----------------
if (!String.prototype.format) {
String.prototype.format = function () {
const args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] !== 'undefined'
? args[number]
: match;
});
};
}
// -------------- Events -----------------
client.listeners.forEach((item) => {
client.on(item.type, async (args1, args2, args3, args4) => {
item.run(client, args1, args2, args3, args4);
});
});
// -------------- Functions -----------------
/**
* Check if the command can be run
* @function canRunCommande
* @param {*} message message send by the user
* @param {*} commande commande to run
* @param {*} interaction interaction send by the user
* @return {Boolean} return true if the command can be run
*/
client.canRunCommande = function (message, commande, interaction = undefined) {
if (interaction === undefined) {
//if(commande.commande_channel === true && !message.channel.name.toLowerCase().includes("commande")) return false
if (!checkpermission(message, commande.owner, commande.permission)) { return "perm"; }
if (commande.place === "dm" && message.channel.type !== ChannelType.DM) { return false; }
if (commande.place === "guild" && message.channel.type !== ChannelType.GuildText) { return false; }
return true;
}
//if(commande.commande_channel === true && !interaction.channel.name.toLowerCase().includes("commande")) return false;
if (!checkpermission(interaction, commande.owner, commande.permission)) { return "perm"; }
return true;
};
/**
* Check if the user has the given permission
* @function checkpermission
* @param {*} message message send by the user
* @param {*} perm permission to check
* @returns {Boolean} return true if the user has the permission
*/
function checkpermission(message, owner, perm) {
const id = message.author !== undefined ? message.author.id : message.user.id;
if (client.owners.includes(id)) {
return true;
}
if (owner === true) {
return false;
}
if (perm === undefined) {
return true;
}
return message?.member?.permissions?.has(perm) ?? false;
}
// -------------- Index2 -----------------
index2.main(client);
// -------------- Login -----------------
client.login(process.env.DISCORD_TOKEN);