-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBotConfig.ts
204 lines (162 loc) · 6.39 KB
/
BotConfig.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
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
import { Client, ColorResolvable, Snowflake } from 'discord.js';
import { Version2Client as JiraClient } from 'jira.js';
import config from 'config';
import MojiraBot from './MojiraBot.js';
import Sqlite3 from 'better-sqlite3';
import { VersionChangeType } from './tasks/VersionFeedTask.js';
import SlashCommandRegister from './commands/commandHandlers/SlashCommandRegister.js';
function getOrDefault<T>( configPath: string, defaultValue: T ): T {
if ( !config.has( configPath ) ) MojiraBot.logger.debug( `config ${ configPath } not set, assuming default` );
return config.has( configPath ) ? config.get( configPath ) : defaultValue;
}
export enum PrependResponseMessageType {
Never = 'never',
WhenResolved = 'whenResolved',
Always = 'always'
}
export class RequestConfig {
public channels: Snowflake[];
public internalChannels: Snowflake[];
public requestLimits: number[];
public testingRequestChannels: Snowflake[];
public logChannel: Snowflake;
public invalidTicketEmoji: string;
public noLinkEmoji: string;
public warningLifetime: number;
public invalidRequestJql: string;
public waitingEmoji: string;
public suggestedEmoji: string[];
public ignorePrependResponseMessageEmoji: string;
public ignoreResolutionEmoji: string;
public resolveDelay: number;
public progressMessageAddDelay: number;
public prependResponseMessage: PrependResponseMessageType;
public prependResponseMessageInLog: boolean;
public responseMessage: string;
constructor() {
this.channels = getOrDefault( 'request.channels', [] );
this.internalChannels = this.channels.length ? config.get( 'request.internalChannels' ) : getOrDefault( 'request.internalChannels', [] );
this.requestLimits = this.channels.length ? config.get( 'request.requestLimits' ) : getOrDefault( 'request.requestLimits', [] );
this.testingRequestChannels = getOrDefault( 'request.testingRequestChannels', [] );
this.logChannel = config.get( 'request.logChannel' );
if ( this.channels.length !== this.internalChannels.length ) {
throw new Error( 'There are not exactly as many Request channels and ' );
}
this.invalidTicketEmoji = config.get( 'request.invalidTicketEmoji' );
this.noLinkEmoji = config.get( 'request.noLinkEmoji' );
this.warningLifetime = config.get( 'request.warningLifetime' );
this.invalidRequestJql = config.get( 'request.invalidRequestJql' );
this.waitingEmoji = config.get( 'request.waitingEmoji' );
this.suggestedEmoji = getOrDefault( 'request.suggestedEmoji', [] );
this.ignorePrependResponseMessageEmoji = config.get( 'request.ignorePrependResponseMessageEmoji' );
this.ignoreResolutionEmoji = config.get( 'request.ignoreResolutionEmoji' );
this.resolveDelay = config.get( 'request.resolveDelay' );
this.progressMessageAddDelay = config.get( 'request.progressMessageAddDelay' );
this.prependResponseMessage = getOrDefault( 'request.prependResponseMessage', PrependResponseMessageType.Never );
this.prependResponseMessageInLog = getOrDefault( 'request.prependResponseMessageInLog', false );
this.responseMessage = getOrDefault( 'request.responseMessage', '' );
}
}
export interface RoleConfig {
emoji: Snowflake;
title: string;
desc?: string;
id: Snowflake;
}
export interface RoleGroupConfig {
roles: RoleConfig[];
prompt: string;
desc?: string;
color: ColorResolvable;
channel: Snowflake;
message?: Snowflake;
radio?: boolean;
}
export interface FilterFeedConfig {
jql: string;
jqlRemoved?: string;
channel: Snowflake;
interval: number;
filterFeedEmoji: string | Snowflake;
title: string;
titleSingle?: string;
publish?: boolean;
cached?: boolean;
}
export interface VersionConfig {
name: string;
id: number;
}
export interface VersionFeedConfig {
projects: VersionConfig[];
channel: Snowflake;
interval: number;
versionFeedEmoji: string | Snowflake;
scope: number;
actions: VersionChangeType[];
publish?: boolean;
}
export default class BotConfig {
public static debug: boolean;
public static logDirectory: false | string;
private static token: string;
private static jiraPat?: string;
public static owners: Snowflake[];
public static homeChannel: Snowflake;
public static modmailEnabled: boolean;
public static modmailChannel: Snowflake;
public static ticketUrlsCauseEmbed: boolean;
public static quotedTicketsCauseEmbed: boolean;
public static requiredTicketPrefix: string;
public static forbiddenTicketPrefix: string;
public static embedDeletionEmoji: string;
public static maxSearchResults: number;
public static projects: string[];
public static request: RequestConfig;
public static roleGroups: RoleGroupConfig[];
public static filterFeeds: FilterFeedConfig[];
public static versionFeeds: VersionFeedConfig[];
public static database: Sqlite3.Database;
public static init(): void {
this.debug = getOrDefault( 'debug', false );
this.logDirectory = getOrDefault( 'logDirectory', false );
this.token = config.get( 'token' );
this.jiraPat = getOrDefault( 'jiraPat', undefined );
this.owners = getOrDefault( 'owners', [] );
this.homeChannel = config.get( 'homeChannel' );
this.modmailEnabled = getOrDefault( 'modmailEnabled', false );
this.modmailChannel = getOrDefault( 'modmailChannel', '' );
this.ticketUrlsCauseEmbed = getOrDefault( 'ticketUrlsCauseEmbed', false );
this.quotedTicketsCauseEmbed = getOrDefault( 'quotedTicketsCauseEmbed', false );
this.forbiddenTicketPrefix = getOrDefault( 'forbiddenTicketPrefix', '' );
this.requiredTicketPrefix = getOrDefault( 'requiredTicketPrefix', '' );
this.embedDeletionEmoji = getOrDefault( 'embedDeletionEmoji', '' );
this.maxSearchResults = config.get( 'maxSearchResults' );
this.projects = config.get( 'projects' );
this.request = new RequestConfig();
this.roleGroups = getOrDefault( 'roleGroups', [] );
this.filterFeeds = config.get( 'filterFeeds' );
this.versionFeeds = config.get( 'versionFeeds' );
this.database = new Sqlite3( './db.sqlite' );
}
public static async login( client: Client ): Promise<boolean> {
try {
await client.login( this.token );
await SlashCommandRegister.registerCommands( client, this.token );
} catch ( err ) {
MojiraBot.logger.error( err );
return false;
}
return true;
}
public static jiraLogin(): void {
// TODO: integrate newErrorHandling from Jira.js
MojiraBot.jira = new JiraClient( {
host: 'https://bugs.mojang.com',
telemetry: false,
authentication: this.jiraPat === undefined ? undefined : {
personalAccessToken: this.jiraPat,
},
} );
}
}