commit
This commit is contained in:
parent
e2f51ddee9
commit
6781c7ff91
3 changed files with 140 additions and 67 deletions
81
app.js
81
app.js
|
@ -1,8 +1,7 @@
|
||||||
//Importing libs
|
//Importing libs
|
||||||
const { Telegraf } = require('telegraf');
|
const { Telegraf } = require('telegraf');
|
||||||
const fs = require('fs');
|
|
||||||
const discord = require('discord.js');
|
const discord = require('discord.js');
|
||||||
const mysql = require('mysql');
|
const fs = require('fs');
|
||||||
|
|
||||||
//Importing other files
|
//Importing other files
|
||||||
const { getJoke } = require('./libs/dadJokes');
|
const { getJoke } = require('./libs/dadJokes');
|
||||||
|
@ -10,72 +9,12 @@ const { rtag, r34 } = require('./libs/rule34');
|
||||||
const { addToLogs, isTrue, image_search, getHelp } = require('./libs/botTools');
|
const { addToLogs, isTrue, image_search, getHelp } = require('./libs/botTools');
|
||||||
const { rockPaperScissorsAgainstBot } = require('./libs/games');
|
const { rockPaperScissorsAgainstBot } = require('./libs/games');
|
||||||
const { generateImage, answerQuestion } = require('./libs/openAi');
|
const { generateImage, answerQuestion } = require('./libs/openAi');
|
||||||
|
const { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv } = require('./libs/mysql');
|
||||||
|
|
||||||
//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: 3276799});
|
||||||
|
|
||||||
//mysql connection
|
|
||||||
const connection = mysql.createConnection({
|
|
||||||
host: "localhost",
|
|
||||||
user: "root",
|
|
||||||
password: process.env.MYSQL,
|
|
||||||
database: "discord"
|
|
||||||
});
|
|
||||||
|
|
||||||
//mysql functions
|
|
||||||
function addUserToDb(id, user) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
connection.query('INSERT INTO users(userid, username) VALUES("' + id + '","' + user + '")' , (error, results, fields) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function incrementQuota(id) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
connection.query('UPDATE users SET quota = quota + 1 WHERE userid = ' + id, (error, results, fields) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
} else {
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function usersInDb() {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
connection.query('SELECT userid FROM users', (error, results, fields) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
} else {
|
|
||||||
users = [];
|
|
||||||
results.forEach(element => {
|
|
||||||
users.push(element.userid);
|
|
||||||
});
|
|
||||||
resolve(users);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getQuota(id) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
connection.query('SELECT quota FROM users WHERE userid = ' + id, (error, results, fields) => {
|
|
||||||
if (error) {
|
|
||||||
reject(error);
|
|
||||||
} else {
|
|
||||||
resolve(results[0].quota);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//Telegram commands
|
//Telegram commands
|
||||||
bot.command('start', ctx => {
|
bot.command('start', ctx => {
|
||||||
bot.telegram.sendMessage(ctx.chat.id, 'hello there! Welcome to my new telegram bot.\nType /help for help.', {})
|
bot.telegram.sendMessage(ctx.chat.id, 'hello there! Welcome to my new telegram bot.\nType /help for help.', {})
|
||||||
|
@ -162,7 +101,7 @@ bot.command('q', async ctx => {
|
||||||
answerQuestion(ctx.message.text.slice(+3)).then((res) => {
|
answerQuestion(ctx.message.text.slice(+3)).then((res) => {
|
||||||
console.log('[Telegram] Sent answer to : ' + ctx.message.text.slice(+3));
|
console.log('[Telegram] Sent answer to : ' + ctx.message.text.slice(+3));
|
||||||
addToLogs('[Telegram] Sent answer to : ' + ctx.message.text.slice(+3));
|
addToLogs('[Telegram] Sent answer to : ' + ctx.message.text.slice(+3));
|
||||||
bot.telegram.sendMessage(ctx.chat.id, res.data.choices[0].text.slice(+2), {});
|
bot.telegram.sendMessage(ctx.chat.id, res.data.choices[0].message.content, {});
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
addToLogs(err);
|
addToLogs(err);
|
||||||
|
@ -221,7 +160,7 @@ client.on('interactionCreate', async interaction => {
|
||||||
users = await usersInDb();
|
users = await usersInDb();
|
||||||
|
|
||||||
if (!(users.includes(interaction.member.user.id))) {
|
if (!(users.includes(interaction.member.user.id))) {
|
||||||
addUserToDb(interaction.member.user.id, interaction.member.user.username);
|
await addUserToDb(interaction.member.user.id, interaction.member.user.username);
|
||||||
addToLogs('[Discord] Added user to the database : ' + 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);
|
console.log('[Discord] Added user to the database : ' + interaction.member.user.username);
|
||||||
}
|
}
|
||||||
|
@ -236,7 +175,7 @@ client.on('interactionCreate', async interaction => {
|
||||||
|
|
||||||
answerQuestion(interaction.options.get('question').value).then((res) => {
|
answerQuestion(interaction.options.get('question').value).then((res) => {
|
||||||
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.lenght);
|
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);
|
||||||
console.log('[Discord] Sent answer to : ' + interaction.options.get('question').value);
|
console.log('[Discord] Sent answer to : ' + interaction.options.get('question').value);
|
||||||
}
|
}
|
||||||
|
@ -266,6 +205,16 @@ client.on('interactionCreate', async interaction => {
|
||||||
else if (interaction.commandName === 'info') {
|
else if (interaction.commandName === 'info') {
|
||||||
console.log(interaction)
|
console.log(interaction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (interaction.commandName === 'addconv') {
|
||||||
|
console.log(await addConv(interaction.options.get('name').value));
|
||||||
|
interaction.reply('Conversation added to db');
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (interaction.commandName === 'delconv') {
|
||||||
|
console.log(await delConv(interaction.options.get('name').value));
|
||||||
|
interaction.reply('Conversation deleted from db');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//bot launch
|
//bot launch
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
const mysql = require('mysql');
|
||||||
|
|
||||||
|
const connection = mysql.createConnection({
|
||||||
|
host: "localhost",
|
||||||
|
user: "root",
|
||||||
|
password: process.env.MYSQL,
|
||||||
|
database: "discord"
|
||||||
|
});
|
||||||
|
|
||||||
|
function addUserToDb(id, user) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query('INSERT INTO users(userid, username) VALUES("' + id + '","' + user + '")' , (error, results, fields) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function incrementQuota(id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query('UPDATE users SET quota = quota + 1 WHERE userid = ' + id, (error, results, fields) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function usersInDb() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query('SELECT userid FROM users', (error, results, fields) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
users = [];
|
||||||
|
results.forEach(element => {
|
||||||
|
users.push(element.userid);
|
||||||
|
});
|
||||||
|
resolve(users);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getQuota(id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query('SELECT quota FROM users WHERE userid = ' + id, (error, results, fields) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve(results[0].quota);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addConv (convName) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query('INSERT INTO conversations (name) VALUES ("' + convName + '")', (error, results, fields) => {
|
||||||
|
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) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve(results);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function delConv (convName) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
connection.query('DROP TABLE ' + convName, (error, results, fields) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
connection.query('DELETE FROM conversations WHERE name = "' + convName + '"', (error, results, fields) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error);
|
||||||
|
} else {
|
||||||
|
resolve(results);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { addUserToDb, incrementQuota, usersInDb, getQuota, addConv, delConv };
|
|
@ -30,7 +30,33 @@ const commands = [
|
||||||
required : true,
|
required : true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name : 'addconv',
|
||||||
|
description : 'Add a conversation to the database',
|
||||||
|
options : [
|
||||||
|
{
|
||||||
|
name : 'name',
|
||||||
|
description : 'The name of the conversation',
|
||||||
|
type : ApplicationCommandOptionType.String,
|
||||||
|
required : true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name : 'delconv',
|
||||||
|
description : 'Delete a conversation from the database',
|
||||||
|
options : [
|
||||||
|
{
|
||||||
|
name : 'name',
|
||||||
|
description : 'The name of the conversation',
|
||||||
|
type : ApplicationCommandOptionType.String,
|
||||||
|
required : true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD);
|
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue