-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
300 lines (253 loc) · 9.73 KB
/
main.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const { exec } = require('child_process');
const config_manager = require("./config.json")
const config = config_manager[config_manager.using]
const is_test = config.is_test ? config.is_test : false
const rsc_config = config.register_slash_commands
require('dotenv').config();
let token;
if (is_test) {
token = process.env.TOKEN_TEST;
} else {
token = process.env.TOKEN;
}
if (is_test) {
const pathToExe = process.cwd() + '/progress_display/code_counter.exe';
const directoryPath = 'C:\\Users\\oem\\OneDrive\\Dokumenty\\GitHub\\TsunamiBot';
exec(`${pathToExe} "${directoryPath}"`, (error, stdout, stderr) => {
if (error) {
console.error(`Błąd podczas wykonywania pliku .exe: ${error.message}`);
return;
}
if (stderr) {
console.error(`Błąd podczas wykonywania pliku .exe: ${stderr}`);
return;
}
console.log(stdout); // Wyświetl wynik w konsoli
});
}
const ConsoleLogger = require("./handlers/console")
const logger = ConsoleLogger.getInstance();
console.log({
"Using configuration": config_manager.using,
"Test mode: ": is_test,
})
const { Client, GatewayIntentBits, Events, Partials } = require("discord.js")
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildMessageReactions
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction
],
});
const LoadModLogsGuilds = require("./handlers/modlogsMessages_handler")
LoadModLogsGuilds.getInstance().LoadGuilds()
const { register_slash_commands, unregisterAllCommands } = require("./handlers/SlashCommandHandler")
const { commandsMap } = require("./handlers/commandsMap")
const welcome_messages = require("./handlers/welcome")
const autorole = require("./handlers/autorole")
const log_messages = require("./handlers/logMessages")
const { lvl_system } = require("./handlers/lvlHandler")
const status_handler = require("./handlers/botStatus")
const api = require("./api/api")
const mod_logs = require("./handlers/mod_logs_handler")
const Database = require("./db/database")
const database = new Database(__dirname + "/db/files/servers.json")
const dad_handler = require("./handlers/dad_handler");
const { messages_stats_handler } = require("./handlers/stats_handler")
const { registerSlashCommandsForGuild, unregisterAllCommandsForGuild } = require("./handlers/SlashCommandHandler")
const { audio_api_run } = require("./handlers/audio/api")
const { AudioApiV2 } = require("./handlers/audio/apiV2")
const run_sdk = require("./sdk/server/server")
const manage_auto_vc = require("./handlers/auto_vc_handler")
const filter_links = require("./handlers/filter_links")
const auto_vc_commands_handler = require("./handlers/auto_vc_commands")
const auto_vc_cache = require("./handlers/auto_vc_cache")
const {handleCustomTextCommands, CustomCommands} = require("./handlers/custom_commands")
const CustomCommandsHandler = CustomCommands.getInstance();
const BotLogs = require("./handlers/bot_logs_handler")
const BotLogsHandler = BotLogs.getInstance();
const InviteTracker = require("./handlers/invite_tracker")
const { AudioDataStore } = require("./handlers/audio/cache")
const AudioStore = AudioDataStore.getInstance()
const {addEmoji, removeEmoji} = require("./handlers/emoji_handler")
const {addRatingEmoji } = require("./handlers/ticket_ratings_emoji_handler")
const {tickets_loop, load_tickets_db,} = require("./handlers/tickets_handler")
// "/test" handlers
require("./test/handlers/handler")(client)
const test_msg_handler = require("./test/handlers/msg_handler")
const auto_vc_channels = new auto_vc_cache()
CustomCommandsHandler.loadTextCommands()
BotLogsHandler.LoadGuilds()
const inviteTracker = new InviteTracker(client)
mod_logs(client);
client.on("ready", async (res) => {
logger.log(`${res.user.tag} is ready`);
status_handler(client, config)
load_tickets_db().then(tickets_loop())
database.backup(__dirname + "/db/backup")
if (!is_test) {
run_sdk()
}
api();
audio_api_run();
// AudioApiV2();
await AudioStore.load_songs(client)
// console.log(AudioStore.get())
// RSC_config - register slash commands config
if (rsc_config) {
//dodać sprawdzanie listy / commands bota na discordzie, jeżeli jest jakaś któraj nie ma w map to tylko wtedy usówać!
await unregisterAllCommands(client)
.then(await register_slash_commands(client, is_test))
.then(logger.log("All commands registered successfully on all guilds."))
} else {
console.error("RSC disabled")
}
});
client.on("guildCreate", async (guild) => {
if(!guild) return
registerSlashCommandsForGuild(guild, client);
})
//execute
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const commandName = interaction.commandName;
const commandLocation = commandsMap.get(commandName);
if (commandLocation) {
const { _, execute } = require(commandLocation);
try {
await execute(interaction, client);
} catch (error) {
logger.error(error);
await interaction.channel.send({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
}
});
//autocomplete
client.on('interactionCreate', async interaction => {
if (interaction.isAutocomplete()) {
const commandName = interaction.commandName
if (!commandName) return;
if (interaction.responded) return;
const commandLocation = commandsMap.get(commandName);
if (commandLocation) {
const { data, autocomplete } = require(commandLocation);
try {
await autocomplete(interaction, client);
} catch (error) {
logger.error(error);
console.log(error)
await interaction.channel.send({
content: "There was an error while executing autocomplete in this command!",
ephemeral: true,
});
}
}
}
});
// emoji's
client.on(Events.MessageReactionAdd, async (reaction, user) => {
addEmoji(client, reaction, user)
addRatingEmoji(client, reaction, user)
});
client.on(Events.MessageReactionRemove, async (reaction, user) => {
removeEmoji(client, reaction, user)
});
// Dodanie event handlera do przycisków
// client.on("interactionCreate", async (interaction) => {
// if (interaction.isButton()) {
// console.log(interaction)
// try {
// await handleButtonInteraction(interaction, data);
// } catch (error) {
// console.error(error);
// await interaction.reply({ content: "There was an error with the button interaction.", ephemeral: true });
// }
// }
// });
client.on('guildMemberAdd', async member => {
welcome_messages(member, client)
autorole(member, client)
inviteTracker.userJoin(member, client)
});
client.on("messageCreate", async message => {
if (message.author.bot) return;
log_messages(message)
lvl_system(message)
dad_handler(client, message)
messages_stats_handler(message)
test_msg_handler(client, message)
filter_links(client, message)
auto_vc_commands_handler(message, auto_vc_channels)
handleCustomTextCommands(message)
if (message.author.id === "438336824516149249" && !message.author.bot && message.content.startsWith("reload")) {
const args = message.content.trim().split(/ +/);
if (args[0] === "reload") {
const guild = message.guild
await message.reply("commands are being refreshed. This may take a few minutes");
unregisterAllCommandsForGuild(guild, client)
.then(
registerSlashCommandsForGuild(guild, client)
.then(
await message.channel.send("All command refreshed succesfully.")
)
)
}
}
})
client.on('voiceStateUpdate', async (oldState, newState) => {
manage_auto_vc(client, oldState, newState, auto_vc_channels)
})
async function restartBot() {
try {
console.log('Restarting bot...');
// Disconect bot from Discord
await client.destroy();
// Connect bota to Discord
await client.login(token);
console.log('Bot are restarted succesfully');
} catch (error) {
console.error('Wystąpił błąd podczas restartowania bota:', error);
}
}
// Definicja funkcji wyłączającej bota
async function bot_off() {
try {
console.log('Turning bot off');
// Rozłączanie bota z Discord
await client.destroy();
console.log('Bot are succesfully turned off');
} catch (error) {
console.error('Wystąpił błąd podczas wyłączania bota:', error);
}
}
// Definicja funkcji włączającej bota
async function bot_on() {
try {
console.log('Turning bot on');
// Ponowne połączenie bota z Discord
await client.login(token);
console.log('Bot are succesfully turned on');
} catch (error) {
console.error('Wystąpił błąd podczas włączania bota:', error);
}
}
client.on("uncaughtException", (e) => {
logger.warn(e)
//TODO dodać funkcję zapisującą do db dane z crasha, dane bota
// i inne pierdoły, wysyłać komunikat na webhooka że bot jest down
});
client.login(token)
module.exports = { client, config, restartBot, bot_off, bot_on }