-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtop-sets.js
109 lines (86 loc) · 3.35 KB
/
top-sets.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
const Telegram = require('telegraf/telegram')
const cron = require('node-cron')
const { atlasDb } = require('./database')
const { escapeHTML } = require('./utils')
const telegram = new Telegram(process.env.BOT_TOKEN)
const config = require('./config')
// Define a function to get the most popular sticker packs in a week
async function getPopularStickerPacks () {
const timeAgo = new Date().setDate(new Date().getDate() - 30)
const popularStickerPacks = await atlasDb
.StickerSet
.find({
'about.safe': true,
'installations.month': { $gt: 0 },
'reaction.total': { $gt: 5 },
publishDate: { $gte: timeAgo },
stickerChannel: { $exists: false },
$and: [
{ 'about.description': { $ne: null } },
{ 'about.description': { $ne: '' } }
]
})
.sort({ 'reaction.total': -1 })
.limit(1)
return popularStickerPacks
}
// Define a function to post the most popular sticker packs to a channel
async function postPopularStickerPacksToChannel () {
const popularStickerPacks = await getPopularStickerPacks()
for (const stickerPack of popularStickerPacks) {
const stickerSet = await telegram.getStickerSet(stickerPack.name)
let title = stickerSet.title
// remove (@username) or :: @username from the title
title = title.replace(/\s\(@\w+\)/, '')
title = title.replace(/::\s@\w+/, '')
title = title.replace(/@/, '')
let about = `${stickerPack.about.description}`
// remove Stickers from Stickers.Wiki from the about
about = about.replace(/Stickers from Stickers.Wiki/, '').trim()
const message = `<b>${escapeHTML(title)}</b>\n${escapeHTML(about)}`
// get random sticker from the sticker pack
const sticker = stickerSet.stickers[Math.floor(Math.random() * stickerSet.stickers.length)]
const bot = await telegram.getMe()
const stickerMessageId = await telegram.sendSticker(config.stickerChannelId, sticker.file_id, {
reply_markup: {
inline_keyboard: [
[
{
text: `👍 ${stickerPack.reaction.total}`,
url: `https://t.me/${bot.username}/catalog?startApp=set=${stickerSet.name}&startapp=set=${stickerSet.name}`
}
]
]
}
})
await telegram.sendMessage(config.stickerChannelId, message, {
parse_mode: 'HTML'
})
stickerPack.stickerChannel = {
messageId: stickerMessageId.message_id
}
await stickerPack.save()
}
}
if (config.stickerChannelId) {
cron.schedule('0 */2 * * *', () => postPopularStickerPacksToChannel()) // every 2 hours
const updateMessage = async () => {
const stickerPacks = await atlasDb.StickerSet.find({ 'stickerChannel.messageId': { $gt: 0 } })
for (const stickerPack of stickerPacks) {
const stickerSet = await telegram.getStickerSet(stickerPack.name)
const bot = await telegram.getMe()
const inlineKeyboard = [
{
text: `👍 ${stickerPack.reaction.total}`,
url: `https://t.me/${bot.username}/catalog?startApp=set=${stickerSet.name}&startapp=set=${stickerSet.name}`
}
]
await telegram.editMessageReplyMarkup(config.stickerChannelId, stickerPack.stickerChannel.messageId, null, {
inline_keyboard: [inlineKeyboard]
}).catch(() => {})
await new Promise(resolve => setTimeout(resolve, 3000))
}
updateMessage()
}
updateMessage()
}