-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
101 lines (84 loc) · 3.27 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
require('./functions/telemetry');
// init Discord
const { Client, Intents, Collection } = require('discord.js');
// init file system
const fs = require('fs');
// init command builder
const { SlashCommandBuilder } = require('@discordjs/builders');
// setting essential global values
// init Discord client
global.client = new Client({
disableEveryone: true,
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGES,
],
});
// init config
global.config = require('./config.json');
global.DEBUG = process.env.NODE_ENV === 'development';
global.CmdBuilder = SlashCommandBuilder;
global.ERR = (err) => {
console.error('ERROR:', err);
if (DEBUG) return;
const { MessageEmbed } = require('discord.js');
const stack = err.stack.length > 4000 ? `${err.stack.slice(0, 4000)}...` : err.stack;
const embed = new MessageEmbed()
.setAuthor({ name: `Error: '${err.message}'` })
.setDescription(`STACKTRACE:\n\`\`\`${stack}\`\`\``)
.setColor('RED');
// client.channels.fetch(config.logChannel).send({ embeds: [embed] });
client.channels.cache.get(config.logChannel).send({ embeds: [embed] });
return;
};
// creating collections
client.commands = new Collection();
client.functions = new Collection();
// anouncing debug mode
if (DEBUG) console.log(`[${config.name}] Bot is on Debug-Mode. Some functions are not going to be loaded.`);
// Login the bot
client.login(process.env.DCtoken)
// TODO: cleanup
.then(() => {
// import Functions and Commands; startup database connection
fs.readdirSync('./functions/STARTUP').forEach((FCN) => {
if (FCN.search('.js') === -1) return;
const INIT = require(`./functions/STARTUP/${FCN}`);
INIT.run(fs);
});
});
client.on('ready', async () => {
// confirm user logged in
console.log(`[${config.name}] Logged in as "${client.user.tag}"!`);
// setup tables
console.log('[DB] Syncing tables...');
await sequelize.sync();
await console.log('[DB] Done syncing!');
// run startup functions
config.setup.setupFunctions.forEach((FCN) => {
client.functions.get(FCN).run(config);
});
});
// EVENT user gets banned
client.on('guildBanAdd', (ban) => client.functions.get('EVENT_guildBanAdd').run(ban));
// EVENT user gets unbanned
client.on('guildBanRemove', (ban) => client.functions.get('EVENT_guildBanRemove').run(ban));
// user joins the server
client.on('guildMemberAdd', (member) => client.functions.get('EVENT_guildMemberAdd').run(member));
// bot joins the server
client.on('guildCreate', (guild) => client.functions.get('EVENT_guildCreate').run(guild));
// bot leaves the server
client.on('guildDelete', (guild) => client.functions.get('EVENT_guildDelete').run(guild));
// TODO: check what information is shared without message intent, so tag recording is still possible
// // record user tag
// client.functions.get('SET_DB_userTagRecord').run(member.id, member.user.tag);
// itneraction is triggered (command, autocomplete, etc.)
client.on('interactionCreate', (interaction) => client.functions.get('EVENT_interactionCreate').run(interaction));
// logging errors and warns
client.on('error', (ERR));
client.on('warn', (ERR));
process.on('uncaughtException', (ERR));