-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (76 loc) · 2.7 KB
/
app.js
File metadata and controls
87 lines (76 loc) · 2.7 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
// ===== Libraries/API Imports ===== //
require("dotenv").config();
const fs = require("fs");
const {
Client,
Collection,
GatewayIntentBits,
Partials,
} = require("discord.js");
const mongoose = require("./database/mongoose");
// ===== Declaring Bot GatewayIntentBits and Partials ===== //
// Creating a new client with intents and partials needed for this bot to function
// Partials make sure that we receive the full data of the object returned from events
const client = new Client({
intents: [
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
],
partials: [
Partials.Message,
Partials.Channel,
Partials.GuildMember,
Partials.User,
Partials.GuildScheduledEvent,
],
});
// ===== Setting up Bot Configs ===== //
client.prefix = "!"; // Messages starting with the prefix will be handled as a command
client.commands = new Collection(); // Commands are to be stored in a hash
// ===== Authenticates Bot Login ===== //
client.on("ready", (client) => {
console.log(`This bot is now online: ${client.user.tag}`);
});
client.login(process.env.TOKEN);
// ===== Retrieves all command and event files from the project ===== //
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
const eventFiles = fs
.readdirSync("./events")
.filter((file) => file.endsWith(".js"));
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client));
} else {
client.on(event.name, (...args) => event.execute(...args, client));
}
}
// ===== Initializes MongoDB ===== //
mongoose.execute();
client.on("messageCreate", (message) => {
if (message.author.bot) return; // only allow non-bots to perform any code execution
const userInput = message.content.toLowerCase().split(" ");
const userInputText = userInput[0];
if (userInputText.startsWith(client.prefix)) {
const args = message.content
.slice(client.prefix.length) // start from the prefix to the end of the string
.trim() // reduce and trailing or leading white spaces
.split(/ +/g); // globally splits the string by spaces
const commandName = userInputText.split(client.prefix)[1];
const command = client.commands.get(commandName);
if (!command)
return message.channel.send(
`There does not exist a command within this bot called ${commandName}.`
);
command.run(client, message, args);
}
});