-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
430 lines (381 loc) · 13.5 KB
/
server.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
'use strict';
const bodyParser = require('body-parser');
const PushmessageLine = require('./Commands/Pushmessage');
const line = require('@line/bot-sdk');
const express = require('express');
let guild = null;
require('x-date');
// create LINE SDK config from env variables
const config = {
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET,
};
const allycode = process.env.GUILD_ALLYCODE;
const rssChannelId = process.env.RSS_CCHANNEL_ID;
const ApiSwgohHelp = require('api-swgoh-help');
const swapi = new ApiSwgohHelp({
"username": process.env.API_USERNAME,
"password": process.env.API_PASSWORD
});
// create LINE SDK client
const client = new line.Client(config);
// create Express app
// about Express itself: https://expressjs.com/
const app = express();
// create application/json parser
const jsonParser = bodyParser.json()
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
// register a webhook handler with middleware
// about the middleware, please refer to doc
app.post('/callback', line.middleware(config), (req, res) => {
req.connection.setTimeout(1000*60*60*5);
Promise
.all(req.body.events.map(handleEvent))
.then((result) => res.json(result))
.catch((err) => {
console.error(err);
res.status(500).end();
});
});
// this is for echobase
app.post('/echobase', jsonParser, function(req,res) {
console.log('Webhook recieved!');
res.send({status: 200});
console.log("req.body", req.body);
var discordMessage = req.body;
var lineMessage = "";
lineMessage += discordMessage.content.replaceAll("`", "").replaceAll(":white_check_mark:","✔").replaceAll(":x:","❌").replaceAll(":busts_in_silhouette:","Char").replace(":rocket:","Fleet").replace(":information_source:","");
console.log('lineMessage', lineMessage);
PushmessageLine(rssChannelId, lineMessage);
});
// event handler
async function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
// ignore non-text-message event
return Promise.resolve(null);
}
var message = "";
console.log(event);
if(event.message.text.toLowerCase() == "hello there" || event.message.text.toLowerCase() == "hello there!") {
message = "General Kenobi!";
}
let ran = getRandomInt(100);
if(ran && ran >95 && event.source.groupId == rssChannelId){
const grivious = require('./Commands/GG');
grivious(event.source.groupId);
}
if(event.source.userId == 'U772f8750bbd469cb25d2a2b64925d78f' && !event.source.groupId)
{
client.pushMessage(rssChannelId, event.message)
.then(() => {
console.log('pushed');
})
.catch((err) => {
console.log(err.message);
});
}
if (event.message.text.startsWith("#")) {
var words = event.message.text.split(" ");
var eventWithoutStart = words[0].replace("#", "");
try{
message = "🤔";
switch (eventWithoutStart.toLowerCase()) {
case "events":
try {
const GetEvents = require('./Commands/GetEvents');
GetEvents(event.source.groupId);
}
catch (err) {
message = err.message;
}
break;
case "heist":
try {
const GetHeists = require('./Commands/GetHeists');
GetHeists(event.source.groupId);
}
catch (err) {
message = err.message;
}
break;
case "spunkte":
try {
const spunkte = require('./Commands/Spunkte');
let allycode = spunkte(event.source.userId , event.source.groupId);
}
catch (err) {
message = err.message;
}
break;
case "spunkteall":
try {
if(event.source.userId == 'U772f8750bbd469cb25d2a2b64925d78f' || event.source.userId == 'U9a88ae364b5103fd0cec37efdb54f9e0' || event.source.userId == 'Ufeaa0ff6b92c16dd947bc4b3e8718600' || event.source.userId == 'U16a21d48a86c6b19d02b66debca3011d' || event.source.userId == 'U6d2c2dc11d1abe2c6d94d5573063462c')
{
const spunkteAll = require('./Commands/spunkteAll');
let allycode = spunkteAll(event.source.userId , event.source.groupId);
}
else{
message = "Diese Funktion steht dir leider nicht zur verfügung.";
}
}
catch (err) {
message = err.message;
}
break;
case "addme":
try{
let messageWithoutCommando = event.message.text.replace("#addme ","");
const addMe = require('./Commands/AddAllyCode');
let allycode = addMe(event.source.userId , messageWithoutCommando, event.source.groupId );
}
catch(err){
message = err.message;
};
break;
case "addguild":
try{
let messageWithoutCommando = event.message.text.replace("#addguild ","");
const configData = require('./Commands/AddAllyCode');
let allycode = configData(event.source.groupId , messageWithoutCommando, event.source.groupId );
}
catch(err){
message = err.message;
};
break;
case "activatedevnotifications":
try{
const configData = require('./Commands/DevNotifications');
let allycode = configData(event.source.groupId , true, event.source.groupId );
}
catch(err){
message = err.message;
};
break;
case "deactivatedevnotifications":
try{
const configData = require('./Commands/DevNotifications');
let allycode = configData(event.source.groupId , false, event.source.groupId );
}
catch(err){
message = err.message;
};
break;
case "mods":
try{
let messageWithoutCommando = event.message.text.replace("#mods ","");
const ModRecommendation = require('./Commands/ModRecommendation');
ModRecommendation(event.source.groupId , messageWithoutCommando );
}
catch(err){
message = err.message;
};
break;
case "regeln":
message = "Dies sind die Regeln des Schattenkollektives:"
+ "\r\n- Wenn man am Territorialkrieg angemeldet ist muss man sich beteiligen"
+ "\r\n- In den Territorialschlachten ist das 3fache der eigenen GM einzusetzen"
+ "\r\n- In den TB und TW ist den Befehlen der Offizieren stets folge zu leisten"
+ "\r\n- Nach 7 Tagen inaktivitaet ohne vorherige abmeldung wird man der Gilde entfernt"
+ "\r\n- 2100 Raidtickets pro Woche sind zu erbringen"
+ "\r\n- Man muss ueber Line erreichbar sein. Wenn man nicht selbst in Line ist dann muss man zumindest ueber eine dritte Perosn die ueber Line verfuegt erreichbar sein"
+ "\r\n- Rancor Startzeit: 19:30"
+ "\r\n- Haat Startzeit: 20:00"
+ "\r\n- Sithraid: wann immer moeglich";
break;
case "twlineup":
message = "TW-Lineup: "
+ "\r\nO1 - min 95K"
+ "\r\nO2 - max 60k"
+ "\r\nO3 - max 200k (Flotte)"
+ "\r\nO4 - max 100k (Flotte)"
+ "\r\nU1 - 65k-75k"
+ "\r\nU2 - 80k-95k"
+ "\r\nU3 - 65k-75k"
+ "\r\nU4 - max 65k"
+ "\r\nM1 - 65k-75k"
+ "\r\nM2 - max 65k";
break;
case "allycode":
if(words.length > 1)
{
try{
const GetAllyCodeFromGuild = require('./Commands/GetAllyCodeFromGuild');
GetAllyCodeFromGuild(event.source.groupId , event.message.text.replace("#allycode ","") );
}catch(err){
message = err.message;
}
}
else
{
message = "please input the membername you search ( z.B. : #allycode sdtbarbarossa )";
}
break;
case "guild":
if(words.length > 1)
{
try{
let messageWithoutCommando = event.message.text.replace("#guild ","");
let allycodeNow = null;
let charList = [];
const GetGuildOverview = require('./Commands/GetGuildOverview');
if(messageWithoutCommando.indexOf("#") > 0)
{
allycodeNow = messageWithoutCommando.substr(0, (messageWithoutCommando.indexOf("#")-1));
console.log('allycodeNow', allycodeNow);
let charListNow = messageWithoutCommando.substr(messageWithoutCommando.indexOf("#")+1, (messageWithoutCommando.length-(messageWithoutCommando.indexOf("#")+1)));
console.log('charListNow', charListNow);
GetGuildOverview(event.source.groupId, allycodeNow, charListNow.split(","));
}
else{
GetGuildOverview(event.source.groupId, messageWithoutCommando, charList);
}
}
catch(err){
message = err.message;
}
}
else
{
message = "please give me the allycode of a guildmember ( z.B. : #guild 123456789 )";
}
break;
case "character":
if(words.length > 1)
{
try{
let messageWithoutCommando = event.message.text.replace("#character ","");
let allycodeNow = null;
let charList = [];
const GetCharFromGuild = require('./Commands/GetCharFromGuild');
console.log('allycodeNow', allycodeNow);
let charListNow = messageWithoutCommando;
console.log('charListNow', charListNow);
GetCharFromGuild(event.source.groupId, charListNow.split(","));
}
catch(err){
message = err.message;
}
}
else
{
message = "please give me the allycode of a guildmember ( z.B. : #guild 123456789 )";
}
break;
case "zeta":
try{
let messageWithoutCommando = event.message.text.replace("#zeta ","");
//to-do
let criteria = "";
criteria = ["pvp", "tw", "tb", "pit", "tank", "sith"].includes(messageWithoutCommando) ? messageWithoutCommando : 'sith';
const zeta = require('./Commands/Zeta');
let allycode = zeta(event.source.userId , event.source.groupId, criteria );
}
catch(err){
message = err.message;
}
break;
case "squads":
try{
let messageWithoutCommando = event.message.text.replace("#squads ","");
if(messageWithoutCommando.length == 0){
message = "please give me a criteria. for e.g. #squads sith 1";
}
else{
const SquadRecommendation = require('./Commands/SquadRecommendation');
let criterias = messageWithoutCommando.split(" ");
SquadRecommendation( event.source.groupId, criterias[0], criterias[1]);
}
}
catch(err){
message = err.message;
}
break;
case "lstb":
try{
let messageWithoutCommando = event.message.text.replace("#lstb ","");
if(messageWithoutCommando.length == 0){
message = "please give me a criteria. for e.g. #squads sith 1";
}
else{
const tb = require('./Commands/TB');
tb(event.source.userId , event.source.groupId, messageWithoutCommando, true);
}
}
catch(err){
message = err.message;
}
break;
case "dstb":
try{
let messageWithoutCommando = event.message.text.replace("#dstb ","");
if(messageWithoutCommando.length == 0){
message = "please give me a criteria. for e.g. #squads sith 1";
}
else{
const tb = require('./Commands/TB');
tb(event.source.userId , event.source.groupId, messageWithoutCommando, false);
}
}
catch(err){
message = err.message;
}
break;
case "help":
message = "Available commands: "
+ "\r\n #events"
+ "\r\n #heist"
+ "\r\n #addme {allycode}"
+ "\r\n #addguild {allycode}"
+ "\r\n #activateDevNotifications"
+ "\r\n #deactivateDevNotifications"
+ "\r\n #zeta {criteria}"
+ "\r\n #allycode membername"
+ "\r\n #guild {allycode} #{chracter}"
+ "\r\n #character {character}"
+ "\r\n #squads {raid} {phase}"
+ "\r\n #allycode membername"
+ "\r\n #help";
break;
default:
message = "Available commands: "
+ "\r\n #events"
+ "\r\n #heist"
+ "\r\n #addme {allycode}"
+ "\r\n #addguild {allycode}"
+ "\r\n #activateDevNotifications"
+ "\r\n #deactivateDevNotifications"
+ "\r\n #zeta {criteria}"
+ "\r\n #allycode membername"
+ "\r\n #guild {allycode} #{chracter}"
+ "\r\n #character {character}"
+ "\r\n #squads {raid} {phase}"
+ "\r\n #allycode membername"
+ "\r\n #help";
}
}
catch (err) {
message = "Error: " + err.message;
}
}
if (message == "") {
return;
}
return sendMessage(message, event.replyToken);
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function sendMessage(message, token) {
// create a echoing text message
const echo = { type: 'text', text: message };
// use reply API
return client.replyMessage(token, echo);
}
// listen on port
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`listening on ${port}`);
});