diff --git a/app.js b/app.js index da622dd..5cc873c 100644 --- a/app.js +++ b/app.js @@ -12,7 +12,6 @@ const { generateImage, answerQuestion } = require('./libs/openAi'); //bot initialization const bot = new Telegraf(process.env.TELEGRAM); - const client = new discord.Client({intents: 3276799}); //Telegram commands @@ -82,11 +81,23 @@ bot.command('rps', ctx => { }) bot.command('g', ctx => { - generateImage(ctx.message.text.slice(+3), ctx, bot) + generateImage(ctx.message.text.slice(+3)).then((res) => { + console.log('[Telegram] Sent image to : ' + ctx.message.text.slice(+3)); + addToLogs('[Telegram] Sent image to : ' + ctx.message.text.slice(+3)); + bot.telegram.sendPhoto(ctx.chat.id, res.data.data[0].url, {}); + }).catch((err) => { + console.log(err); + addToLogs(err); + bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); + }) + + console.log('[Telegram] Generating image to : ' + ctx.message.text.slice(+3)); + addToLogs('[Telegram] Generating image to : ' + ctx.message.text.slice(+3)); + bot.telegram.sendMessage(ctx.chat.id, "Generating image...", {}); }) bot.command('q', async ctx => { - answerQuestion(ctx.message.text.slice(+3), ctx, bot).then((res) => { + answerQuestion(ctx.message.text.slice(+3)).then((res) => { console.log('[Telegram] Sent answer to : ' + ctx.message.text.slice(+3)); addToLogs('[Telegram] Sent answer to : ' + ctx.message.text.slice(+3)); bot.telegram.sendMessage(ctx.chat.id, res.data.choices[0].text.slice(+2), {}); @@ -106,9 +117,8 @@ bot.command('sb' , ctx => { }) //Discord commands - client.on('ready', () => { - console.log(`Logged in as ${client.user.tag}!`); + console.log(`[Discord] Logged in as ${client.user.tag} !`); }); client.on('messageCreate', async msg => { @@ -127,9 +137,30 @@ client.on('messageCreate', async msg => { addToLogs('[Discord] Generating answer to : ' + msg.content.slice(+3)); msg.reply('Generating the answer...'); } + + if (msg.content.startsWith('/g')) { + generateImage(msg.content.slice(+3)).then((res) => { + console.log('[Discord] Sent image to : ' + msg.content.slice(+3)); + addToLogs('[Discord] Sent image to : ' + msg.content.slice(+3)); + msg.channel.send(res.data.data[0].url); + }).catch((err) => { + console.log(err); + addToLogs(err); + msg.reply("Something went wrong"); + }) + + console.log('[Discord] Generating image to : ' + msg.content.slice(+3)); + addToLogs('[Discord] Generating image to : ' + msg.content.slice(+3)); + msg.reply('Generating the image...'); + } + + else if (msg.content.startsWith('/github')) { + console.log('[Discord] Sent github link') + addToLogs('[Discord] Sent github link') + msg.reply('Link of the Gihhub repository :\n -https://github.com/Ninja-Jambon/chaise_bot') + } }); //bot launch bot.launch() - client.login(process.env.DISCORD); \ No newline at end of file diff --git a/libs/openAi.js b/libs/openAi.js index ac67fab..aa5112d 100644 --- a/libs/openAi.js +++ b/libs/openAi.js @@ -1,5 +1,4 @@ const { Configuration, OpenAIApi } = require("openai"); -const { addToLogs } = require('./botTools'); const configuration = new Configuration({ apiKey: process.env.OPENAI, @@ -7,8 +6,8 @@ const configuration = new Configuration({ const openai = new OpenAIApi(configuration); -function generateImage(query, ctx, bot) { - const image = openai.createImage({ +async function generateImage(query, ctx, bot) { + const image = await openai.createImage({ prompt: query, n: 1, size: "1024x1024", @@ -18,19 +17,10 @@ function generateImage(query, ctx, bot) { addToLogs("--> error : " + err); bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); }); + + return image; - console.log("--> generating image for the querry " + query); - addToLogs("--> generating image for the querry " + query) - bot.telegram.sendMessage(ctx.chat.id, "Generating the image.", {}); - - image.then((res) => { - url = res.data.data[0].url - - bot.telegram.sendPhoto(ctx.chat.id, url, {"caption": "This is a generated image for the querry : " + query}).catch((err) => { - bot.telegram.sendMessage(ctx.chat.id, "Something went wrong.", {}); - console.log("--> error while sending the image : " + err); - }) - }) + //image link : image.data[0].url } async function answerQuestion(query) {