-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
271 lines (204 loc) · 9.88 KB
/
main.js
File metadata and controls
271 lines (204 loc) · 9.88 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
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
// Configuring dotenv package & token variable
require('dotenv').config();
asyncLogs = require('./public/async-logs'); // Creates global variable asyncLogs; Useful for modules
const { log, warn, debug, parseSeconds, startWatch, stopWatch } = asyncLogs;
config = require('./config.json'); // Creates global variable config
prefix = "!"; // Creates global variable prefix w/ default of "!"
const fs = require('fs');
const { Client, GatewayIntentBits, Collection, Partials, Events, ChannelType, REST/*, Routes, InteractionType*/ } = require('discord.js'); // Not a global variable on-purpose to prevent data mishandling
const client = new Client({ intents: [GatewayIntentBits.MessageContent, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages], partials: [Partials.Channel] });
(async () => { try {
require('./public/getNasa')(client, process.exit);
if (process.argv[2] === "nasa") return;
client.commands = new Collection();
const { statusMessages, throwInvalid } = config; // Importing needed configs
prefix = config.prefix || prefix; // If config.prefix===undefined then do default (set above)
// ----------------------------------------------------------------
console.group('Startup');
log('\n%cLoading Commands...\n', 'font-weight: bold;');
startWatch("Commands Load Time", process.uptime());
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
helpList = []; // Array used for the help command
cmdList = []; // Array later used to store command information
for (const file of commandFiles) {
(async (file) => {try {
let cmd = require(`./commands/${file}`);
// grabs the help & conf exports
const help = cmd.help,
conf = cmd.conf;
/*const slash = cmd.command;*/
if (typeof conf !== 'object' || !conf.enabled) {
debug("\u23ED " + file + "\t\u2192\t " + JSON.stringify(conf)); // Log Skipped
return;
}
if (typeof help !== 'object' || !help.name) {
warn(new Error(`help.name of ${file} is undefined`));
return;
}
let obj = {name: help.name, debug: conf}; // used once for entering data in help, deleted after
client.commands.set(help.name.toLowerCase(), cmd); // defines the commands, this will be used for running the commands
if (typeof help.description === 'string') obj.description = help.description; // Add description if it exists
if (typeof help.usage === 'string') obj.usage = help.usage; // Add usage info if it exists
switch (Array.isArray(help.aliases) && help.aliases.length > 0) { // if there are no aliases, don't add them
case true:
for (alias of help.aliases) {
client.commands.set(alias.toLowerCase(), cmd);
}
obj.aliases = help.aliases;
log(`${help.name}\n \u21B3 ${help.aliases.join("\n \u21B3 ")}`);
break;
case false:
log(help.name);
break;
}
cmdList.push(obj);
if (!help.omit)
helpList.push({ name: (prefix + obj.name), value: (obj.description ||
((typeof obj.usage === "string" && obj.usage.length > 0) ?
`${prefix}${obj.usage}` : "<:TealDeer:910194620732932106>")),
inline: true}); // adds the information to the commands list
/* if (slash && 'data' in slash && 'execute' in slash) {
log(` \u25AA Slash command loaded.`);
} else {
log(` \u25AA Slash command not loaded.`);
} */
} catch (e) {
e.fileName = file;
log();
warn(e);
warn("SyntaxError:", `file ${file} was incorrectly setup so it was not loaded`);
log();
}})(file)
}
// Export all the information needed for the !help command, circular dependency fix
(client.commands.get('help').setup(cmdList)) ? log('\n%cFinished!\n', 'font-weight: bold;') : process.exit(1);
stopWatch("Commands Load Time", process.uptime(), undefined, true); // Using this to develop asynchronous commands
console.groupEnd('Startup');
// ----------------------------------------------------------------
const MessageTextEvent = async msg => {
// If the message mentions the bot, return the prefix
if(msg.mentions.has(client.user) && msg.content.startsWith(`<@${client.user.id}>`)) {
return msg.reply(`My prefix is \`${prefix}\``);
};
if(!msg.content.startsWith(prefix)) return; // if the prefix is not given, return
// cleans up the command for easy parsing
const split = msg.content.split(" "),
args = split.slice(1),
command = split[0].slice(prefix.length).toLowerCase();
const cmd = await client.commands.get(command);
if (typeof cmd !== "object") {
switch (throwInvalid == true) {
case true:
msg.reply(`**${prefix}${command}** is not a valid command.`);
return;
case false:
return;
}
}
try { // Runs the command
if (cmd.conf.await) { // Check if await should be used
await cmd.run(client, msg, args);
} else {
cmd.run(client, msg, args);
}
} catch (e) { // Respond to an incorrect command
debug(e, `"${msg.content}"`);
try {
await msg.reply(`Please use the format \`${prefix}${cmd.help.usage}\``);
} catch (e) {
debug(e);
try {
await msg.reply(`Incorrect usage of \`${prefix}${command}\``);
} catch (e) {}
}
};
};
// ------------ Application Commands Under Development ------------
// const MessageApplicationCommandEvent = async msg => {
// const command = interaction.client.commands.get(interaction.commandName).slashCommand;
// if (!command) {
// error(`No command matching ${interaction.commandName} was found.`);
// return;
// }
// try {
// await command.execute(interaction);
// } catch (error) {
// error(error);
// await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
// }
// };
// ----------------------------------------------------------------
client.on(Events.MessageCreate, async msg => {
if (msg.author.bot) return; // Prevent bots from running commands
switch (msg.channel.type) {
case ChannelType.GuildText:
case ChannelType.GuildStageVoice:
case ChannelType.GuildVoice:
MessageTextEvent(msg);
break;
case ChannelType.DM:
case ChannelType.GroupDM:
break;
}
});
// ------------ Application Commands Under Development ------------
// client.on(Events.InteractionCreate, async int => {
// if (msg.author.bot) return; // Prevent bots from running commands
// switch (msg.interaction.type) {
// case InteractionType.ApplicationCommand:
// MessageApplicationCommandEvent(msg);
// }
// });
// ----------------------------------------------------------------
client.on("ready", () => {
// Logs startup info to console in English & Portuguese
log.bilingual (
`Bot online with : ${client.users.cache.size} users, ${client.guilds.cache.size} guilds, & ${client.channels.cache.size} channels.`,
`Bot foi iniciado com : ${client.users.cache.size} usuários, ${client.guilds.cache.size} servidores, e ${client.channels.cache.size} canais.`
);
dt = Date();
ut = process.uptime();
parseSeconds(ut, (time) => {
log('\nStart Date : ' + dt, "\nLoad Time : " + time, '\n');
});
void(delete ut, dt); // Some cleaning
let stsId = 0;
setInterval(() => {
client.user.setActivity(statusMessages[stsId]);
stsId = (stsId+1)%statusMessages.length; // Loops through messages
}, 10000);
/* Rough Translations, Fix Me! */
//0 = Jogando / Logging On To
//1 = Transmitindo / Transmitting
//2 = Ouvindo / Receiving
//3 = Assistindo / Stand-By
});
// ----------------------------------------------------------------
/**
* @private
*
* Login the bot
*/
let token = process.env["DISCORD_TOKEN"];
client.login(token);
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(token);
void(delete token);
log(client.guilds.cache.map((guild) => guild));
// (async (clientId, guildId) => {
// try {
// log(`Started refreshing ${slashCommands.length} application (/) commands.`);
// // The put method is used to fully refresh all commands in the guild with the current set
// const data = await rest.put(
// Routes.applicationGuildCommands(clientId, guildId),
// { body: slashCommands },
// );
// log(`Successfully reloaded ${data.length} application (/) commands.`);
// } catch (err) {
// // And of course, make sure you catch and log any errors!
// error(err);
// }
// })();
} catch (e) {
throw e;
}})()