-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
93 lines (78 loc) · 2.94 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
require('./functions/telemetry');
// init Discord
const {
Client, IntentsBitField, Collection, Partials,
} = require('discord.js');
// init filesystem
const fs = require('fs');
// init command builder
const { SlashCommandBuilder } = require('@discordjs/builders');
// use contructor to create intent bit field
const intents = new IntentsBitField([
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
]);
// init Discord client
global.client = new Client({
allowedMentions: { parse: ['users', 'roles'], repliedUser: true },
partials: [Partials.Message],
intents,
});
// 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 { EmbedBuilder } = require('discord.js');
const embed = new EmbedBuilder()
.setAuthor({ name: `Error: '${err.message}'` })
.setDescription(`STACKTRACE:\n\`\`\`${err.stack.slice(0, 4000)}\`\`\``)
.setColor(16449540);
client.channels.cache.get(config.logChannel).send({ embeds: [embed] });
return;
};
// create new collections in client and config
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.`);
client.login(process.env.discordToken)
.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.once('ready', async () => {
// setup tables
console.log('[DB] Syncing tables...');
await sequelize.sync();
await console.log('[DB] Done syncing!');
});
client.on('ready', async () => {
// confirm user logged in
console.log(`[${config.name}] Logged in as "${client.user.tag}"!`);
// run startup functions
config.setup.setupFunctions.forEach((FCN) => {
client.functions.get(FCN).run();
});
});
// trigger on new message
client.on('messageCreate', (message) => client.functions.get('EVENT_messageCreate').run(message));
// trigger on channelDeletion
client.on('channelDelete', (channel) => client.functions.get('EVENT_channelDelete').run(channel));
// trigger on guildDelete
client.on('guildDelete', (guild) => client.functions.get('EVENT_guildDelete').run(guild));
// trigger on deleted message with raw package
client.on('messageDelete', (message) => client.functions.get('EVENT_messageDelete').run(message));
// 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));