This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiscordService.ts
178 lines (118 loc) · 5.54 KB
/
DiscordService.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
// Copyright (c) 2021 Sendanor. All rights reserved.
import {Observer, ObserverCallback, ObserverDestructor} from "../../core/Observer";
import {DiscordApplicationDTO, isDiscordApplicationDTO} from "./types/DiscordApplicationDTO";
import {DiscordCreateMessageDTO} from "./types/DiscordCreateMessageDTO";
import {DiscordMessageDTO, isDiscordMessageDTO} from "./types/DiscordMessageDTO";
import {DiscordBotGatewayDTO, isDiscordBotGatewayDTO} from "./types/DiscordBotGatewayDTO";
import {DISCORD_API_ENDPOINT, LIBRARY_NAME, LIBRARY_URL, LIBRARY_VERSION} from "./discord-constants";
import { LogService } from "../../core/LogService";
import { RequestClientImpl } from "../../core/RequestClientImpl";
import { JsonAny } from "../../core/Json";
const LOG = LogService.createLogger('DiscordService');
export enum DiscordBotServiceEvent {
}
export class DiscordService {
public static Event = DiscordBotServiceEvent;
private readonly _botToken : string;
private readonly _guildId : string;
private _observer : Observer<DiscordBotServiceEvent>;
private _me : DiscordApplicationDTO | undefined;
public constructor (
botToken : string,
discordGuildId : string
) {
this._botToken = botToken;
this._observer = new Observer<DiscordBotServiceEvent>("DiscordBotService");
this._me = undefined;
this._guildId = discordGuildId;
}
public destroy () {
}
public getMe () : DiscordApplicationDTO | undefined {
return this._me;
}
public on (name : DiscordBotServiceEvent, callback: ObserverCallback<DiscordBotServiceEvent>) : ObserverDestructor {
return this._observer.listenEvent(name, callback);
}
public async initialize () {
this._me = await DiscordService.getMe(this._botToken);
}
public async changeMyNick (newNick : string) : Promise<void> {
await DiscordService.changeMyNick(this._botToken, this._guildId, newNick);
}
public async createMessage (channelId : string, payload: DiscordCreateMessageDTO) : Promise<DiscordMessageDTO> {
return DiscordService.createMessage(this._botToken, channelId, payload);
}
public async createMessageWithNick (nick: string, channelId : string, payload: DiscordCreateMessageDTO) : Promise<DiscordMessageDTO> {
return DiscordService.createMessageWithNick(this._botToken, nick, channelId, payload);
}
public static async getMe (botToken: string) : Promise<DiscordApplicationDTO> {
return await RequestClientImpl.getJson(
`${DISCORD_API_ENDPOINT}/oauth2/applications/@me`,
DiscordService.generateBotHeadersObject(botToken)
).then((response: any) => {
if (!isDiscordApplicationDTO(response)) {
LOG.debug('response = ', response);
throw new TypeError('Response was not DiscordApplicationDTO');
}
return response;
});
}
public static async changeMyNick (botToken: string, guildId: string, newNick : string) : Promise<void> {
const payload = {
nick : newNick
};
return await RequestClientImpl.patchJson(
`${DISCORD_API_ENDPOINT}/guilds/${guildId}/members/@me/nick`,
payload as JsonAny,
DiscordService.generateBotHeadersObject(botToken)
).then((
/*response: any*/) : void => {
});
}
public static async createMessageWithNick (botToken: string, nick: string, channelId : string, payload: DiscordCreateMessageDTO) : Promise<DiscordMessageDTO> {
// await this.changeMyNick(nick);
const newPayload : DiscordCreateMessageDTO = {
...payload,
content: `<${nick}> ${payload.content}`
};
return await DiscordService.createMessage(botToken, channelId, newPayload);
}
public static async getDiscordBotGatewayDTO (botToken: string) : Promise<DiscordBotGatewayDTO> {
return await RequestClientImpl.getJson(
`${DISCORD_API_ENDPOINT}/gateway/bot`,
DiscordService.generateBotHeadersObject(botToken)
).then((response: any) => {
if (!isDiscordBotGatewayDTO(response)) {
LOG.debug('response = ', response);
throw new TypeError('Response was not DiscordBotGatewayDTO');
}
return response;
});
}
public static async createMessage (botToken: string, channelId : string, payload: DiscordCreateMessageDTO) : Promise<DiscordMessageDTO> {
return await RequestClientImpl.postJson(
`${DISCORD_API_ENDPOINT}/channels/${channelId}/messages`,
payload as JsonAny,
DiscordService.generateBotHeadersObject(botToken)
).then((response: any) => {
if (!isDiscordMessageDTO(response)) {
LOG.debug('response = ', response);
throw new TypeError('Response was not DiscordMessageDTO');
}
return response;
});
}
public static generateBotHeadersObject (botToken: string) : { [key: string]: string } {
return {
'Authorization': DiscordService.getBotAuthorizationHeader(botToken),
'User-Agent': DiscordService.getUserAgentString()
};
}
public static getUserAgentString () : string {
return `${LIBRARY_NAME} (${LIBRARY_URL}, v${LIBRARY_VERSION})`;
}
public static getBotAuthorizationHeader (botToken: string) : string {
return `Bot ${botToken}`;
}
}