-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.ts
113 lines (98 loc) · 3.06 KB
/
startup.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import DiscordJS, { GatewayIntentBits, Events } from "discord.js";
import dotenv from "dotenv";
import { SetupFunctions } from "./setup";
import { CommandBridge } from "./interpreters/std_bridge";
import { DatabaseFunctions } from "./utility/database";
import { UtilityFunctions } from "./utility/general";
import { Connection, Pool } from "mysql2";
import { LogLevel, LoggingFunctions } from "./utility/logging";
dotenv.config();
const client = new DiscordJS.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
],
});
UtilityFunctions.errorCheck(
process.env.TOKEN === undefined,
"Token must be provided"
);
UtilityFunctions.errorCheck(
process.env.USER === undefined,
"Username must be provided"
);
UtilityFunctions.errorCheck(
process.env.DATABASE === undefined,
"Database name must be provided"
);
const gamesDBName: string | undefined = process.env.DATABASE;
const connectionMode: string | undefined = process.env.CONNECTIONMODE;
const guildID: string = String(process.env.TESTGUILD);
let gamedb: Connection | Pool | undefined = undefined;
let port: number | undefined = parseInt(String(process.env.PORT?.trim()));
port = isNaN(port) ? undefined : port;
const isPool: boolean =
connectionMode === undefined || connectionMode.toLowerCase() === "pool";
client.on(Events.ClientReady, () => {
LoggingFunctions.log(
"Bot is ready.",
LogLevel.INFO,
undefined,
undefined,
true
);
const guild =
process.env.MODE === "test"
? client.guilds.cache.get(guildID)
: undefined;
DatabaseFunctions.createDatabase(
process.env.HOST,
process.env.USER,
process.env.PASSWORD,
gamesDBName,
port
);
if (isPool) {
gamedb = DatabaseFunctions.createPool(
process.env.HOST,
process.env.USER,
process.env.PASSWORD,
gamesDBName,
port
);
}
SetupFunctions.commandSetup(guild, client);
LoggingFunctions.log(
"Bot has completed setup.",
LogLevel.INFO,
undefined,
undefined,
true
);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) {
return;
}
if (!isPool || gamedb === undefined) {
gamedb = DatabaseFunctions.connect(
process.env.HOST,
process.env.USER,
process.env.PASSWORD,
gamesDBName,
port
);
}
const id =
process.env.EXECUTIONMODE?.toLowerCase() === "test"
? guildID
: String(interaction.guild?.id);
await CommandBridge.reply(interaction, gamedb, id, client).then(() => {
if (!isPool) {
DatabaseFunctions.disconnect(gamedb);
}
});
});
client.login(process.env.TOKEN);