commit
This commit is contained in:
parent
fcd80c5ac8
commit
8654d31004
4 changed files with 126 additions and 11 deletions
52
app.js
52
app.js
|
@ -8,8 +8,8 @@ const { getJoke } = require('./libs/dadJokes');
|
|||
const { rtag, r34 } = require('./libs/rule34');
|
||||
const { addToLogs, isTrue, image_search, getHelp } = require('./libs/botTools');
|
||||
const { rockPaperScissorsAgainstBot } = require('./libs/games');
|
||||
const { generateImage, answerQuestion } = require('./libs/openAi');
|
||||
const { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv, getConvs } = require('./libs/mysql');
|
||||
const { generateImage, answerQuestion, sendConv } = require('./libs/openAi');
|
||||
const { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv, getConvs, addMessage, getMessages } = require('./libs/mysql');
|
||||
|
||||
//bot initialization
|
||||
const bot = new Telegraf(process.env.TELEGRAM);
|
||||
|
@ -207,11 +207,13 @@ client.on('interactionCreate', async interaction => {
|
|||
}
|
||||
|
||||
else if (interaction.commandName === 'addconv') {
|
||||
if (!interaction.options.get('name').value.includes(" ")) {
|
||||
console.log(await addConv(interaction.options.get('name').value));
|
||||
interaction.reply('Conversation added to db');
|
||||
await interaction.deferReply();
|
||||
convs = await getConvs();
|
||||
if (!interaction.options.get('name').value.includes(" ") && !convs.includes(interaction.options.get('name').value)) {
|
||||
await addConv(interaction.options.get('name').value);
|
||||
interaction.editReply('Conversation added to db');
|
||||
} else {
|
||||
interaction.reply('Conversation name cannot contain spaces');
|
||||
interaction.editReply('Verify the name of the conversation (it must not contain spaces and must be unique)');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -233,6 +235,44 @@ client.on('interactionCreate', async interaction => {
|
|||
|
||||
interaction.reply(message);
|
||||
}
|
||||
|
||||
else if (interaction.commandName === 'addmsg') {
|
||||
await interaction.deferReply();
|
||||
await addMessage(interaction.options.get('name').value,"user" ,interaction.options.get('message').value);
|
||||
|
||||
messages = await getMessages(interaction.options.get('name').value);
|
||||
|
||||
sendConv(messages).then((res) => {
|
||||
addMessage(interaction.options.get('name').value,"assistant",res.data.choices[0].message.content);
|
||||
|
||||
const embed_user = new discord.EmbedBuilder()
|
||||
.setColor(0xBBFAF4)
|
||||
.setAuthor({ name : interaction.member.user.username, iconURL : "https://cdn.discordapp.com/avatars/"+interaction.member.user.id+"/"+interaction.member.user.avatar+".jpeg"})
|
||||
.setTitle(interaction.options.get('message').value);
|
||||
|
||||
const embed_bot = new discord.EmbedBuilder()
|
||||
.setColor(0xFABBDE)
|
||||
.setAuthor({ name : "Chaise bot", iconURL : client.user.displayAvatarURL()})
|
||||
.setTitle(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" });
|
||||
|
||||
interaction.editReply({ embeds : [embed_user, embed_bot] });
|
||||
}).catch((err) => {
|
||||
console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
else if (interaction.commandName === 'displayconv') {
|
||||
await interaction.deferReply();
|
||||
messages = await getMessages(interaction.options.get('name').value);
|
||||
message = "";
|
||||
|
||||
messages.forEach(element => {
|
||||
message += element.role + " : " + element.content + "\n";
|
||||
});
|
||||
|
||||
interaction.editReply(message);
|
||||
}
|
||||
});
|
||||
|
||||
//bot launch
|
||||
|
|
|
@ -65,11 +65,11 @@ function addConv (convName) {
|
|||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
connection.query('CREATE TABLE ' + convName + '(id int NOT NULL AUTO_INCREMENT, author varchar(20) NOT NULL, message text, PRIMARY KEY (id))', (error, results, fields) => {
|
||||
connection.query('CREATE TABLE ' + convName + '(id int NOT NULL AUTO_INCREMENT, author varchar(20) NOT NULL, message text, PRIMARY KEY (id))', async (error, results, fields) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(results);
|
||||
resolve(addMessage(convName, 'system', 'You are a helpful assistant.'));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -111,4 +111,34 @@ function getConvs() {
|
|||
});
|
||||
}
|
||||
|
||||
module.exports = { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv, getConvs };
|
||||
function addMessage(convName, author, message) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query('INSERT INTO ' + convName + ' (author, message) VALUES ("' + author + '"'+', "' + message + '")', (error, results, fields) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getMessages (convName) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query('SELECT author, message FROM ' + convName, (error, results, fields) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
messages = [];
|
||||
|
||||
results.forEach(element => {
|
||||
messages.push({"role" : element.author, "content" : element.message});
|
||||
});
|
||||
|
||||
resolve(messages);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv, getConvs, addMessage, getMessages };
|
|
@ -24,7 +24,21 @@ async function generateImage(query, ctx, bot) {
|
|||
async function answerQuestion(query) {
|
||||
response = await openai.createChatCompletion({
|
||||
model: "gpt-3.5-turbo",
|
||||
messages: [{ "role" : "user", "content" : query}],
|
||||
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"}],
|
||||
temperature: 0.9,
|
||||
}).catch((err) => {
|
||||
console.log(err);
|
||||
addToLogs("--> error : " + err);
|
||||
})
|
||||
|
||||
console.log(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
async function sendConv (messages) {
|
||||
response = await openai.createChatCompletion({
|
||||
model: "gpt-3.5-turbo",
|
||||
messages: messages,
|
||||
temperature: 0.9,
|
||||
}).catch((err) => {
|
||||
console.log(err);
|
||||
|
@ -34,4 +48,4 @@ async function answerQuestion(query) {
|
|||
return response;
|
||||
}
|
||||
|
||||
module.exports = { generateImage, answerQuestion };
|
||||
module.exports = { generateImage, answerQuestion, sendConv };
|
|
@ -62,6 +62,37 @@ const commands = [
|
|||
name : 'listconvs',
|
||||
description : 'List all the conversations in the database',
|
||||
},
|
||||
|
||||
{
|
||||
name : 'addmsg',
|
||||
description : 'Add a message to a conversation',
|
||||
options : [
|
||||
{
|
||||
name : 'name',
|
||||
description : 'The name of the conversation',
|
||||
type : ApplicationCommandOptionType.String,
|
||||
required : true,
|
||||
},
|
||||
{
|
||||
name : 'message',
|
||||
description : 'The message to add to the conversation',
|
||||
type : ApplicationCommandOptionType.String,
|
||||
required : true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name : 'displayconv',
|
||||
description : 'Display a conversation',
|
||||
options : [
|
||||
{
|
||||
name : 'name',
|
||||
description : 'The name of the conversation',
|
||||
type : ApplicationCommandOptionType.String,
|
||||
required : true,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue