This commit is contained in:
Lukian LEIZOUR 2023-01-31 13:42:40 +01:00
parent ab70c2ac19
commit c96c7142d3
2 changed files with 42 additions and 21 deletions

43
app.js
View file

@ -12,7 +12,6 @@ const { generateImage, answerQuestion } = require('./libs/openAi');
//bot initialization //bot initialization
const bot = new Telegraf(process.env.TELEGRAM); const bot = new Telegraf(process.env.TELEGRAM);
const client = new discord.Client({intents: 3276799}); const client = new discord.Client({intents: 3276799});
//Telegram commands //Telegram commands
@ -82,11 +81,23 @@ bot.command('rps', ctx => {
}) })
bot.command('g', 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 => { 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)); console.log('[Telegram] Sent answer to : ' + ctx.message.text.slice(+3));
addToLogs('[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), {}); bot.telegram.sendMessage(ctx.chat.id, res.data.choices[0].text.slice(+2), {});
@ -106,9 +117,8 @@ bot.command('sb' , ctx => {
}) })
//Discord commands //Discord commands
client.on('ready', () => { 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 => { client.on('messageCreate', async msg => {
@ -127,9 +137,30 @@ client.on('messageCreate', async msg => {
addToLogs('[Discord] Generating answer to : ' + msg.content.slice(+3)); addToLogs('[Discord] Generating answer to : ' + msg.content.slice(+3));
msg.reply('Generating the answer...'); 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
bot.launch() bot.launch()
client.login(process.env.DISCORD); client.login(process.env.DISCORD);

View file

@ -1,5 +1,4 @@
const { Configuration, OpenAIApi } = require("openai"); const { Configuration, OpenAIApi } = require("openai");
const { addToLogs } = require('./botTools');
const configuration = new Configuration({ const configuration = new Configuration({
apiKey: process.env.OPENAI, apiKey: process.env.OPENAI,
@ -7,8 +6,8 @@ const configuration = new Configuration({
const openai = new OpenAIApi(configuration); const openai = new OpenAIApi(configuration);
function generateImage(query, ctx, bot) { async function generateImage(query, ctx, bot) {
const image = openai.createImage({ const image = await openai.createImage({
prompt: query, prompt: query,
n: 1, n: 1,
size: "1024x1024", size: "1024x1024",
@ -19,18 +18,9 @@ function generateImage(query, ctx, bot) {
bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {}); bot.telegram.sendMessage(ctx.chat.id, "Something went wrong", {});
}); });
console.log("--> generating image for the querry " + query); return image;
addToLogs("--> generating image for the querry " + query)
bot.telegram.sendMessage(ctx.chat.id, "Generating the image.", {});
image.then((res) => { //image link : image.data[0].url
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);
})
})
} }
async function answerQuestion(query) { async function answerQuestion(query) {