This commit is contained in:
Lukian LEIZOUR 2022-11-26 16:51:33 +01:00
parent 5d6dbadc82
commit fb13e08004

20
app.js
View file

@ -60,7 +60,7 @@ function r34sTag(query, ctx) {
}); });
resp.on('end', () => { resp.on('end', () => {
res = JSON.parse(data); res = JSON.parse(data);
message = "Tags for the query: " + query + "\n" ; message = "Tags for the query " + query + " :\n" ;
if (res.length == 0) { if (res.length == 0) {
console.log("--> no tags found for the query: " + query); console.log("--> no tags found for the query: " + query);
@ -68,10 +68,10 @@ function r34sTag(query, ctx) {
bot.telegram.sendMessage(ctx.chat.id, "No tags found for the query: " + query, {}); bot.telegram.sendMessage(ctx.chat.id, "No tags found for the query: " + query, {});
} else { } else {
for (var i = 0; i < res.length; i++) { for (var i = 0; i < res.length; i++) {
message += "\n - " + res[i].value; message += "\n - `" + res[i].value+"`";
} }
message += "\n\nUse /r34 <tag> to get a random image for the tag"; message += "\n\nUse `/r34 <tag>` to get a random image for the tag" + "\nExample: `/r34 " + res[0].value + "`";
bot.telegram.sendMessage(ctx.chat.id, message, {}); bot.telegram.sendMessage(ctx.chat.id, message, {parse_mode: "Markdown"});
console.log("--> sent the tags for the query: " + query); console.log("--> sent the tags for the query: " + query);
addToLogs("--> sent the tags for the query: " + query); addToLogs("--> sent the tags for the query: " + query);
} }
@ -93,6 +93,12 @@ function r34(tag, ctx) {
data += chunk; data += chunk;
}); });
resp.on('end', () => { resp.on('end', () => {
//test if data is empty
if (data == "") {
console.log("--> no images found for the query: " + tag);
addToLogs("--> no images found for the query: " + tag);
bot.telegram.sendMessage(ctx.chat.id, "No images found for the query: " + tag + "\nUse /r34tag command to find a tag before searching an image", {});
} else {
res = JSON.parse(data); res = JSON.parse(data);
bot.telegram.sendPhoto(ctx.chat.id, res[Math.floor(Math.random() * res.length)].file_url, {"caption": "This is a random image for the tag : " + tag}).catch(err => { bot.telegram.sendPhoto(ctx.chat.id, res[Math.floor(Math.random() * res.length)].file_url, {"caption": "This is a random image for the tag : " + tag}).catch(err => {
@ -100,10 +106,13 @@ function r34(tag, ctx) {
addToLogs("--> error : " + err); addToLogs("--> error : " + err);
bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {});
}); });
}
}); });
}).on("error", (err) => { }).on("error", (err) => {
console.log("Error: " + err.message); console.log("Error: " + err.message);
addToLogs("--> error : " + err);
bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {});
}); });
} }
@ -175,5 +184,8 @@ bot.command('r34', ctx => {
r34(ctx.message.text.slice(+5), ctx) r34(ctx.message.text.slice(+5), ctx)
}) })
bot.command('test', ctx => {
bot.telegram.sendMessage(ctx.chat.id, "`test`", {parse_mode: 'Markdown'})
})
//bot launch //bot launch
bot.launch(); bot.launch();