-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
830 lines (755 loc) · 26.6 KB
/
index.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
const request = require('request');
const fs = require('fs');
const unzip = require('unzip2');
const sharp = require('sharp');
const TelegramBot = require('node-telegram-bot-api');
const config = require('./config.json');
const emojis = [
'😊',
'🙂',
'😋',
'😺',
'🐶',
'🐱',
'🐰',
'🦊',
'🐻',
'🐼',
'🐨',
'🐯',
'🦁',
'🐮',
'🐷',
'🐵',
'🐧',
'🐔',
'🦋',
];
let restring = 0;
setInterval(() => {
if (restring === 0) {
return;
}
if (restring < Date.now()) {
process.exit();
}
var sec = Math.floor((restring - Date.now()) / 1000);
if (sec % 5 === 0) {
console.log('Restart in ' + Math.floor((restring - Date.now()) / 1000) + ' seconds');
}
}, 1000);
const bot1 = new TelegramBot(config.token1, {
polling: true
});
const bot2 = new TelegramBot(config.token2);
const userCD = {};
bot1.on('message', (msg) => {
if (userCD[msg.from.id] !== undefined) {
if (Date.now() - userCD[msg.from.id] < 2 * 1000)
return;
}
userCD[msg.from.id] = Date.now();
console.log(msg);
if (msg.sticker !== undefined) {
var text = '您的使用者編號: <code>' + msg.from.id + '</code>\n';
if (msg.sticker.set_name !== undefined) {
text += '貼圖包編號: <code>' + msg.sticker.set_name + '</code>\n';
text += '貼圖表符: ' + msg.sticker.emoji + ' (<a href="http://telegra.ph/Sticker-emoji-06-03">編輯</a>)\n';
}
text += '貼圖大小: <b>' + msg.sticker.width + '</b>x<b>' + msg.sticker.height + '</b>\n';
bot1.sendMessage(msg.chat.id, text, {
reply_to_message_id: msg.message_id,
parse_mode: 'HTML'
});
if (msg.sticker.set_name !== undefined) {
var found = msg.sticker.set_name.match(/^line(\d+)_by_Sean_Bot$/);
if (found) {
const lid = found[1];
const meta = JSON.parse(fs.readFileSync('files/' + lid + '/metadata', 'utf8'));
bot2.getStickerSet(msg.sticker.set_name)
.then((set) => {
if (set.stickers.length !== meta.stickers.length) {
var text = '<a href="https://t.me/addstickers/' + msg.sticker.set_name + '">這包貼圖</a>怪怪的,要砍掉重練嗎?\n';
bot1.sendMessage(msg.chat.id, text, {
reply_to_message_id: msg.message_id,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '當然 砍了他 😈',
callback_data: 'remove_' + lid
}
]
]
}
});
}
});
}
}
return;
}
if (msg.text === undefined)
return;
if (msg.text === '/restart') {
if (config.admins.indexOf(msg.from.id) < 0)
return;
var text = '指令生效\n';
if (restring === 0) {
restring = Date.now() + 60 * 1000;
text += '已開啟停機模式';
} else {
restring = 0;
text += '已恢復正常模式';
}
bot1.sendMessage(msg.chat.id, text, {
reply_to_message_id: msg.message_id,
});
return;
}
if (msg.text == '/start SECRET') {
var text = '歡迎使用 LINE 貼圖轉換器\n';
text += '您已啟動完成,直接分享貼圖連結過來,就會自動下載囉~\n\n';
text += '如有任何疑慮,歡迎至<a href="https://t.me/StickerGroup">貼圖群</a>詢問';
bot1.sendMessage(msg.chat.id, text, {
parse_mode: 'HTML',
reply_to_message_id: msg.message_id,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{
text: '探索更多貼圖',
url: 'https://t.me/StickerGroup'
}
]
]
}
});
return;
}
if (msg.text == '/start edit_emoji') {
var text = '這邊有教學喔 :D\n';
text += 'http://telegra.ph/Sticker-emoji-06-03';
bot1.sendMessage(msg.chat.id, text, {
reply_to_message_id: msg.message_id,
});
return;
}
var found = msg.text.match(/(?:line.me\/(?:S\/sticker|stickershop\/product)\/|\/(?:line|start)[_ ]?)(\d+)/);
if (!found) {
if (msg.chat.id < 0)
return;
var text = '歡迎使用 LINE 貼圖轉換器\n';
text += '使用前,請先確定已啟動完成\n';
text += '本機器人由 <a href="https://t.me/SeanChannel">Sean</a> 提供';
bot1.sendMessage(msg.chat.id, text, {
parse_mode: 'HTML',
reply_to_message_id: msg.message_id,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{
text: '點我啟動',
url: 'https://t.me/' + config.botName2 + '?start=sticker_dl_start_' + msg.from.id
}
]
]
}
});
return;
}
if (msg.from.username === undefined) {
var text = '請先設定 username 喔 😃';
bot1.sendMessage(msg.chat.id, text, {
reply_to_message_id: msg.message_id,
reply_markup: {
inline_keyboard: [
[
{
text: '點我看教學',
url: 'https://t.me/UNameBot?start=Sean_LINE_bot'
}
]
]
}
});
return;
}
const lid = found[1];
if (!fs.existsSync('files/' + lid)) {
fs.mkdirSync('files/' + lid);
}
if (fs.existsSync('files/' + lid + '/metadata')) {
const meta = JSON.parse(fs.readFileSync('files/' + lid + '/metadata', 'utf8'));
meta.error = [];
if (meta.done !== undefined) {
if (meta.done.length == meta.stickers.length) {
text = '<a href="https://t.me/addstickers/' + meta.name + '">' + enHTML(meta.title) + '</a> 已存在';
bot1.sendMessage(msg.chat.id, text, {
message_id: msg.msgId,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '點我安裝',
url: 'https://t.me/addstickers/' + meta.name
}
],
[
{
text: '分享給朋友',
url: 'https://t.me/share/url'
+ '?url=' + encodeURIComponent('https://t.me/addstickers/' + meta.name)
+ '&text=' + encodeURIComponent(meta.title + '\n\n一起用 @' + config.botName1 + ' 把貼圖搬運來吧~')
}
]
]
}
})
.then((result) => {
msg.msgId = result.message_id;
bot2.getStickerSet('line' + lid + '_by_' + config.botName2)
.then((set) => {
if (set.stickers.length !== meta.stickers.length) {
var text = '前次下載失敗,請先試試看<a href="https://t.me/addstickers/' + meta.name + '">這包貼圖</a>\n';
text += '如有問題,就砍掉重練吧 :D\n';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '砍掉重練 😈',
callback_data: 'remove_' + lid
}
]
]
}
});
}
});
return;
});
}
var stat1 = fs.statSync('files/' + lid);
var stat2 = fs.statSync('files/' + lid + '/metadata');
var mtime = Math.max(stat1.mtimeMs, stat2.mtimeMs);
var sec = Math.floor((mtime - Date.now()) / 1000) + 60;
if (sec > 0) {
var text = '已中斷下載\n'
text += '可能原因: 他人正在下載同款貼圖包\n';
if (meta.done != undefined) {
text += prog(meta.done.length, meta.stickers.length);
}
text += '\n冷卻時間: <b>' + sec + '</b> 秒\n';
text += '點擊 /line_' + lid + ' 指令重試\n';
bot1.sendMessage(msg.chat.id, text, {
parse_mode: 'HTML',
reply_to_message_id: msg.message_id,
});
return;
}
var text = '路邊撿到半包貼圖,接續上傳 💪\n';
if (meta.done.length > 0) {
text += prog(meta.done.length, meta.stickers.length);
if (meta.done.length / meta.stickers.length >= 0.7) {
text += '預覽連結: <a href="https://t.me/addstickers/' + meta.name + '">' + enHTML(meta.title) + '</a>\n';
}
}
bot1.sendMessage(msg.chat.id, text, {
parse_mode: 'HTML',
reply_to_message_id: msg.message_id,
})
.then((result) => {
msg.msgId = result.message_id;
uploadBody(msg, lid);
});
return;
}
}
var text = '準備下載 <a href="https://store.line.me/stickershop/product/' + lid + '/zh-Hant">此貼圖</a>...';
msg.timestamp = Date.now();
bot1.sendMessage(msg.chat.id, text, {
parse_mode: 'HTML',
reply_to_message_id: msg.message_id,
disable_web_page_preview: true
})
.then((result) => {
msg.msgId = result.message_id;
if (restring > 0) {
var text = '⚠️ 機器人要下班了\n\n';
text += '機器人已排程重啟,為了維護貼圖包品質,將拒收新貼圖\n';
text += '請過 <b>' + Math.floor((restring - Date.now()) / 1000) + '</b> 秒後再點 /line_' + lid + ' 開始下載\n\n';
text += '如有造成不便,我也不能怎樣 ¯\\_(ツ)_/¯';
bot1.editMessageText(id, text, {
chat_id: msg.chat.id,
messsage_id: msg.msgId,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '先來去逛街',
url: 'https://t.me/StickerGroup'
}
]
]
}
});
return;
}
var zipname = 'files/' + lid + '/file.zip';
request('http://dl.stickershop.line.naver.jp/products/0/0/1/' + lid + '/iphone/[email protected]')
.on('error', function (err) {
msg.timestamp = Date.now() + 9487 * 1000;
var text = '發生錯誤,已中斷下載\n';
text += '詳細報告: NodeJS <b>request</b> onError\n';
text += '<pre>' + enHTML(JSON.stringify(err)) + '</pre>';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
})
.pipe(fs.createWriteStream(zipname))
.on('finish', (result) => {
if (msg.timestamp > Date.now())
return;
var zipStat = fs.statSync(zipname);
if (zipStat.size < 69) {
var zipText = fs.readFileSync(zipname);
msg.timestamp = Date.now() + 9487 * 1000;
var text = '發生錯誤,已中斷下載\n';
text += '詳細報告: LINE 伺服器提供檔案不正常\n';
text += '下載內容:\n'
text += '<pre>' + enHTML(zipText) + '</pre>';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
return;
}
try {
fs.createReadStream(zipname)
.pipe(unzip.Parse())
.on('entry', function (entry) {
var fileName = entry.path;
if (fileName == 'productInfo.meta') {
entry.pipe(fs.createWriteStream('files/' + lid + '/metadata'));
return;
}
if (/\[email protected]/.test(fileName)) {
entry.pipe(fs.createWriteStream('files/' + lid + '/origin-' + fileName.replace('@2x', '')));
return;
}
if (/(\d+_key|tab_(on|off))@2x.png/.test(fileName)) {
entry.autodrain();
return;
}
entry.pipe(fs.createWriteStream('files/' + lid + '/UNKNOWN-' + fileName));
})
.on('finish', () => {
const meta = JSON.parse(fs.readFileSync('files/' + lid + '/metadata', 'utf8'));
meta.error = [];
meta.name = 'line' + lid + '_by_' + config.botName2;
const langs = [
'zh-Hant',
'ja',
'zh-Hans',
'en',
'ko'
];
langs.some(function (val) {
if (meta['title'][val] !== undefined) {
meta['lang'] = val;
return true;
}
});
meta.origin_title = meta.title;
meta.title = meta['title'][meta.lang];
meta.emoji = emojis[Math.floor(Math.random() * emojis.length)];
var text = '已取得 <a href="https://store.line.me/stickershop/product/' + lid + '/' + meta['lang'] + '">' + enHTML(meta.title) + '</a> 資訊...\n';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
const sid = meta.stickers[0].id;
const origin = 'files/' + lid + '/origin-' + sid + '.png';
const sticker = 'files/' + lid + '/sticker-' + sid + '.png';
sharp(origin)
.resize(512, 512)
.max()
.toFile(sticker)
.catch((error) => {
console.error(error);
msg.timestamp = Date.now() + 9487 * 1000;
meta.error.push(sid);
var text = '發生錯誤,已中斷下載\n';
text += '問題來源: NodeJS <b>sharp</b> (圖片轉檔工具)\n';
text += '詳細報告: \n';
text += '<pre>' + enHTML(JSON.stringify(error)) + '</pre>';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
})
.then((data) => {
if (msg.timestamp > Date.now())
return;
bot2.createNewStickerSet(msg.from.id, meta.name, meta.title + " @SeanChannel", sticker, meta.emoji)
.catch((error) => {
msg.timestamp = Date.now() + 9487 * 1000;
meta.error.push(sid);
if (error.message.includes('sticker set name is already occupied')) {
var text = '前次下載失敗,請先試試看<a href="https://t.me/addstickers/' + meta.name + '">這包貼圖</a>';
text += '如有問題,就點按鈕修復吧 :D\n';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '給他好看 👻',
callback_data: 'remove_' + lid
}
]
]
}
});
return;
}
var text = '發生錯誤,已中斷下載\n';
if (error.message.includes('user not found')) {
text += '請確定 <a href="https://t.me/' + config.botName2 + '">已於此啟動過機器人</a>\n';
}
text += '詳細報告: createNewStickerSet\n';
text += '<pre>' + enHTML(JSON.stringify(error)) + '</pre>';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
disable_web_page_preview: true,
parse_mode: 'HTML'
});
})
.then((result) => {
if (msg.timestamp > Date.now())
return;
if (meta.error.indexOf(sid) < 0) {
meta.done = [ sid ];
fs.writeFileSync('files/' + lid + '/metadata', JSON.stringify(meta));
var text = '上傳 <a href="https://store.line.me/stickershop/product/' + lid + '/' + meta['lang'] + '">' + enHTML(meta.title) + '</a> 中...\n';
text += prog(meta.done.length, meta.stickers.length);
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
uploadBody(msg, lid);
}
fs.appendFile('files/' + lid + '/request', JSON.stringify(msg), (error) => { console.error(error) });
});
});
});
} catch (err) {
console.error(err);
var text = '發生錯誤,已中斷下載\n';
text += '詳細報告: UNKNOWN (catch)\n';
text += '<pre>' + enHTML(error) + '</pre>';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
return;
}
});
});
});
function uploadBody(msg, lid) {
if (restring > 0) {
var text = '⚠️ 機器人要下班了\n\n';
text += '機器人已排程重啟,為了維護貼圖包品質,將不再新增貼圖\n';
text += '請過 <b>' + Math.floor((restring - Date.now()) / 1000) + '</b> 秒後再點 /line_' + lid + ' 開始下載\n\n';
text += '如有造成不便,我也不能怎樣 ¯\\_(ツ)_/¯';
bot1.editMessageText(id, text, {
chat_id: msg.chat.id,
messsage_id: msg.msgId,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '先來去逛街',
url: 'https://t.me/StickerGroup'
}
]
]
}
});
return;
}
const meta = JSON.parse(fs.readFileSync('files/' + lid + '/metadata', 'utf8'));
if (meta.emoji === undefined) {
meta.emoji = emojis[0];
}
meta.error = [];
for (let i = 0; i < meta.stickers.length; i++) {
const sid = meta.stickers[i].id;
if (meta.done.indexOf(sid) > -1)
continue;
const origin = 'files/' + lid + '/origin-' + sid + '.png';
const sticker = 'files/' + lid + '/sticker-' + sid + '.png';
sharp(origin)
.resize(512, 512)
.max()
.toFile(sticker)
.catch((error) => {
console.error(error);
msg.timestamp = Date.now() + 9487 * 1000;
meta.error.push(sid);
var text = '發生錯誤,已中斷下載\n';
text += '問題來源: NodeJS <b>sharp</b> (圖片轉檔工具)\n';
text += '詳細報告: \n';
text += '<pre>' + enHTML(JSON.stringify(error)) + '</pre>';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
})
.then((data) => {
bot2.addStickerToSet(msg.from.id, meta.name, sticker, meta.emoji)
.catch((error) => {
meta.error.push(sid);
if (Date.now() < msg.timestamp)
return;
msg.timestamp = Date.now() + 9487 * 1000;
var text = '發生錯誤,請五分鐘後再試\n';
if (error.message.includes('user not found')) {
text += '請確定 <a href="https://t.me/' + config.botName2 + '">已於此啟動過機器人</a>\n';
text += '點擊 /line_' + lid + ' 重試\n';
} else if (error.message.includes('retry after')) {
text += '上傳速度太快啦,TG 伺服器要冷卻一下\n';
text += '點擊 /line_' + lid + ' 重試\n';
} else if (error.message.includes('STICKERS_TOO_MUCH')) {
text += '貼圖數量衝破天際啦~\n';
}
text += prog(meta.done.length, meta.stickers.length);
text += '貼圖包連結: <a href="https://t.me/addstickers/' + meta.name + '">' + enHTML(meta.title) + '</a>\n';
text += '詳細報告: addStickerToSet\n';
text += '<pre>' + enHTML(JSON.stringify(error)) + '</pre>';
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
})
.then((result) => {
if (meta.error.indexOf(sid) > -1)
return;
meta.done.push(sid);
if (Date.now() < msg.timestamp)
return;
if (meta.done.length == meta.stickers.length) {
var text = '上傳完成!\n';
text += '共 <b>' + meta.stickers.length + '</b> 張貼圖\n';
text += '安裝連結: <a href="https://t.me/addstickers/' + meta.name + '">' + enHTML(meta.title) + '</a>\n';
if (meta.stickerResourceType !== undefined && meta.stickerResourceType !== 'STATIC') {
text += 'PS. 移植後,動態/有聲貼圖將僅保留圖片';
}
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '點我安裝',
url: 'https://t.me/addstickers/' + meta.name
},
{
text: '編輯表符',
callback_data: 'done_edit_emoji_' + meta.name
}
],
[
{
text: '分享給朋友',
url: 'https://t.me/share/url'
+ '?url=' + encodeURIComponent('https://t.me/addstickers/' + meta.name)
+ '&text=' + encodeURIComponent(meta.title + '\n剛出爐的呦~')
}
]
]
}
});
bot2.getStickerSet(msg.sticker.set_name)
.then((set) => {
if (set.stickers.length !== meta.stickers.length) {
var text = '<a href="https://t.me/addstickers/' + msg.sticker.set_name + '">' + enHTML(meta.title) + '</a>怪怪的\n';
text += '要重新下載嗎?';
bot1.sendMessage(msg.chat.id, text, {
reply_to_message_id: msg.message_id,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '重來一次 😅',
callback_data: 'remove_' + lid
}
]
]
}
});
}
});
} else if (Date.now() - msg.timestamp > 3 * 1000) {
msg.timestamp = Date.now();
var text = '上傳 <a href="https://store.line.me/stickershop/product/' + lid + '/' + meta['lang'] + '">' + enHTML(meta.title) + '</a> 中...\n';
text += prog(meta.done.length, meta.stickers.length);
if (meta.done.length / meta.stickers.length >= 0.7) {
text += '預覽連結: <a href="https://t.me/addstickers/' + meta.name + '">' + enHTML(meta.title) + '</a>\n';
}
bot1.editMessageText(text, {
chat_id: msg.chat.id,
message_id: msg.msgId,
parse_mode: 'HTML'
});
}
})
.finally(() => {
fs.writeFile('files/' + lid + '/metadata', JSON.stringify(meta), (error) => { if (error) console.error(error) });
});
});
}
}
bot1.on('callback_query', (query) => {
if (userCD[query.from.id] !== undefined) {
if (Date.now() - userCD[query.from.id] < 2 * 1000)
return;
}
userCD[query.from.id] = Date.now();
if (query.data.startsWith('remove_')) {
lid = query.data.substr(7);
const meta = JSON.parse(fs.readFileSync('files/' + lid + '/metadata', 'utf8'));
meta.name = 'line' + lid + '_by_' + config.botName2;
fs.writeFile('files/' + lid + '/metadata', JSON.stringify(meta), (error) => { if (error) console.error(error) }); // Prevent collision
bot2.getStickerSet('line' + lid + '_by_' + config.botName2)
.then((set) => {
if (meta.done !== undefined) {
var text = '';
var stat1 = fs.statSync('files/' + lid);
var stat2 = fs.statSync('files/' + lid + '/metadata');
if (Date.now() - stat1.mtimeMs < 1 * 60 * 1000
|| Date.now() - stat2.mtimeMs < 1 * 60 * 1000) {
text = '冷卻中,請三分鐘後再點一次';
}
if (meta.stickers.length !== meta.done.length
|| meta.stickers.length === set.stickers.length) {
text = '看起來沒問題呀\n如真的怪怪的,請至群組提出'
}
if (config.admins.indexOf(query.from.id) > -1) {
text = '';
}
if (text !== '') {
bot1.answerCallbackQuery(query.id, {
text: text,
show_alert: 'true'
});
return;
}
}
console.warn(query);
if (meta.origin_title === undefined) {
const langs = [
'zh-Hant',
'ja',
'zh-Hans',
'en',
'ko'
];
langs.some(function (val) {
if (meta['title'][val] !== undefined) {
meta['lang'] = val;
return true;
}
});
meta.origin_title = meta.title;
meta.title = set.title.replace(/ +@SeanChannel/, '');
}
if (meta.emoji === undefined) {
meta.emoji = emojis[Math.floor(Math.random() * emojis.length)];
}
meta.done = [];
for (var i=0; i<set.stickers.length; i++) {
bot2.deleteStickerFromSet(set.stickers[i].file_id);
}
if (!query.message.text.includes('已清空')) {
var text = '已清空 <a href="https://t.me/addstickers/' + meta.name + '">' + meta.title + '</a>\n';
text += '確認顯示「找不到貼圖包」後,請等待三分鐘,並點擊 /line_' + lid + ' 重新下載指令';
bot1.editMessageText(text, {
chat_id: query.message.chat.id,
message_id: query.message.message_id,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[
{
text: '他不是空的 😰',
callback_data: 'remove_' + lid
}
]
]
}
});
}
fs.writeFile('files/' + lid + '/metadata', JSON.stringify(meta), (error) => { if (error) console.error(error) });
bot1.answerCallbackQuery(query.id, {
text: '處理完成 👌',
show_alert: 'true'
});
});
}
if (query.data.startsWith('done_edit_emoji_')) {
var text = '這邊有教學喔 :D\n';
text += 'http://telegra.ph/Sticker-emoji-06-03';
bot1.sendMessage(msg.chat.id, text, {
reply_to_message_id: query.message.message_id
});
bot1.answerCallbackQuery(query.id, {
text: '您的貼圖編號: ' + query.data.substr(16),
show_alert: 'true'
});
}
});
function enHTML(str) {
var s = str + '';
return s.replace('&', '&')
.replace('"', '"')
.replace('<', '<')
.replace('>', '>');
}
function prog(current, total) {
if (current > total) {
current = total;
}
const count = 20;
var str = '下載進度: <b>' + current + '</b>/' + total + ' <code>[';
str += '█'.repeat(Math.round(current * count / total))
str += '-'.repeat(count - Math.round(current * count / total))
str += ']</code>\n'
return str;
}