From 75466659e726ba7e9ae2ffb73a3c1e07f4fe660c Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Sun, 27 Nov 2022 16:13:25 +0100 Subject: [PATCH] commit --- app.js | 92 +++++++++-------------------------------------------- botTools.js | 14 ++++++++ games.js | 5 +++ rule34.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 77 deletions(-) create mode 100644 botTools.js create mode 100644 games.js create mode 100644 rule34.js diff --git a/app.js b/app.js index 70a61c8..3418205 100644 --- a/app.js +++ b/app.js @@ -3,11 +3,17 @@ const google = require('googlethis'); const fs = require('fs'); const https = require('https'); +const r34 = require('./rule34'); +const { addToLogs } = require('./botTools'); + //bot initialization const bot = new Telegraf(process.env.TELEGRAM); //functions function image_search(query, ctx) { + // + //Search for an image on google and send it to the user + // const images = google.image(query, { safe: false }).catch(err => { console.log(err); addToLogs("--> error : " + err); @@ -26,6 +32,9 @@ function image_search(query, ctx) { } function isTrue(message, ctx) { + // + //Check if the message is a command + // if (message != undefined) { console.log("--> message received: " + message); addToLogs("--> message received: " + message); @@ -49,81 +58,6 @@ function isTrue(message, ctx) { } } -function r34sTag(query, ctx) { - console.log("--> r34sTag query: " + query); - addToLogs("--> r34sTag query: " + query); - https.get("https://rule34.xxx/public/autocomplete.php?q=" + query, (resp) => { - let data = ''; - - resp.on('data', (chunk) => { - data += chunk; - }); - resp.on('end', () => { - res = JSON.parse(data); - message = "Tags for the query " + query + " :\n" ; - - if (res.length == 0) { - console.log("--> no tags found for the query: " + query); - addToLogs("--> no tags found for the query: " + query); - bot.telegram.sendMessage(ctx.chat.id, "No tags found for the query: " + query, {}); - } else { - for (var i = 0; i < res.length; i++) { - message += "\n - `" + res[i].value+"`"; - } - message += "\n\nUse `/r34 ` to get a random image for the tag" + "\nExample: `/r34 " + res[0].value + "`"; - bot.telegram.sendMessage(ctx.chat.id, message, {parse_mode: "Markdown"}); - console.log("--> sent the tags for the query: " + query); - addToLogs("--> sent the tags for the query: " + query); - } - }); - - }).on("error", (err) => { - console.log(err); - addToLogs("--> error : " + err); - }) -} - -function r34(tag, ctx) { - console.log("--> r34 query: " + tag); - addToLogs("--> r34 query: " + tag); - https.get('https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&tags=' + tag, (resp) => { - let data = ''; - - resp.on('data', (chunk) => { - data += chunk; - }); - 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); - - 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 => { - console.log(err); - addToLogs("--> error : " + err); - bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); - }); - } - }); - - }).on("error", (err) => { - console.log("Error: " + err.message); - addToLogs("--> error : " + err); - bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); - }); -} - -function addToLogs(message) { - fs.appendFile('./logs/logs.txt', message + "\n", err => { - if (err) { - console.log(err); - } - }); -} - //bot commands bot.command('start', ctx => { bot.telegram.sendMessage(ctx.chat.id, 'hello there! Welcome to my new telegram bot.\nType /help for help.', {}) @@ -169,11 +103,15 @@ bot.command('suggest', ctx => { }) bot.command('rtag', ctx => { - r34sTag(ctx.message.text.slice(+6), ctx) + r34.rtag(ctx.message.text.slice(+6), ctx, bot) }) bot.command('r34', ctx => { - r34(ctx.message.text.slice(+5), ctx) + r34.r34(ctx.message.text.slice(+5), ctx, bot) +}) + +bot.command('rps', ctx => { + rockPaperCiscors(ctx.message.text.slice(+5), ctx) }) //bot launch diff --git a/botTools.js b/botTools.js new file mode 100644 index 0000000..72b592a --- /dev/null +++ b/botTools.js @@ -0,0 +1,14 @@ +const fs = require('fs'); + +function addToLogs(message) { + // + //Add a message to the logs + // + fs.appendFile('./logs/logs.txt', message + "\n", err => { + if (err) { + console.log(err); + } + }); +} + +module.exports = { addToLogs }; \ No newline at end of file diff --git a/games.js b/games.js new file mode 100644 index 0000000..f9137c2 --- /dev/null +++ b/games.js @@ -0,0 +1,5 @@ +function rockPaperScissors() { + // Your code here +} + +module.exports = { rockPaperScissors }; \ No newline at end of file diff --git a/rule34.js b/rule34.js new file mode 100644 index 0000000..a6cb789 --- /dev/null +++ b/rule34.js @@ -0,0 +1,81 @@ +// +// Rule34 Fuctions for the Rule34 Bot +// + +const https = require('https'); + +const { addToLogs } = require('./botTools'); + +function rtag(query, ctx, bot) { + // + //Search for a tag on r34 + // + console.log("--> r34sTag query: " + query); + addToLogs("--> r34sTag query: " + query); + https.get("https://rule34.xxx/public/autocomplete.php?q=" + query, (resp) => { + let data = ''; + + resp.on('data', (chunk) => { + data += chunk; + }); + resp.on('end', () => { + res = JSON.parse(data); + message = "Tags for the query " + query + " :\n" ; + + if (res.length == 0) { + console.log("--> no tags found for the query: " + query); + addToLogs("--> no tags found for the query: " + query); + bot.telegram.sendMessage(ctx.chat.id, "No tags found for the query: " + query, {}); + } else { + for (var i = 0; i < res.length; i++) { + message += "\n - `" + res[i].value+"`"; + } + message += "\n\nUse `/r34 ` to get a random image for the tag" + "\nExample: `/r34 " + res[0].value + "`"; + bot.telegram.sendMessage(ctx.chat.id, message, {parse_mode: "Markdown"}); + console.log("--> sent the tags for the query: " + query); + addToLogs("--> sent the tags for the query: " + query); + } + }); + + }).on("error", (err) => { + console.log(err); + addToLogs("--> error : " + err); + }) +} + +function r34(tag, ctx, bot) { + // + //Search for the tag on r34 and send a random image + // + console.log("--> r34 query: " + tag); + addToLogs("--> r34 query: " + tag); + https.get('https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&json=1&tags=' + tag, (resp) => { + let data = ''; + + resp.on('data', (chunk) => { + data += chunk; + }); + resp.on('end', () => { + 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); + + 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 => { + console.log(err); + addToLogs("--> error : " + err); + bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); + }); + } + }); + + }).on("error", (err) => { + console.log("Error: " + err.message); + addToLogs("--> error : " + err); + bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); + }); +} + +module.exports = { rtag, r34 }; \ No newline at end of file