-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.ts
145 lines (140 loc) · 5.54 KB
/
Client.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
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
import { Collection, Client as DiscordClient } from 'discord.js';
import { Service } from 'typedi';
import Logger from './utils/Logger';
import { BotSettings, BotClient, BotInitializationError } from './types';
import Command from './Command';
import ActionManager from './managers/ActionManager';
import configuration from './config/config';
import PortalAPIManager from './managers/PortalAPIManager';
/**
* The class representing the Discord bot.
*
* Our Client class not only holds the client itself, but also implements additional
* parameters to keep track of bot settings and registered Events and Commands.
*
* The procedure when initializing the Client goes something like this:
* - Pass in any required ClientOptions for Discord.js, if any.
* - Take default configuration from "config.ts" and pass through to our Client,
* adding environment variables found. If any required environment variables don't exist,
* we error out.
* - Initialize our ActionManager, our method of dynamically importing Events and Commands
* - Initialize our PortalAPIManager, our method of centralizing API tokens to the Membership
* Portal API.
* - Login to Discord API when done initializing everything.
*
* ActionManager does the heavy lifting, so read that as well.
*/
@Service()
export default class Client extends DiscordClient implements BotClient {
/**
* The settings for the Client.
*
* These are a mix of environment variables and default Discord.js client options.
*/
public settings: BotSettings;
/**
* The default constructor for Client.
*
* Begins the configuration process. Initialization is done in {@link initialize initialize()}.
* @param actionManager An ActionManager class to run. Injected by TypeDI.
* @param portalAPIManager A PortalAPIManager class to run. Injected by TypeDI
*/
constructor(private actionManager: ActionManager, private portalAPIManager: PortalAPIManager) {
super(
configuration.clientOptions || {
intents: [
'GUILDS',
'GUILD_INTEGRATIONS',
'GUILD_WEBHOOKS',
'GUILD_MESSAGES',
'GUILD_MEMBERS',
'DIRECT_MESSAGES',
'GUILD_MESSAGE_REACTIONS',
'DIRECT_MESSAGE_REACTIONS',
],
}
);
this.settings = configuration;
// We absolutely need some envvars, so if they're not in our .env file, nuke the initialization.
// We can throw Errors here to nuke the bot, since we don't have any catches higher up.
if (!process.env.BOT_TOKEN) {
throw new BotInitializationError('Bot Token');
}
if (!process.env.BOT_PREFIX) {
throw new BotInitializationError('Bot Prefix');
}
if (!process.env.CLIENT_ID) {
throw new BotInitializationError('App Client ID');
}
if (!process.env.ACMURL_USERNAME) {
throw new BotInitializationError('ACMURL Username');
}
if (!process.env.ACMURL_PASSWORD) {
throw new BotInitializationError('ACMURL Password');
}
if (!process.env.MEMBERSHIP_PORTAL_API_URL) {
throw new BotInitializationError('Membership Portal API URL');
}
if (!process.env.MEMBERSHIP_PORTAL_API_USERNAME) {
throw new BotInitializationError('Membership Portal API Username');
}
if (!process.env.MEMBERSHIP_PORTAL_API_PASSWORD) {
throw new BotInitializationError('Membership Portal API Password');
}
if (!process.env.DISCORD_GUILD_IDS) {
throw new BotInitializationError('Discord Guild ID List');
}
if (!process.env.MATCH_ROLE_ID) {
throw new BotInitializationError('Match Role ID');
}
if (!process.env.AS_ATTENDANCE_FORM_URL) {
throw new BotInitializationError('AS Funded Event Attendance Form');
}
this.settings.clientID = process.env.CLIENT_ID;
this.settings.token = process.env.BOT_TOKEN;
this.settings.prefix = process.env.BOT_PREFIX;
this.settings.maintainerID = process.env.MAINTAINER_USER_ID;
this.settings.apiKeys.catAPI = process.env.CAT_API_KEY;
this.settings.apiKeys.unsplash = process.env.UNSPLASH_ACCESS_KEY;
this.settings.acmurl.username = process.env.ACMURL_USERNAME;
this.settings.acmurl.password = process.env.ACMURL_PASSWORD;
this.settings.portalAPI.url = process.env.MEMBERSHIP_PORTAL_API_URL;
this.settings.portalAPI.username = process.env.MEMBERSHIP_PORTAL_API_USERNAME;
this.settings.portalAPI.password = process.env.MEMBERSHIP_PORTAL_API_PASSWORD;
this.settings.discordGuildIDs = JSON.parse(process.env.DISCORD_GUILD_IDS) as Array<string>;
this.settings.matchRoleID = process.env.MATCH_ROLE_ID;
this.settings.asAttendanceForm = process.env.AS_ATTENDANCE_FORM_URL;
this.initialize().then();
}
/**
* Initialize the Client and connect to the Discord API.
*
* Registers all Events and Commands and then logs in to the API for being ready.
* Highly recommend to read ActionManager's code to understand what this does.
* @private
*/
private async initialize(): Promise<void> {
try {
this.portalAPIManager.initializeTokenHandling(this);
this.actionManager.initializeCommands(this);
ActionManager.initializeEvents(this);
await this.login(configuration.token);
} catch (e) {
Logger.error(`Could not initialize bot: ${e}`);
}
}
/**
* Get a map of [commandName, Command] pairs.
*
* Useful to find a registered Command quickly.
*/
public get commands(): Collection<string, Command> {
return this.actionManager.commands;
}
/**
* Get the API token for the Membership Portal API.
*/
public get apiToken(): string {
return this.portalAPIManager.apiToken;
}
}