-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
210 lines (177 loc) · 6.04 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
import 'dotenv/config'
import * as fs from 'fs'
import TelegramBot from 'node-telegram-bot-api'
import { downloadMemes } from './dropbox.js'
import Jimp from 'jimp'
const { BOT_TOKEN, GROUP_ID, DROPBOX_LIBRARY } = process.env
const bot = new TelegramBot(BOT_TOKEN, { polling: true })
const lastSentMemes = []
let lastMessage = Date.now(), memeList = []
console.log('Tholdier bot started')
const allowedExtensions = [
{ ext: 'jpg', method: 'sendPhoto' },
{ ext: 'jpeg', method: 'sendPhoto' },
{ ext: 'png', method: 'sendPhoto' },
{ ext: 'gif', method: 'sendAnimation' },
{ ext: 'mp4', method: 'sendAnimation' },
]
const isChatAllowed = (fromId, chatId) => {
if (
fromId === chatId ||
chatId === Number(GROUP_ID)
) {
return true
}
}
const getMemeFile = msg => {
for (const { ext, method } of allowedExtensions) {
const filePath = memeList.find(meme => {
const hasFullName = meme === `${msg}.${ext}`
const hasShortName = meme.replaceAll('_', '') === `${msg}.${ext}`
return hasFullName || hasShortName
})
if (filePath) {
const file = fs.readFileSync(`memes/${filePath}`)
return { method, file }
}
}
}
bot.onText(/\/meme/, async ({
from: { id: fromId, username },
chat: { id: chatId },
text
}) => {
try {
if (isChatAllowed(fromId, chatId)) {
const splitMeme = text.split('meme').pop()
const firstCharMeme = splitMeme.substring(0, 1) === '_' ? splitMeme.replace('_', '') : splitMeme
const cleanMeme = firstCharMeme.toLowerCase()
const memeFile = getMemeFile(cleanMeme)
if (memeFile) {
await bot[memeFile.method](chatId, memeFile.file, { caption: `@${username}` })
}
}
} catch (error) {
console.log('sendmeme', error.stack)
}
})
bot.onText(/\/listmemes/, ({
from: { id: fromId, username },
chat: { id: chatId }
}) => {
try {
if (isChatAllowed(fromId, chatId)) {
let message = `<b>🪖 Hey tholdierth @${username}!</b>\n\n`
message += 'You can post the memes here both by calling it with '
message += 'or without the underscore (_) to call if faster. '
message += 'Just check the meme library, choose the meme you '
message += 'want and run the command here using the filename. \n\n'
message += '👉 Example: "Thend it.jpg" can be called using '
message += '/meme_thend_it and /memethendit \n\n'
message += `📚 Here is the full meme library: ${DROPBOX_LIBRARY} \n\n`
bot.sendMessage(chatId, message, {
disable_web_page_preview: true,
parse_mode : 'HTML'
})
}
} catch (error) {
console.log('listmemes', error.stack)
}
})
bot.onText(/\/downloadmemes/, async ({
from: { id: fromId, username },
chat: { id: chatId }
}) => {
try {
if (isChatAllowed(fromId, chatId)) {
bot.sendMessage(chatId, `@${username} starting downloading the new memes... `)
const { memesOnServer, downloaded, deleted } = await downloadMemes(memeList, allowedExtensions)
setTimeout(() => {
loadMemeList()
bot.sendMessage(chatId, `@${username} ${memesOnServer} memes found on server, ${downloaded} new memes downloaded and ${deleted} deleted`)
}, 5000)
}
} catch (error) {
console.log('downloadMemes', error.stack)
}
})
bot.onText(/./, () => lastMessage = Date.now())
// bot.onText(/\/homeless {1}.*/, async ({
// text,
// from: { id: fromId, username },
// chat: { id: chatId }
// }) => {
// try {
// if (isChatAllowed(fromId, chatId)) {
// const fullText = text.substring(10)
// const splitText = fullText.split(' ')
// const halfPos = Math.ceil(splitText.length / 2)
// const halfIndex = fullText.indexOf(splitText[halfPos])
// const firstText = fullText.substring(0, halfIndex).trim()
// const secondText = fullText.substring(halfIndex).trim()
// const tholmeleth = await Jimp.read('images/tholmeleth.png')
// const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK)
// const firstTextWidth = Jimp.measureText(font, firstText)
// const firstTextX = (474 - firstTextWidth) / 2
// await tholmeleth.print(font, firstTextX, 375, firstText)
// const secondTextWidth = Jimp.measureText(font, secondText)
// const secondTextX = (474 - secondTextWidth) / 2
// await tholmeleth.print(font, secondTextX, 415, secondText)
// const finalImage = await tholmeleth.getBufferAsync(Jimp.MIME_PNG)
// bot.sendPhoto(chatId, finalImage, { caption: `@${username}` })
// }
// } catch (error) {
// console.log('homeless', error.stack)
// }
// })
const commandByMemeFile = filename => {
const fileWithoutExt = filename.substr(0, filename.length - 4)
const command = `/meme_${fileWithoutExt}`
return command
}
const getRandomMeme = () => {
let randomMeme
while (!randomMeme) {
const randomIndex = Math.floor(Math.random() * memeList.length)
const possibleRandomMeme = memeList[randomIndex]
if (!lastSentMemes.includes(possibleRandomMeme)) {
randomMeme = possibleRandomMeme
}
}
if (lastSentMemes.length === 15) {
lastSentMemes.shift()
}
lastSentMemes.push(randomMeme)
return randomMeme
}
const sendRandomMeme = async () => {
try {
if (memeList.length) {
const randomMeme = getRandomMeme()
const memeExtension = randomMeme.substr(-3)
const memeFile = fs.readFileSync(`memes/${randomMeme}`)
console.log(randomMeme, memeExtension, memeFile)
const memeMethod = allowedExtensions.find(e => e.ext === memeExtension).method
if (memeFile) {
const command = commandByMemeFile(randomMeme)
await bot[memeMethod](GROUP_ID, memeFile, { caption: command })
}
}
} catch (error) {
console.log('sendRandomMeme', error.stack)
}
}
const loadMemeList = () => {
const memeFiles = fs.readdirSync('memes')
memeList = []
for (const memeFile of memeFiles) {
memeList.push(memeFile)
}
}
loadMemeList()
setInterval(async () => {
const msSinceLastMsg = Date.now() - lastMessage
if (msSinceLastMsg > 300000) {
await sendRandomMeme()
}
}, 300000)