-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
420 lines (371 loc) · 16.3 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// --define and create some things--
// packages
const Discord = require("discord.js");
const { REST } = require("@discordjs/rest");
// native
const path = require("path");
const fs = require("node:fs");
const util = require("util");
const wait = require("node:timers/promises").setTimeout;
// functions
const log = require("./functions/log.js");
const gettime = require("./functions/gettime.js");
// files
const config = require("./config.json");
const blacklist = require("./data/blacklist.json");
const package = require("./package.json");
// other
const cooldownSet = new Set();
// client
const client = new Discord.Client({
partials: [Discord.Partials.Message, Discord.Partials.User, Discord.Partials.Channel],
allowedMentions: {
parse: ["users", "roles", "everyone"],
repliedUser: false,
},
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildBans,
Discord.GatewayIntentBits.GuildEmojisAndStickers,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.DirectMessageReactions,
],
});
// SLASH-COMMANDS
function slashCommands() {
let cmds = {};
const commands = [];
try {
var commandFiles = fs.readdirSync("./scommands");
} catch (err) {
return log("[" + gettime() + "] » Error: Unable to scan directory: " + err, "red", "reset", true);
}
for (const file of commandFiles) {
if (!file.toLowerCase().endsWith(".js")) {
log(`Skipping: ${file}`, "green", "reset", true);
} else {
cmds[file] = require(`./scommands/${file}`);
const command = require(`./scommands/${file}`);
commands.push(command.data.toJSON());
log(`Loading: ${file}`, "cyan", "reset", true);
}
}
client.cmds = cmds;
const rest = new REST({ version: "10" }).setToken(config.token);
async function refresh() {
try {
log(
"Refreshing: " + commandFiles.filter((file) => file.toLowerCase().endsWith(".js")).length + " Slash-Commands\n",
"cyan",
"reset",
true
);
const data = await rest.put(Discord.Routes.applicationCommands(client.user.id), {
body: commands,
});
log(`Loaded: ${data.length} Slash-Commands`, "blue", "reset", false);
log("Start complete!\n", "red", "reset", false);
} catch (error) {
client.error(error, "main.js");
}
}
refresh();
}
// EVENTS
function events() {
let events = {};
fs.readdir(path.join(__dirname, "events"), async function (err, files) {
if (err) {
return log("[" + gettime() + "] » Error: Unable to scan directory: " + err, "red", "reset", true);
}
await files.forEach(function (file) {
if (!file.toLowerCase().endsWith(".js")) {
log(`Skipping: ${file}`, "green", "reset", true);
return;
}
const eventName = file.split(".")[0];
if (!events[eventName]) {
events[eventName] = [];
log(`Grouping: ${eventName}`, "cyan", "reset", true);
}
events[eventName].push(file);
log(`\t${file}`, "cyan", "reset", true);
});
client.events = events;
log("\nRegistering: Events", "yellow", "reset", false);
await Object.keys(events).forEach((event) => {
log("Listening: " + event, "cyan", "reset", true);
if (event == "ready") {
events[event].forEach((file) => {
require(`./events/${file}`)(client);
});
return;
}
client.on(event, async (...args) => {
try {
if (event == "messageCreate") var user = args[0].author;
else if (event == "interactionCreate") var user = args[0].user;
else if (event == "voiceStateUpdate") var user = args[0].member.user;
else if (event.startsWith("guildMember")) var user = args[0].user;
if (user.bot) return;
// is user blacklisted?
if (await blacklist.includes(user.id)) {
} else {
const maintenance = await JSON.parse(fs.readFileSync("./data/maintenance.json", "utf8"));
if (maintenance.maintenance == true && user.id != config.owner) {
if (event == "interactionCreate" && !args[0].isAutocomplete() && !args[0].isChatInputCommand()) {
console.log(event, args[0], user);
args[0].reply({
content: "🛑 Der Bot ist aktuell gesperrt. \n Grund: `" + maintenance.reason + "`",
ephemeral: true,
});
}
return;
}
events[event].forEach((file) => {
const eventFile = require(`./events/${file}`);
eventFile(client, ...args);
});
}
} catch (error) {
client.error(error, "main.js");
}
});
});
log(`\nLoaded: ${files.filter((file) => file.toLowerCase().endsWith(".js")).length} Events`, "blue", "reset", false);
});
}
// start all handlers
client.on("ready", async () => {
// startup log
log(`\nLoading: ${package.name} v${package.version}\n`, "red", "reset", false);
log("Registering: Slash-Commands", "yellow", "reset", false);
await slashCommands();
log("\nGrouping: Events", "yellow", "reset", false);
await events();
});
// slash command handler
client.on("interactionCreate", async (interaction) => {
if (interaction.isChatInputCommand() || interaction.isAutocomplete()) {
if (await blacklist.includes(interaction.user.id)) {
} else {
const maintenance = await JSON.parse(fs.readFileSync("./data/maintenance.json", "utf8"));
if (maintenance.maintenance == true && interaction.user.id != config.owner) {
if (!interaction.isAutocomplete()) {
interaction.reply({
content: "🛑 Der Bot ist aktuell gesperrt. \n Grund: `" + maintenance.reason + "`",
ephemeral: true,
});
}
return;
}
// cooldown
if (interaction.user.id != config.owner && interaction.isChatInputCommand()) {
const cooldown = client.cmdStructure.cmds[interaction.commandName + ".js"].cooldown;
if (cooldown == undefined) {
// No custom cooldown, use the default
// react and remove the message
if (cooldownSet.has(interaction.user.id))
return interaction.reply({
content: `⏳ \`/${interaction.commandName}\` hat einen Cooldown von \`${config.cooldown_standard}\` Sekunden.`,
ephemeral: true,
});
// add the user to the cooldown set
cooldownSet.add(interaction.user.id);
setTimeout(() => {
cooldownSet.delete(interaction.user.id);
}, config.cooldown_standard * 1000);
} else {
// the same as above, but with a custom cooldown
// calculate the cooldown
d = Number(cooldown);
var h = Math.floor(d / 3600);
var m = Math.floor((d % 3600) / 60);
var s = Math.floor((d % 3600) % 60);
var hDisplay =
+h > 0
? +h == 1
? +m > 0
? `eine Stunde, `
: +h == 1
? `eine Stunde`
: `${h} Stunden`
: +m > 0
? `${h} Stunden, `
: `${h} Stunden`
: ``;
var mDisplay =
+m > 0
? +m == 1
? +s > 0
? `eine Minute, `
: +m == 1
? `eine Minute`
: `${m} Minuten`
: +s > 0
? `${m} Minuten, `
: `${m} Minuten`
: ``;
var sDisplay = +s > 0 ? (+s == 1 ? `eine Sekunde` : `${s} Sekunden`) : ``;
// do the magic
if (cooldownSet.has(interaction.user.id + interaction.commandName) || cooldownSet.has(interaction.user.id))
return interaction.reply({
content: `⏳ \`/${interaction.commandName}\` hat einen Cooldown von \`${hDisplay + mDisplay + sDisplay}\``,
ephemeral: true,
});
cooldownSet.add(interaction.user.id + interaction.commandName);
setTimeout(() => {
cooldownSet.delete(interaction.user.id + interaction.commandName);
}, cooldown * 1000);
}
}
if (interaction.guild) interaction.guild.members.fetch();
const eventFile = require(`./events/slash-commands/${interaction.commandName}.js`);
eventFile(client, interaction);
if (interaction.isAutocomplete()) return;
const executed = new Discord.EmbedBuilder()
.setTitle("registered a slash-command")
.setThumbnail(interaction.user.avatarURL())
.setDescription(`\`${interaction.user.tag}\`, <@${interaction.user.id}>`)
.addFields([
{
name: "command:",
value: `/\`${interaction.commandName}\``,
inline: true,
},
{
name: "channel:",
value: "<#" + interaction.channel.id + ">",
inline: true,
},
{
name: "timestamp:",
value: `<t:${Math.round(interaction.createdTimestamp / 1000)}:F> (${Math.round(interaction.createdTimestamp / 1000)})`,
inline: true,
},
])
.setTimestamp()
.setColor(config.color["cmd_log"]);
if (interaction.guild) {
executed
.addFields([
{
name: "guild.id:",
value: `\`${interaction.guild.id}\``,
inline: true,
},
{
name: "guild.name:",
value: `\`${interaction.guild.name}\``,
inline: true,
},
{
name: "link:",
value: `[Link to ${interaction.user.username}'s interaction](https://discord.com/channels/${interaction.guild.id}/${interaction.channel.id}/${interaction.id})`,
inline: true,
},
])
.setFooter({
text: interaction.guild.name,
iconURL: interaction.guild.iconURL(),
});
} else {
executed
.addFields([
{
name: "user.id:",
value: `\`${interaction.user.id}\``,
inline: true,
},
{
name: "user.name:",
value: `\`${interaction.user.username}\``,
inline: true,
},
{
name: "link:",
value: `[Link to ${interaction.user.username}'s discord Account](https://discord.com/users/${interaction.user.id})`,
inline: true,
},
])
.setFooter({
text: interaction.user.tag,
iconURL: interaction.user.avatarURL(),
});
}
if (interaction.options) {
options = "";
await interaction.options._hoistedOptions.forEach((option) => {
options += `\`${option.name}\`: \`${option.value}\`\n`;
});
}
// wait(1000)
if (!options) options = "n/a";
executed.addFields([
{
name: "arguments:",
value: `${options.substring(0, 1000)}${options.toString().length > 1000 ? "\n..." : ""}`,
inline: false,
},
]);
// if (interaction.isChatInputCommand()) {
client.channels.cache.get(config.channel["cmd_log"]).send({ embeds: [executed] });
// }
}
} else return;
});
// custom log override
client.modLog = async function (message, file = "custom") {
const time = Date.now();
const embed0 = new Discord.EmbedBuilder()
.setTitle("Mod-Log")
.setDescription(`\`\`\`${message.toString().substring(0, 2022)}\`\`\``)
.setFooter({ text: "origin: " + file + " | " + gettime(true, time) })
.setColor(config.color["mod_log"]);
client.channels.cache.get(config.channel["mod_log"]).send({ embeds: [embed0] });
};
// custom error override
client.error = async function (message, file = "custom") {
const time = Date.now();
const embed1 = new Discord.EmbedBuilder()
.setTitle("error")
.setDescription(`\`\`\`${message.toString().substring(0, 2022)}\`\`\``)
.setFooter({
text: "origin: " + file + " | " + gettime(true, time),
})
.setColor(config.color["error"]);
client.channels.cache.get(config.channel["mod_log"]).send({ embeds: [embed1] });
};
client.login(config.token);
// uncaught error handling
// log("Initialising: error-handling", "yellow", "reset", false)
// process.on('uncaughtException', function(error, source) {
// const embedfail = new Discord.EmbedBuilder()
// .setTitle('Es gab einen Fehler!')
// .setThumbnail('https://mir-s3-cdn-cf.behance.net/project_modules/max_1200/ab0c1e57515093.59d8c6eb16d19.gif')
// .setFooter({
// text: `${source}`,
// iconURL: client.user.avatarURL({ format: 'png' })
// })
// .setTimestamp()
// .setColor(config.color["error"])
// .addFields([
// { name: "Exakte Zeit:", value: `\`${gettime()}\``, inline: true },
// ])
// if (error.toString().length < 1000) {
// embedfail.addFields([
// { name: "Fehler:", value: `\`${error}\``, inline: false}
// ])
// } else {
// embedfail.addFields([
// { name: "Fehler:", value: "Der Fehler ist zu lang, um hier dargestellt zu werden.", inline: true}
// ])
// const attachment = new Discord.AttachmentBuilder(Buffer.from(`Source:\n${source}\n\n${error}`, 'utf-8'), 'error.log')
// client.channels.cache.get(config.channel["mod_log"]).send({ files: [attachment] })
// }
// client.channels.cache.get(config.channel["mod_log"]).send({ embeds: [embedfail] })
// })
// log(`Initialised: Error-Handling`, "red", "reset", false)