This repository was archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (156 loc) · 4.21 KB
/
index.js
File metadata and controls
186 lines (156 loc) · 4.21 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
const fs = require('fs');
const config = require('./config.json');
const Discord = require('discord.js');
const discord = new Discord.Client();
const logger = require('js-logger');
let commands = [];
let events = [];
let tasks = [];
/**
*
* Allows any other file that has access to index to use other files.
*
* @author Nausher Rao
*
*/
module.exports = {
"discord": discord,
"logger": logger,
}
/**
*
* Main function that handles all calls to other parts of the bot.
*
* @author Nausher Rao
*
*/
function main() {
discord.once('ready', () => {
initLogger();
setPresence();
registerCommands();
registerEvents();
registerTasks();
handleCommands();
logger.info("Bot loaded!");
});
discord.login(config.token);
}
/**
*
* Sets up the logger to look pretty.
* This should be changed to your liking.
*
* @author Nausher Rao
*
*/
function initLogger() {
logger.useDefaults({
defaultLevel: logger.DEBUG,
formatter: function (messages, context) {
messages.unshift(`[${new Date().toUTCString()}] [${context.level.name}]: `)
}
});
}
/**
*
* Sets the initial Discord bot user presence text.
* This should be changed to your liking.
*
* @author Nausher Rao
*
*/
function setPresence() {
logger.info("Setting presence!");
discord.user.setPresence({
status: "dnd",
activity: {
name: "Loading bot...",
type: "WATCHING",
url: null
},
type: "WATCHING" });
}
/**
*
* Load all command files from the "commands" folder, and POST them to the Discord
* command endpoint for the specific server.
*
* @author Nausher Rao
*
*/
function registerCommands() {
logger.info("Loading commands!");
let files = fs.readdirSync('./commands')
.filter(file => file.endsWith('.js') && file != 'example.command.js')
for(const file of files) {
const command = require(`./commands/${file}`);
commands.push(command);
discord.api.applications(discord.user.id).guilds(config.server).commands.post(command);
logger.info(`Loaded command from file: commands/${file}`);
}
}
/**
*
* Load all event handler files from the "events" folder, and registers them
* with the Discord event manager.
*
* @author Nausher Rao
*
*/
function registerEvents() {
logger.info("Loading event handlers!");
let files = fs.readdirSync('./events')
.filter(file => file.endsWith('.js') && file != 'example.event.js');
for(const file of files) {
const event = require(`./events/${file}`);
events.push(event);
if(event.once)
discord.once(event.name, (...args) => event.execute(...args));
else
discord.on(event.name, (...args) => event.execute(...args));
logger.info(`Loaded event handler from file: events/${file}`);
}
}
/**
*
* Load all repeating task files from the "tasks" folder, and registers them
* with the JS Window DOM.
*
* @author Nausher Rao
*
*/
function registerTasks() {
logger.info("Loading tasks!");
let files = fs.readdirSync('./tasks')
.filter(file => file.endsWith('.js') && file != 'example.task.js');
for(const file of files) {
const task = require(`./tasks/${file}`);
tasks.push(task);
setInterval(task.execute, task.interval);
logger.info(`Loaded task from file: tasks/${file}`);
}
}
/**
*
* Code registered directly with the web socket to execute code
* when a slash command ("interaction") is recorded.
*
* @author Nausher Rao
*
*/
function handleCommands() {
logger.info("Registering commands with the interaction create web socket!");
discord.ws.on('INTERACTION_CREATE', async interaction => {
const input = interaction.data.name.toLowerCase();
for(const command of commands) {
if(command.data.name == input) {
logger.debug("Processing command: " + command.data.name);
command.execute(interaction);
break;
} else
continue;
}
});
}
main();