This commit is contained in:
Lukian LEIZOUR 2023-03-04 17:30:09 +01:00
parent 81df8879c2
commit 20292262d6
4 changed files with 47 additions and 25 deletions

27
app.js
View file

@ -167,13 +167,12 @@ client.on('interactionCreate', async interaction => {
quota = await getQuota(interaction.member.user.id); quota = await getQuota(interaction.member.user.id);
if (quota >= 999) { if (quota >= 200000) {
interaction.editReply('Quota exceeded, please wait'); interaction.editReply('Quota exceeded, please wait untill reset (every month)');
} }
else { else {
incrementQuota(interaction.member.user.id);
answerQuestion(interaction.options.get('question').value).then((res) => { answerQuestion(interaction.options.get('question').value).then((res) => {
incrementQuota(interaction.member.user.id, res.data.usage.total_tokens);
if (res.data.choices[0].message.content.length > 4096) { if (res.data.choices[0].message.content.length > 4096) {
interaction.editReply(res.data.choices[0].message.content); interaction.editReply(res.data.choices[0].message.content);
addToLogs('[Discord] Sent answer to : ' +interaction.options.get('question').value); addToLogs('[Discord] Sent answer to : ' +interaction.options.get('question').value);
@ -244,6 +243,12 @@ client.on('interactionCreate', async interaction => {
else if (interaction.commandName === 'addmsg') { else if (interaction.commandName === 'addmsg') {
await interaction.deferReply(); await interaction.deferReply();
quota = await getQuota(interaction.member.user.id);
if (quota >= 200000) {
interaction.editReply('Quota exceeded, please wait untill reset (every month)');
} else {
await addMessage(interaction.options.get('name').value,"user" ,interaction.options.get('message').value, interaction.member.user.username); await addMessage(interaction.options.get('name').value,"user" ,interaction.options.get('message').value, interaction.member.user.username);
messages = await getMessages(interaction.options.get('name').value, "role"); messages = await getMessages(interaction.options.get('name').value, "role");
@ -267,6 +272,7 @@ client.on('interactionCreate', async interaction => {
console.log(err); console.log(err);
}); });
} }
}
else if (interaction.commandName === 'displayconv') { else if (interaction.commandName === 'displayconv') {
await interaction.deferReply(); await interaction.deferReply();
@ -289,6 +295,19 @@ client.on('interactionCreate', async interaction => {
interaction.editReply({ embeds : [embed] }); interaction.editReply({ embeds : [embed] });
} }
else if (interaction.commandName === 'getmyguota') {
await interaction.deferReply();
quota = await getQuota(interaction.member.user.id);
const embed = new discord.EmbedBuilder()
.setColor(0xFABBDE)
.setAuthor({ name : "Quota : " + interaction.member.user.username, iconURL : "https://cdn.discordapp.com/avatars/"+interaction.member.user.id+"/"+interaction.member.user.avatar+".jpeg"})
.setDescription("You have a quota of " + quota + "/200k tokens")
.setFooter({ text : "Powered by OpenAI https://www.openai.com/", iconURL : "https://seeklogo.com/images/O/open-ai-logo-8B9BFEDC26-seeklogo.com.png" });
interaction.editReply({ embeds : [embed] });
}
}); });
//bot launch //bot launch

View file

@ -19,9 +19,9 @@ function addUserToDb(id, user) {
}); });
} }
function incrementQuota(id) { function incrementQuota(id, value) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
connection.query('UPDATE users SET quota = quota + 1 WHERE userid = ' + id, (error, results, fields) => { connection.query('UPDATE users SET quota = quota + '+ value + ' WHERE userid = ' + id, (error, results, fields) => {
if (error) { if (error) {
reject(error); reject(error);
} else { } else {

View file

@ -24,14 +24,13 @@ async function generateImage(query, ctx, bot) {
async function answerQuestion(query) { async function answerQuestion(query) {
response = await openai.createChatCompletion({ response = await openai.createChatCompletion({
model: "gpt-3.5-turbo", model: "gpt-3.5-turbo",
messages: [{"role":"system", "content" : "You are a helpful assistant."},{ "role" : "user", "content" : "who is the president of the united states?" }, { "role" : "assistant", "content" : "Joe Biden"}], messages: [{ "role" : "user", "content" : query}],
temperature: 0.9, temperature: 0.9,
}).catch((err) => { }).catch((err) => {
console.log(err); console.log(err);
addToLogs("--> error : " + err); addToLogs("--> error : " + err);
}) })
console.log(response);
return response; return response;
} }

View file

@ -93,6 +93,10 @@ const commands = [
}, },
], ],
}, },
{
name : 'getmyguota',
description : 'Get your quota',
},
]; ];
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD); const rest = new REST({ version: '10' }).setToken(process.env.DISCORD);