-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinterface.js
executable file
·203 lines (174 loc) · 8.48 KB
/
interface.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
const { MessageMedia, Message, Client } = require('whatsapp-web.js');
const got = require('got')
const { promisify } = require('util');
const exec = promisify(require('child_process').exec)
const fs = require('fs');
require('dotenv').config()
const TG_BOT_API = process.env.TG_BOT_API || null;
if (!fs.existsSync('./modules/sticker/tmp/')) {
// If it doesn't exist, create it
fs.mkdirSync('./modules/sticker/tmp/');
console.log(`Folder './modules/sticker/tmp/' created.`);
}
class Module {
/** @type {string} */
name = 'Sticker'
/** @type {string} */
description = 'Create stickers.'
/** @type {JSON} */
commands = {
'sticker': `Reply or send media with ${process.env.PREFIX}sticker, also supports Telegram sticker packs. For a custom name and author use\n${process.env.PREFIX}sticker packName-authorName`,
'steal': 'Reply to a sticker to steal it.',
'image': 'Converts the sticker to image.',
'square': 'Get back the media as 1:1 cropped sticker.'
};
/**
* @param {string} msg_string
* @param {Message} msg
*/
async fun(msg_string, msg) {
//regex to extract the stickername and stickerauthor from message text
let stkName = 'bonk!';
let stkAuth = 'cheems';
const regxmatch = msg_string.match(/ (.+)\-(.+)/);
if (regxmatch) {
stkName = regxmatch[1];
stkAuth = regxmatch[2];
}
// console.log(regxmatch);
console.log(stkName, stkAuth);
let media = await msg.downloadMedia();
console.log(media.mimetype);
if (msg_string.includes(`${process.env.PREFIX}sticker`) || msg_string.includes(`${process.env.PREFIX}steal`)) {
await msg.reply(media, msg.from, { sendMediaAsSticker: true, stickerName: stkName, stickerAuthor: stkAuth })
}
if (msg_string.includes(`${process.env.PREFIX}square`)) {
let randno = (Math.random() * 100 + 100).toString();
let filextension = media.mimetype.split('/')[1];
let filepath = "./modules/sticker/tmp/" + randno + '.' + filextension;
let squarefilepath = "./modules/sticker/tmp/" + 'square_' + randno + '.' + filextension;
console.log("filesize:", media.filesize);
fs.writeFile(filepath, media.data, "base64",
function (err) {
if (err) {
console.log(err);
}
}
);
try {
await exec(`ffmpeg -to 00:00:05 -i ${filepath} -vf "crop=w='min(iw,ih)':h='min(iw,ih)'" -movflags "frag_keyframe+empty_moov" -y ${squarefilepath}`);
media = MessageMedia.fromFilePath(squarefilepath);
await msg.reply(media, msg.from, { sendMediaAsSticker: true, stickerName: stkName, stickerAuthor: stkAuth })
fs.rmSync(filepath)
fs.rmSync(squarefilepath)
}
catch (err) {
console.log(err);
if (fs.existsSync(filepath)) {
fs.rmSync(filepath)
}
if (fs.existsSync(squarefilepath)) {
fs.rmSync(squarefilepath)
}
await msg.reply("Corrupted file.", msg.from, { sendMediaAsSticker: true, stickerName: stkName, stickerAuthor: stkAuth })
}
}
if (msg_string.includes(`${process.env.PREFIX}image`)) {
await msg.reply(media, msg.from)
}
}
/**
* @param {Client} client
* @param {Message} msg
*/
async operate(client, msg) {
//if the message has url
const urlRegex = /(https?|ftp):\/\/[^\s/$.?#].[^\s]*/i;
const tgRegex = /https:\/\/(t|telegram)\.me\/addstickers\/(\w+)( +(\d+))?/g
var matches = msg.body.match(urlRegex);
if (matches) {
//TG sticker pack check
let tgMatch = msg.body.matchAll(tgRegex)
let matchList = [...tgMatch]
if (matchList[0]) {
let maxcount = 200
if (matchList[0][4])
maxcount = parseInt(matchList[0][4])
//check if bot_token is available
if (!TG_BOT_API) {
msg.reply("TG_BOT_API ENV variable is not set.", msg.from)
return;
}
let packname = matchList[0][2];
console.log("packname:", packname);
let response = await got(`https://api.telegram.org/bot${TG_BOT_API}/getStickerSet?name=${packname}`);
let packjson = JSON.parse(response.body);
if (packjson['result']['is_animated']) {
msg.reply("Animated sticker packs are not supported. Use only static or video sticker-packs for telegram.", msg.from)
return;
}
for (var i = 0; i < Math.min(maxcount, packjson['result']['stickers'].length); i++) {
let sticker = packjson['result']['stickers'][i];
let stickeres = await got(`https://api.telegram.org/bot${TG_BOT_API}/getFile?file_id=${sticker['file_id']}`);
let stickerjson = JSON.parse(stickeres.body);
console.log(stickerjson['result']['file_path']);
if (stickerjson['result']['file_path'].includes('webp')) {
console.log("webp sticker");
let media = await MessageMedia.fromUrl(`https://api.telegram.org/file/bot${TG_BOT_API}/${stickerjson['result']['file_path']}`, { unsafeMime: true });
await client.sendMessage(msg.from, media, { sendMediaAsSticker: true, stickerName: 'bonk!', stickerAuthor: 'cheems' });
await new Promise(resolve => setTimeout(resolve, 1234));
}
else {
console.log("Not .webp");
let filename = stickerjson['result']['file_path'].split('/')[1];
let filepath = "./modules/sticker/tmp/" + filename;
let newpath = "./modules/sticker/tmp/" + filename + '.webp';
const data = await got(`https://api.telegram.org/file/bot${TG_BOT_API}/${stickerjson['result']['file_path']}`, { responseType: 'buffer' });
// Save the response body to a file
fs.writeFileSync(filepath, data.body);
// await exec(`ffmpeg -i "${filepath}" -c libwebp -y "${newpath}"`);
await exec(`ffmpeg -c:v libvpx-vp9 -i "${filepath}" -y "${newpath}"`);
// await exec(`ffmpeg -i "${filepath}" -c libwebp -y "${newpath}"`);
let media = MessageMedia.fromFilePath(newpath);
console.log("Sticker size, ", media.mimetype, media.filesize);
try {
await client.sendMessage(msg.from, media, { sendMediaAsSticker: true, stickerName: 'bonk!', stickerAuthor: 'cheems' });
}
catch (err) {
console.log("Failed sending the sticker,", err);
}
if (fs.existsSync(filepath)) {
fs.rmSync(filepath)
}
if (fs.existsSync(newpath)) {
fs.rmSync(newpath)
}
}
}
}
//Bad practice therefore removed
// else {
// let media = await MessageMedia.fromUrl(url);
// await msg.reply(media, msg.from, { sendMediaAsSticker: true, stickerName: 'bonk!', stickerAuthor: 'cheems' })
// }
}
//if the message has media
if (msg.hasMedia) {
this.fun(msg.body, msg)
.catch(err => {
throw (err)
});
}
// if the command is replied to media
if (msg.hasQuotedMsg) {
let quoted = await msg.getQuotedMessage();
if (quoted.hasMedia) {
this.fun(msg.body, quoted)
.catch(err => {
throw (err)
})
}
}
}
}
module.exports = Module