-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
94 lines (83 loc) · 2.83 KB
/
index.ts
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
// Import packages from NPM
import DiscordJs, { Intents } from "discord.js";
import WOKCommands from "wokcommands";
import path from "path";
import chalk from "chalk";
import dotenv from "dotenv";
// import custom modules
import { checkForRoles, checkIfCollectionsExist } from "./rolesOps";
import { classModel } from "./models/classModel";
import { staffModel } from "./models/staffModel";
import { yearModel } from "./models/yearModel";
// import all environment variables from .env file
dotenv.config();
// Create a new Discord client
const client = new DiscordJs.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
],
});
client.on("ready", async () => {
if (client.user) {
console.log(chalk.green(`Logged in as ${client.user.tag}!`));
console.log(
chalk.yellow.bold(`I am running version: ${process.env.VERSION}`)
);
// Check to make sure the roles exist in all servers
console.log("Checking if all roles exist in servers.");
await Promise.all(
client.guilds.cache.map((guild) => {
checkForRoles(guild);
})
);
}
const dbOptions = {
// These are the default values
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
};
// Set up the WOKCommands instance
const wok = new WOKCommands(client, {
commandDir: path.join(__dirname, "commands"),
featureDir: path.join(__dirname, "features"),
dbOptions,
typeScript: true,
mongoUri: String(process.env.MONGODB_URI),
disabledDefaultCommands: ["help", "language"],
botOwners: [
"603629606154666024",
"680813135556378634",
"451761128704573440",
],
}).setDefaultPrefix(String(process.env.BOT_DEFAULT_PREFIX));
// Set up the connection to the database
wok.on("databaseConnected", async () => {
await checkIfCollectionsExist(classModel);
await checkIfCollectionsExist(staffModel);
await checkIfCollectionsExist(yearModel);
console.log(chalk.green("Connected to the database"));
});
});
// Catch all errors that are not handled well and just dump to the console. THis will be changed later but for now it's fine.
client.on("error", console.error);
client.on("warn", (e) => console.warn(e));
process.on("unhandledRejection", (promise, reason) => {
console.error("Unhandled promise rejection:", promise, "\nreason", reason);
});
// Login to Discord with the bot token and display an error if it fails.
client.login(process.env.BOT_TOKEN).catch((error) => {
console.log(
chalk.red.bold(`There was an error connecting to the Discord API`)
);
console.error(error);
process.exit(1);
});
// Exit gracefully and print the exit code.
process.on("exit", (code) => {
console.log("Now exiting...");
console.log(`Exited with status code: ${code}`);
});