From 13ec9babde94f7bbf13a04ac9f6766b89a986a01 Mon Sep 17 00:00:00 2001 From: Lukian Date: Tue, 20 Jun 2023 15:09:53 +0200 Subject: [PATCH] commit --- app.js | 2 +- commands/commands.js | 28 ++++++++++++++- events/discordEvents.js | 58 +++++++++++++++++++++--------- functions/discord/addchannel.js | 43 ++++++++++++++++++++++ functions/discord/deletechannel.js | 43 ++++++++++++++++++++++ libs/mysql.js | 42 +++++++++++++++++++++- libs/openAi.js | 15 +++++++- 7 files changed, 211 insertions(+), 20 deletions(-) create mode 100644 functions/discord/addchannel.js create mode 100644 functions/discord/deletechannel.js diff --git a/app.js b/app.js index 8c8515a..2ce7e9b 100644 --- a/app.js +++ b/app.js @@ -8,7 +8,7 @@ const telegramEvents = require('./events/telegramEvents'); //bot initialization const bot = new Telegraf(process.env.TELEGRAM); -const client = new discord.Client({intents: 3276799}); +const client = new discord.Client({intents: 33297}); //Telegram events telegramEvents.start(bot); diff --git a/commands/commands.js b/commands/commands.js index 3169906..d26d6d0 100644 --- a/commands/commands.js +++ b/commands/commands.js @@ -1,4 +1,4 @@ -const {ApplicationCommandOptionType} = require('discord.js'); +const { ApplicationCommandOptionType } = require('discord.js'); const commands = [ { @@ -115,6 +115,32 @@ const commands = [ }, ], }, + + { + name : 'addchannel', + description : 'Add a channel to the conversation system', + options : [ + { + name : 'channel', + description : 'The channel you want to add', + type : ApplicationCommandOptionType.Channel, + required : true, + }, + ], + }, + + { + name : 'deletechannel', + description : 'Delete a channel from the conversation system', + options : [ + { + name : 'channel', + description : 'The channel you want to delete', + type : ApplicationCommandOptionType.Channel, + required : true, + }, + ], + }, ]; module.exports = { commands }; \ No newline at end of file diff --git a/events/discordEvents.js b/events/discordEvents.js index 7d717da..484e8b3 100644 --- a/events/discordEvents.js +++ b/events/discordEvents.js @@ -1,8 +1,5 @@ const discord = require('discord.js'); -const { addToLogs } = require('../libs/botTools'); -const { generateImage, } = require('../libs/openAi'); - const { commands } = require('../commands/commands'); const gptrequest = require('../functions/discord/gptrequest'); @@ -15,12 +12,39 @@ const displayconv = require('../functions/discord/displayconv'); const getmyguota = require('../functions/discord/getmyguota'); const github = require('../functions/discord/github'); const dalle = require('../functions/discord/dalle'); +const addchannel = require('../functions/discord/addchannel'); +const deletechannel = require('../functions/discord/deletechannel'); + +const { listchannels, incrementQuota } = require('../libs/mysql'); +const { sendQuickConv } = require('../libs/openAi') module.exports = { newMessage: (client) => { client.on('messageCreate', async msg => { - if (msg.content.startsWith('/g')) { + const channels = await listchannels(); + channelId = msg.channel.id; + + if (!channels.includes(channelId) || msg.author.bot == true) {} + else { + discordMessages = await msg.channel.messages.fetch({ limit: 15 }) + + discordMessages.reverse(); + + messages = [{"role": "system", "content": "You are a helpful assistant."}]; + + discordMessages.forEach(async message => { + if (msg.author.id == '1059559067846189067') { + messages.push({"role" : "assistant", "content" : message.content}); + } else { + messages.push({"role" : "user", "content" : message.content}); + } + }); + + response = await sendQuickConv(messages); + + msg.reply(response.data.choices[0].message.content); + incrementQuota(msg.author.id, response.data.usage.total_tokens); } }); }, @@ -73,28 +97,30 @@ module.exports = { else if (interaction.commandName === 'dalle') { dalle(interaction, client); } + + else if (interaction.commandName === 'addchannel') { + addchannel(interaction, client); + } + + else if (interaction.commandName === 'deletechannel') { + deletechannel(interaction, client); + } }); }, ready: (client) => { - client.on('ready', () => { + client.on('ready', async () => { console.log(`[Discord] Logged in as ${client.user.tag} !`); client.user.setPresence({ activities: [{ name: 'la belle chaise', type: 3 }] }); const rest = new discord.REST({ version: '10' }).setToken(process.env.DISCORD); - client.guilds.cache.forEach(async (guild) => { - try { - await rest.put( - discord.Routes.applicationGuildCommands('1059559067846189067', guild.id), - { body: commands }, - ); + await rest.put( + discord.Routes.applicationCommands('1059559067846189067'), + { body: commands }, + ); - console.log('[Discord] Successfully reloaded application (/) commands for ' + guild.name + '.'); - } catch (error) { - console.error(error); - } - }) + console.log('[Discord] Successfully reloaded application (/) commands globally.'); }); }, diff --git a/functions/discord/addchannel.js b/functions/discord/addchannel.js new file mode 100644 index 0000000..f843dfe --- /dev/null +++ b/functions/discord/addchannel.js @@ -0,0 +1,43 @@ +const { listchannels, addChannel } = require('../../libs/mysql'); +const { EmbedBuilder, PermissionsBitField } = require('discord.js'); + +async function addchannel(interaction, client) { + await interaction.deferReply(); + + if (!interaction.memberPermissions.has(PermissionsBitField.Flags.Administrator)) { + const embed = new EmbedBuilder() + .setColor(0xFABBDE) + .setAuthor({ name: "Error", iconURL: client.user.displayAvatarURL() }) + .setDescription("You must be an administrator to use this command.") + .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] }); + return; + } + + channels = await listchannels(); + + id = interaction.options.get('channel').value; + + if (!channels.includes(id)) { + addChannel(id); + + const embed = new EmbedBuilder() + .setColor(0xFABBDE) + .setAuthor({ name: "Channel added", iconURL: client.user.displayAvatarURL() }) + .setDescription("Channel " + id + " added to db") + .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] }); + } else { + const embed = new EmbedBuilder() + .setColor(0xFABBDE) + .setAuthor({ name: "Error", iconURL: client.user.displayAvatarURL() }) + .setDescription("Channel " + id + " already exists in the database.") + .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] }); + } +} + +module.exports = addchannel; \ No newline at end of file diff --git a/functions/discord/deletechannel.js b/functions/discord/deletechannel.js new file mode 100644 index 0000000..05d194e --- /dev/null +++ b/functions/discord/deletechannel.js @@ -0,0 +1,43 @@ +const { listchannels, deleteChannel } = require('../../libs/mysql'); +const { EmbedBuilder, PermissionsBitField } = require('discord.js'); + +async function delechannel(interaction, client) { + await interaction.deferReply(); + + if (!interaction.memberPermissions.has(PermissionsBitField.Flags.Administrator)) { + const embed = new EmbedBuilder() + .setColor(0xFABBDE) + .setAuthor({ name: "Error", iconURL: client.user.displayAvatarURL() }) + .setDescription("You must be an administrator to use this command.") + .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] }); + return; + } + + channels = await listchannels(); + + id = interaction.options.get('channel').value; + + if (channels.includes(id)) { + deleteChannel(id); + + const embed = new EmbedBuilder() + .setColor(0xFABBDE) + .setAuthor({ name: "Channel deleted", iconURL: client.user.displayAvatarURL() }) + .setDescription("Channel " + id + " deleted from db") + .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] }); + } else { + const embed = new EmbedBuilder() + .setColor(0xFABBDE) + .setAuthor({ name: "Error", iconURL: client.user.displayAvatarURL() }) + .setDescription("Channel " + id + " does not exist in the database.") + .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] }); + } +} + +module.exports = delechannel; \ No newline at end of file diff --git a/libs/mysql.js b/libs/mysql.js index 0c0e5fd..c9a83db 100644 --- a/libs/mysql.js +++ b/libs/mysql.js @@ -167,4 +167,44 @@ async function isNewUser(id, username) { }); } -module.exports = { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv, getConvs, addMessage, getMessages, isNewUser }; \ No newline at end of file +async function listchannels() { + return new Promise((resolve, reject) => { + connection.query('SELECT id FROM channels', (error, results, fields) => { + if (error) { + reject(error); + } else { + channels = []; + results.forEach(element => { + channels.push(element.id); + }); + resolve(channels); + } + }); + }); +} + +async function addChannel(id) { + return new Promise((resolve, reject) => { + connection.query('INSERT INTO channels (id) VALUES (' + id + ')', (error, results, fields) => { + if (error) { + reject(error); + } else { + resolve(); + } + }); + }); +} + +async function deleteChannel(id) { + return new Promise((resolve, reject) => { + connection.query('DELETE FROM channels WHERE id = ' + id, (error, results, fields) => { + if (error) { + reject(error); + } else { + resolve(); + } + }); + }); +} + +module.exports = { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv, getConvs, addMessage, getMessages, isNewUser, listchannels, addChannel, deleteChannel }; \ No newline at end of file diff --git a/libs/openAi.js b/libs/openAi.js index 2c91f72..5267563 100644 --- a/libs/openAi.js +++ b/libs/openAi.js @@ -60,4 +60,17 @@ async function sendConv (messages) { return response; } -module.exports = { generateImage, answerQuestion, sendConv, quickAnswer }; \ No newline at end of file +async function sendQuickConv (messages) { + response = await openai.createChatCompletion({ + model: "gpt-3.5-turbo", + messages: messages, + temperature: 0.9, + }).catch((err) => { + console.log(err); + addToLogs("--> error : " + err); + }) + + return response; +} + +module.exports = { generateImage, answerQuestion, sendConv, quickAnswer, sendQuickConv }; \ No newline at end of file