diff --git a/app.js b/app.js index 7d98190..7f3a6c8 100644 --- a/app.js +++ b/app.js @@ -30,6 +30,7 @@ function addUserToDb(id, user) { console.error('Erreur dans la requête : ', error); } else { console.log('Les résultats de la requête : ', results); + return true; } }); @@ -198,52 +199,54 @@ client.on('interactionCreate', async interaction => { }); if (!(users.includes(interaction.member.user.id))) { - addUserToDb(interaction.member.user.id, interaction.member.user.username) + addUserToDb(interaction.member.user.id, interaction.member.user.username); addToLogs('[Discord] Added user to the database : ' + interaction.member.user.username); console.log('[Discord] Added user to the database : ' + interaction.member.user.username); } } }); + + setTimeout(() => { + connection.query('SELECT quota FROM users WHERE userid = '+ interaction.member.user.id, (error, results, fields) => { + if (error) { + console.error('Erreur dans la requête : ', error); + } else { + if (results[0].quota >= 999) { + interaction.editReply('Quota exceeded, please wait'); + } + else { + incrementQuota(interaction.member.user.id); - connection.query('SELECT quota FROM users WHERE userid = '+ interaction.member.user.id, (error, results, fields) => { - if (error) { - console.error('Erreur dans la requête : ', error); - } else { - if (results[0].quota >= 999) { - interaction.editReply('Quota exceeded, please wait'); + answerQuestion(interaction.options.get('question').value).then((res) => { + if (res.data.choices[0].message.content.length > 4096) { + interaction.editReply(res.data.choices[0].message.content.lenght); + addToLogs('[Discord] Sent answer to : ' +interaction.options.get('question').value); + console.log('[Discord] Sent answer to : ' + interaction.options.get('question').value); + } + else{ + const embed = new discord.EmbedBuilder() + .setColor(0xFABBDE) + .setAuthor({ name : "Reply to : " + interaction.member.user.username, iconURL : "https://cdn.discordapp.com/avatars/"+interaction.member.user.id+"/"+interaction.member.user.avatar+".jpeg"}) + .setTitle("Question : " + interaction.options.get('question').value) + .setDescription(res.data.choices[0].message.content) + .setFooter({ text : "Powered by OpenAI https://www.openai.com/", iconURL : "https://seeklogo.com/images/O/open-ai-logo-8B9BFEDC26-seeklogo.com.png" }); + + console.log('[Discord] Sent answer to : ' + interaction.options.get('question').value); + addToLogs('[Discord] Sent answer to : ' +interaction.options.get('question').value); + interaction.editReply({ embeds : [embed] }); + } + }).catch((err) => { + console.log(err); + addToLogs(err); + interaction.editReply("Something went wrong"); + }) + + console.log('[Discord] Generating answer to : ' + interaction.options.get('question').value); + addToLogs('[Discord] Generating answer to : ' + interaction.options.get('question').value); + } } - else { - incrementQuota(interaction.member.user.id); - - answerQuestion(interaction.options.get('question').value).then((res) => { - if (res.data.choices[0].message.content.length > 4096) { - interaction.editReply(res.data.choices[0].message.content.lenght); - addToLogs('[Discord] Sent answer to : ' +interaction.options.get('question').value); - console.log('[Discord] Sent answer to : ' + interaction.options.get('question').value); - } - else{ - const embed = new discord.EmbedBuilder() - .setColor(0xFABBDE) - .setAuthor({ name : "Reply to : " + interaction.member.user.username, iconURL : "https://cdn.discordapp.com/avatars/"+interaction.member.user.id+"/"+interaction.member.user.avatar+".jpeg"}) - .setTitle("Question : " + interaction.options.get('question').value) - .setDescription(res.data.choices[0].message.content) - .setFooter({ text : "Powered by OpenAI https://www.openai.com/", iconURL : "https://seeklogo.com/images/O/open-ai-logo-8B9BFEDC26-seeklogo.com.png" }); - - console.log('[Discord] Sent answer to : ' + interaction.options.get('question').value); - addToLogs('[Discord] Sent answer to : ' +interaction.options.get('question').value); - interaction.editReply({ embeds : [embed] }); - } - }).catch((err) => { - console.log(err); - addToLogs(err); - interaction.editReply("Something went wrong"); - }) - - console.log('[Discord] Generating answer to : ' + interaction.options.get('question').value); - addToLogs('[Discord] Generating answer to : ' + interaction.options.get('question').value); - } - } - }); + }); + }, 100); } else if (interaction.commandName === 'info') {