forked from OtakoidTony/MessengerBotExample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KakaoDiscord.js
381 lines (367 loc) · 15 KB
/
KakaoDiscord.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// TODO: Channel management | Create DM Edit History Permission
// TODO: Edit Profile | Edit_Profile
// TODO: Invites | Join
// TODO: Message management | Send_File Edit Delete
// TODO: Role management | Create Delete Edit Info
// TODO: Server management | Ban Ban_List Create Delete Edit Info Kick Unban Change_Owner
// TODO: User management | Voice_Move
function Discord(isBot, token) {
this.BaseURL = "https://discordapp.com/api";
this.GatewayUrl = "wss://gateway.discord.gg/?v=6&encoding=json";
this.token = token;
this.isBot = isBot;
this.identify_object = {
"token": token,
"properties": {
"$os": "linux",
"$browser": "MessengerBot",
"$device": "Android"
}
};
function GET(url) {
var url = new java.net.URL(url);
var con = url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
if (this.isBot) {
con.setRequestProperty("Authorization", "Bot " + this.token);
} else {
con.setRequestProperty("Authorization", "Bearer " + this.token);
}
if (con != null) {
con.setConnectTimeout(5000);
con.setUseCaches(false);
var isr = new java.io.InputStreamReader(con.getInputStream());
var br = new java.io.BufferedReader(isr);
var str = br.readLine();
var line = "";
while ((line = br.readLine()) != null) {
str += "\n" + line;
}
isr.close();
br.close();
con.disconnect();
}
var result = str + "";
return JSON.parse(result);
}
/**
* 클라이언트가 접속시 사용할 단일 유효 WSS URL이
* 담긴 오브젝트를 반환합니다.
* */
this.getGateway = function () {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/gateway")
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.method(org.jsoup.Connection.Method.GET)
.execute();
return result;
} catch (e) {
Log.debug(e);
}
}
/**
* 봇에 관련된 Get Gateway 기반의 함수입니다.
* 몇 함수는 이 함수로부터 쿠키를 얻어 사용합니다.
* */
this.getGatewayBot = function () {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/gateway/bot")
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bot " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.method(org.jsoup.Connection.Method.GET)
.execute();
return result;
} catch (e) {
Log.debug(e);
}
}
/**
* 디스코드로 메시지를 전송합니다.
* @param {any} sender 발신자 이름
* @param {any} message 디스코드로 전송할 메시지 내용
* 주의: 2000자까지 발송 가능.
* @param {any} room 보낸 카카오톡 채팅방 이름
* @param {any} channel_id 디스코드 채널 ID
*/
this.sendMessage = function (sender, message, room, channel_id) {
var message_json = {
"tts": false,
"embed": {
"title": sender,
"description": message,
"author": {
"name": room,
"icon_url": "https://developers.kakao.com/assets/img/about/logos/kakaolink/kakaolink_btn_medium_ov.png"
},
"image": {
"url": "https://developers.kakao.com/assets/img/kakao.png"
}
}
};
if (this.isBot) {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/channels/" + channel_id + "/messages")
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bot " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGatewayBot().cookies())
.requestBody(JSON.stringify(message_json))
.header("Content-Type", "application/json")
.post();
return result;
} catch (e) {
Log.debug(e);
}
} else {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/channels/" + channel_id + "/messages")
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bearer " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGateway().cookies())
.requestBody(JSON.stringify(message_json))
.header("Content-Type", "application/json")
.post();
return result;
} catch (e) {
Log.debug(e);
}
}
}
/**
* 채널 혹은 개인 채팅에서의 메시지를 수신합니다.
* 만약 길드채널인 경우 이 함수는 VIEW_CHANNEL 권한이 요구됩니다.
* @param {string} channel_id 디스코드 채널 ID
*/
this.getMessage = function (channel_id) {
try {
return GET(this.BaseURL + "/v6/channels/" + channel_id + "/messages");
} catch (e) {
Log.debug(e);
}
}
/**
* 채널 객체를 반환하는 함수입니다.
* @param {string} channel_id 디스코드 채널 ID
*/
this.getChannel = function (channel_id) {
try {
var url = new java.net.URL(this.BaseURL + "/v6/channels/" + channel_id);
var con = url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0");
if (this.isBot) {
con.setRequestProperty("Authorization", "Bot " + this.token);
} else {
con.setRequestProperty("Authorization", "Bearer " + this.token);
}
if (con != null) {
con.setConnectTimeout(5000);
con.setUseCaches(false);
var isr = new java.io.InputStreamReader(con.getInputStream());
var br = new java.io.BufferedReader(isr);
var str = br.readLine();
var line = "";
while ((line = br.readLine()) != null) {
str += "\n" + line;
}
isr.close();
br.close();
con.disconnect();
}
var result = str + "";
return JSON.parse(result);
} catch (e) {
Log.debug(e);
}
}
/**
* 채널을 삭제하거나 개인 채팅을 종료(삭제)할 때 사용하는 함수입니다.
* @param {any} channel_id 디스코드 채널 ID
*/
this.deleteChannel = function (channel_id) {
if (this.isBot) {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/channels/" + channel_id)
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bot " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGatewayBot().cookies())
.header("Content-Type", "application/json")
.method(org.jsoup.Connection.Method.DELETE)
.execute();
return result;
} catch (e) {
Log.debug(e);
}
} else {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/channels/" + channel_id)
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bearer " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGateway().cookies())
.header("Content-Type", "application/json")
.method(org.jsoup.Connection.Method.DELETE)
.execute();
return result;
} catch (e) {
Log.debug(e);
}
}
}
/**
* 새로운 초대 링크를 생성합니다.
* @param {string} channel_id 디스코드 채널 ID
* @param {number} max_age 초대링크 유효시간
* 기본값: 86400 (24 hours)
* @param {number} max_uses 초대링크 최대 사용치
* 기본값: 0 (무제한)
* @param {boolean} temporary 역할 부여한 경우의 초대 여부
* 기본값: false
* @param {boolean} unique 고유한 링크 생성 여부
* 기본값: false
*/
this.createChannelInvite = function (channel_id, max_age, max_uses, temporary, unique) {
var json_params = {
'max_age': max_age,
'max_uses': max_uses,
'temporary': temporary,
'unique': unique
};
if (this.isBot) {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/channels/" + channel_id + "/invites")
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bot " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGatewayBot().cookies())
.requestBody(JSON.stringify(json_params))
.header("Content-Type", "application/json")
.post();
return result;
} catch (e) {
Log.debug(e);
}
} else {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/channels/" + channel_id + "/invites")
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bearer " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGateway().cookies())
.requestBody(JSON.stringify(json_params))
.header("Content-Type", "application/json")
.post();
return result;
} catch (e) {
Log.debug(e);
}
}
}
this.getChannelInvite = function (channel_id) {
try {
return GET(this.BaseURL + "/v6/channels/" + channel_id + "/invites");
} catch (e) {
Log.debug(e);
}
}
this.deleteInvite = function (invite_code) {
if (this.isBot) {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/invites/" + invite_code)
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bot " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGatewayBot().cookies())
.header("Content-Type", "application/json")
.method(org.jsoup.Connection.Method.DELETE)
.execute();
return result;
} catch (e) {
Log.debug(e);
}
} else {
try {
var result = org.jsoup.Jsoup.connect(this.BaseURL + "/v6/invites/" + invite_code)
.timeout(5000)
.ignoreContentType(true)
.header("Host", "discordapp.com")
.header("Authorization", "Bearer " + this.token)
.header("User-Agent", "Mozilla/5.0")
.header("Cache-Control", "no-cache")
.header("Accept-Encoding", "gzip, deflate")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Accept", "*/*")
.cookies(this.getGateway().cookies())
.header("Content-Type", "application/json")
.method(org.jsoup.Connection.Method.DELETE)
.execute();
return result;
} catch (e) {
Log.debug(e);
}
}
}
}