This commit is contained in:
Lukian 2023-06-20 15:09:53 +02:00
parent ab45931f0f
commit 13ec9babde
7 changed files with 211 additions and 20 deletions

2
app.js
View file

@ -8,7 +8,7 @@ const telegramEvents = require('./events/telegramEvents');
//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: 33297});
//Telegram events //Telegram events
telegramEvents.start(bot); telegramEvents.start(bot);

View file

@ -1,4 +1,4 @@
const {ApplicationCommandOptionType} = require('discord.js'); const { ApplicationCommandOptionType } = require('discord.js');
const commands = [ 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 }; module.exports = { commands };

View file

@ -1,8 +1,5 @@
const discord = require('discord.js'); const discord = require('discord.js');
const { addToLogs } = require('../libs/botTools');
const { generateImage, } = require('../libs/openAi');
const { commands } = require('../commands/commands'); const { commands } = require('../commands/commands');
const gptrequest = require('../functions/discord/gptrequest'); const gptrequest = require('../functions/discord/gptrequest');
@ -15,12 +12,39 @@ const displayconv = require('../functions/discord/displayconv');
const getmyguota = require('../functions/discord/getmyguota'); const getmyguota = require('../functions/discord/getmyguota');
const github = require('../functions/discord/github'); const github = require('../functions/discord/github');
const dalle = require('../functions/discord/dalle'); 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 = { module.exports = {
newMessage: (client) => { newMessage: (client) => {
client.on('messageCreate', async msg => { 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') { else if (interaction.commandName === 'dalle') {
dalle(interaction, client); dalle(interaction, client);
} }
else if (interaction.commandName === 'addchannel') {
addchannel(interaction, client);
}
else if (interaction.commandName === 'deletechannel') {
deletechannel(interaction, client);
}
}); });
}, },
ready: (client) => { ready: (client) => {
client.on('ready', () => { client.on('ready', async () => {
console.log(`[Discord] Logged in as ${client.user.tag} !`); console.log(`[Discord] Logged in as ${client.user.tag} !`);
client.user.setPresence({ activities: [{ name: 'la belle chaise', type: 3 }] }); client.user.setPresence({ activities: [{ name: 'la belle chaise', type: 3 }] });
const rest = new discord.REST({ version: '10' }).setToken(process.env.DISCORD); const rest = new discord.REST({ version: '10' }).setToken(process.env.DISCORD);
client.guilds.cache.forEach(async (guild) => { await rest.put(
try { discord.Routes.applicationCommands('1059559067846189067'),
await rest.put( { body: commands },
discord.Routes.applicationGuildCommands('1059559067846189067', guild.id), );
{ body: commands },
);
console.log('[Discord] Successfully reloaded application (/) commands for ' + guild.name + '.'); console.log('[Discord] Successfully reloaded application (/) commands globally.');
} catch (error) {
console.error(error);
}
})
}); });
}, },

View file

@ -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;

View file

@ -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;

View file

@ -167,4 +167,44 @@ async function isNewUser(id, username) {
}); });
} }
module.exports = { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv, getConvs, addMessage, getMessages, isNewUser }; 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 };

View file

@ -60,4 +60,17 @@ async function sendConv (messages) {
return response; return response;
} }
module.exports = { generateImage, answerQuestion, sendConv, quickAnswer }; 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 };