switch to mysql

This commit is contained in:
Loïc Bersier 2019-07-08 22:10:14 +02:00
parent e64f4a9eb1
commit 1b66983fb7
11 changed files with 349 additions and 255 deletions

View file

@ -1,5 +1,5 @@
const fs = require('fs');
const { Command } = require('discord-akairo');
const autoResponseStat = require('../../models').autoresponseStat;
class autoresponseCommand extends Command {
constructor() {
@ -8,7 +8,7 @@ class autoresponseCommand extends Command {
category: 'admin',
args: [
{
id: 'text',
id: 'stat',
type: 'string',
prompt: {
start: 'Do you want to **enable** or **disable** auto response?',
@ -30,61 +30,18 @@ class autoresponseCommand extends Command {
}
async exec(message, args) {
let text = args.text;
let all = args.all;
if (args.stat == 'enable' || args.stat == 'disable') {
const autoresponseStat = await autoResponseStat.findOne({where: {serverID: message.guild.id}});
if (text.toLowerCase() == 'enable' || text.toLowerCase() == 'disable') {
let autoresponse = {};
let json = JSON.stringify(autoresponse);
if (all == 'all') {
const guild = this.client.guilds.get(message.guild.id);
fs.readFile('./json/autoresponse.json', 'utf8', function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
autoresponse = JSON.parse(data); //now it an object
guild.channels.forEach(channel => autoresponse[channel] = text.toLowerCase());
json = JSON.stringify(autoresponse); //convert it back to json
json = json.replace(/[<#>]/g, '');
fs.writeFile('./json/autoresponse.json', json, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
}
});
return message.channel.send('Auto response have been disable/enable on every channel');
} else if (text.toLowerCase() == 'disable' || text.toLowerCase() == 'enable') {
fs.readFile('./json/autoresponse.json', 'utf8', function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
autoresponse = JSON.parse(data); //now it an object
autoresponse[message.channel.id] = text.toLowerCase();
json = JSON.stringify(autoresponse); //convert it back to json
fs.writeFile('./json/autoresponse.json', json, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
}
});
if (!autoresponseStat) {
const body = {serverID: message.guild.id, stat: args.stat};
autoResponseStat.create(body);
return message.channel.send(`Autoresponse have been ${args.stat}ed`);
} else {
const body = {serverID: message.guild.id, stat: args.stat};
autoResponseStat.update(body, {where: {serverID: message.guild.id}});
return message.channel.send(`Autoresponse have been ${args.stat}ed`);
}
return message.channel.send(`Autoresponse have been ${text.toLowerCase()}d`);
} else {
return message.channel.send('You didin\'t type a valid input');
}
}
}

View file

@ -1,5 +1,5 @@
const { Command } = require('discord-akairo');
const fs = require('fs');
const Tag = require('../../models').Tag;
class TagCommand extends Command {
constructor() {
@ -35,37 +35,15 @@ class TagCommand extends Command {
}
async exec(message, args) {
if (args.trigger == null || args.response == null) return;
let trigger = args.trigger;
let response = args.response;
const tag = await Tag.findOne({where: {trigger: args.trigger, serverID: message.guild.id}});
trigger = trigger.toLowerCase();
let customresponse = {};
let json = JSON.stringify(customresponse);
fs.readFile(`./tag/${message.guild.id}.json`, 'utf8', function readFileCallback(err, data) {
if (err) {
fs.writeFile(`./tag/${message.guild.id}.json`, `{"${trigger}":"${response}"}`, function (err) {
if (err) {
console.log(err);
}
});
} else {
customresponse = JSON.parse(data); //now it an object
customresponse[trigger] = response;
json = JSON.stringify(customresponse); //convert it back to json
fs.writeFile(`./tag/${message.guild.id}.json`, json, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
}
});
return message.channel.send(`autoresponse have been set to ${trigger} : ${response}`);
if (!tag) {
const body = {trigger: args.trigger, response: args.response, owner: message.author.id, serverID: message.guild.id};
Tag.create(body);
return message.channel.send(`autoresponse have been set to ${args.trigger} : ${args.response}`);
} else {
return message.channel.send('The tag already exist!');
}
}
}

View file

@ -1,5 +1,5 @@
const { Command } = require('discord-akairo');
const fs = require('fs');
const Tag = require('../../models').Tag;
class UnTagCommand extends Command {
constructor() {
@ -28,33 +28,13 @@ class UnTagCommand extends Command {
}
async exec(message, args) {
let trigger = args.trigger;
trigger = trigger.toLowerCase();
let customresponse = {};
let json = JSON.stringify(customresponse);
fs.readFile(`./tag/${message.guild.id}.json`, 'utf8', function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
customresponse = JSON.parse(data); //now it an object
delete customresponse[trigger];
json = JSON.stringify(customresponse); //convert it back to json
fs.writeFile(`./tag/${message.guild.id}.json`, json, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
}
});
return message.channel.send(`The following autoresponse have been deleted: ${trigger}`);
const tag = await Tag.findOne({where: {trigger: args.trigger, serverID: message.guild.id}});
if (tag) {
Tag.destroy({where: {trigger: args.trigger, serverID: message.guild.id}});
return message.channel.send('Sucesffuly deleted the following tag: ' + args.trigger);
} else {
return message.channel.send('Did not find the specified tag, are you sure it exist?');
}
}
}

View file

@ -1,11 +1,9 @@
const { Listener } = require('discord-akairo');
const { MessageEmbed } = require('discord.js');
const responseObject = require('../../json/reply.json');
const reactObject = require('../../json/react.json');
const imgResponseObject = require('../../json/imgreply.json');
const rand = require('../../rand.js');
const reload = require('auto-reload');
const fs = require('fs');
const Tag = require('../../models').Tag;
const autoResponse = require('../../models').autoresponse;
const autoResponseStat = require('../../models').autoresponseStat;
class messageListener extends Listener {
constructor() {
@ -16,162 +14,156 @@ class messageListener extends Listener {
}
async exec(message) {
if (message.author.bot) return; {
let autoresponse = reload('../../json/autoresponse.json');
let message_content = message.content.toLowerCase();
if (message.author.bot) return;
const autoresponseStat = await autoResponseStat.findOne({where: {serverID: message.guild.id}});
if (autoresponseStat) {
// If autoresponse is enable send the response
if(autoresponse[message.channel.id] == 'enable') {
// Reply with images as attachement
if(imgResponseObject[message_content]) {
message.channel.send({files: [imgResponseObject[message_content]]});
}
// React only to the messages
else if(reactObject[message_content]) {
message.react(reactObject[message_content]);
}
// auto respond to messages
else if(responseObject[message_content]) {
message.channel.send(responseObject[message_content]);
// If it contain 'like if' react with 👍
} else if (message_content.includes('like if')) {
message.react('\u{1F44D}');
// If it contain 'jeff' react with a jeff emote
} else if (message_content.includes('jeff')) {
message.react('496028845967802378');
if(autoresponseStat.get('stat') == 'enable' && autoresponseStat.get('serverID') == message.guild.id) {
// Reply with images as attachement
const autoresponse = await autoResponse.findOne({where: {trigger: message.content.toLowerCase()}});
if (autoresponse) {
autoResponse.findOne({where: {trigger: message.content.toLowerCase()}});
let trigger = autoresponse.get('trigger');
let type = autoresponse.get('type');
let content = autoresponse.get('response');
if (trigger == message.content.toLowerCase() && type == 'text') {
return message.channel.send(content);
} else if (trigger == message.content.toLowerCase() && type == 'react') {
return message.react(content);
} else if (trigger == message.content.toLowerCase() && type == 'image') {
return message.channel.send({files: [content]});
}
}
}
// User autoresponse
if (fs.existsSync(`./tag/${message.guild.id}.json`)) {
let customresponse = reload(`../../tag/${message.guild.id}.json`);
const tag = await Tag.findOne({where: {trigger: message.content.toLowerCase(), serverID: message.guild.id}});
if (tag) {
Tag.findOne({where: {trigger: message.content.toLowerCase(), serverID: message.guild.id}});
let text = tag.get('response');
if (text.includes('[ban]')) {
message.member.ban('Tag ban :^)');
} else if (text.includes('[kick]')) {
message.member.kick('Tag kick :^)');
} else if (text.includes('[delete]')) {
message.delete('Tag delete :^)');
}
if(customresponse[message_content]) {
let text = customresponse[message_content];
if (text.includes('[ban]')) {
message.member.ban('Tag ban :^)');
} else if (text.includes('[kick]')) {
message.member.kick('Tag kick :^)');
} else if (text.includes('[delete]')) {
message.delete('Tag delete :^)');
}
text = rand.random(text, message);
text = rand.random(text, message);
let attach = '';
let attach = '';
if (text.includes('[attach:')) {
attach = text.split(/(\[attach:.*?])/);
for (let i = 0, l = attach.length; i < l; i++) {
if (attach[i].includes('[attach:')) {
attach = attach[i].replace('[attach:', '').slice(0, -1);
i = attach.length;
}
}
text = text.replace(/(\[attach:.*?])/, '');
}
// THIS SECTION IS VERY VERY BAD MUST CHANGE
if (text.includes('[embed]')) {
text = text.replace(/\[embed\]/, ' ');
let title = '';
let desc = '';
let image;
let thumbnail;
let footer = '';
let color;
if (text.includes('[embedImage:')) {
image = text.split(/(\[embedImage:.*?])/);
for (let i = 0, l = image.length; i < l; i++) {
if (image[i].includes('[embedImage:')) {
image = image[i].replace('[embedImage:', '').slice(0, -1);
text = text.replace(/(\[embedimage:.*?])/g, '');
i = image.length;
}
}
}
if (text.includes('[embedThumbnail:')) {
thumbnail = text.split(/(\[embedThumbnail:.*?])/);
for (let i = 0, l = thumbnail.length; i < l; i++) {
if (thumbnail[i].includes('[embedThumbnail:')) {
thumbnail = thumbnail[i].replace('[embedThumbnail:', '').slice(0, -1);
text = text.replace(/(\[embedThumbnail:.*?])/g, '');
i = thumbnail.length;
}
}
}
if (text.includes('[embedColor:')) {
color = text.split(/(\[embedColor:.*?])/);
for (let i = 0, l = color.length; i < l; i++) {
if (color[i].includes('[embedColor:')) {
color = color[i].replace('[embedColor:', '').slice(0, -1);
text = text.replace(/(\[embedColor:.*?])/g, '');
i = color.length;
}
}
}
if (text.includes('[embedTitle:')) {
title = text.split(/(\[embedTitle:.*?])/);
for (let i = 0, l = title.length; i < l; i++) {
if (title[i].includes('[embedTitle:')) {
title = title[i].replace('[embedTitle:', '').slice(0, -1);
text = text.replace(/(\[embedTitle:.*?])/g, '');
i = title.length;
}
}
}
if (text.includes('[embedFooter:')) {
footer = text.split(/(\[embedFooter:.*?])/);
for (let i = 0, l = footer.length; i < l; i++) {
if (footer[i].includes('[embedFooter:')) {
footer = footer[i].replace('[embedFooter:', '').slice(0, -1);
text = text.replace(/(\[embedFooter:.*?])/g, '');
i = footer.length;
}
}
}
if (text.includes('[embedDesc:')) {
desc = text.split(/(\[embedDesc:.*?])/);
for (let i = 0, l = desc.length; i < l; i++) {
if (desc[i].includes('[embedDesc:')) {
desc = desc[i].replace('[embedDesc:', '').slice(0, -1);
i = desc.length;
}
}
}
const embed = new MessageEmbed()
.setColor(color)
.setTitle(title)
.setImage(image)
.setThumbnail(thumbnail)
.setDescription(desc)
.setFooter(footer)
.setTimestamp();
if (attach) {
return message.channel.send(embed, {files: [attach]});
} else {
return message.channel.send(embed);
if (text.includes('[attach:')) {
attach = text.split(/(\[attach:.*?])/);
for (let i = 0, l = attach.length; i < l; i++) {
if (attach[i].includes('[attach:')) {
attach = attach[i].replace('[attach:', '').slice(0, -1);
i = attach.length;
}
}
text = text.replace(/(\[attach:.*?])/, '');
}
// THIS SECTION IS VERY VERY BAD MUST CHANGE
if (text.includes('[embed]')) {
text = text.replace(/\[embed\]/, ' ');
let title = '';
let desc = '';
let image;
let thumbnail;
let footer = '';
let color;
if (text.includes('[embedImage:')) {
image = text.split(/(\[embedImage:.*?])/);
for (let i = 0, l = image.length; i < l; i++) {
if (image[i].includes('[embedImage:')) {
image = image[i].replace('[embedImage:', '').slice(0, -1);
text = text.replace(/(\[embedimage:.*?])/g, '');
i = image.length;
}
}
}
if (text.includes('[embedThumbnail:')) {
thumbnail = text.split(/(\[embedThumbnail:.*?])/);
for (let i = 0, l = thumbnail.length; i < l; i++) {
if (thumbnail[i].includes('[embedThumbnail:')) {
thumbnail = thumbnail[i].replace('[embedThumbnail:', '').slice(0, -1);
text = text.replace(/(\[embedThumbnail:.*?])/g, '');
i = thumbnail.length;
}
}
}
if (text.includes('[embedColor:')) {
color = text.split(/(\[embedColor:.*?])/);
for (let i = 0, l = color.length; i < l; i++) {
if (color[i].includes('[embedColor:')) {
color = color[i].replace('[embedColor:', '').slice(0, -1);
text = text.replace(/(\[embedColor:.*?])/g, '');
i = color.length;
}
}
}
if (text.includes('[embedTitle:')) {
title = text.split(/(\[embedTitle:.*?])/);
for (let i = 0, l = title.length; i < l; i++) {
if (title[i].includes('[embedTitle:')) {
title = title[i].replace('[embedTitle:', '').slice(0, -1);
text = text.replace(/(\[embedTitle:.*?])/g, '');
i = title.length;
}
}
}
if (text.includes('[embedFooter:')) {
footer = text.split(/(\[embedFooter:.*?])/);
for (let i = 0, l = footer.length; i < l; i++) {
if (footer[i].includes('[embedFooter:')) {
footer = footer[i].replace('[embedFooter:', '').slice(0, -1);
text = text.replace(/(\[embedFooter:.*?])/g, '');
i = footer.length;
}
}
}
if (text.includes('[embedDesc:')) {
desc = text.split(/(\[embedDesc:.*?])/);
for (let i = 0, l = desc.length; i < l; i++) {
if (desc[i].includes('[embedDesc:')) {
desc = desc[i].replace('[embedDesc:', '').slice(0, -1);
i = desc.length;
}
}
}
const embed = new MessageEmbed()
.setColor(color)
.setTitle(title)
.setImage(image)
.setThumbnail(thumbnail)
.setDescription(desc)
.setFooter(footer)
.setTimestamp();
if (attach) {
return message.channel.send(text, {files: [attach]});
return message.channel.send(embed, {files: [attach]});
} else {
return message.channel.send(text);
return message.channel.send(embed);
}
}
return message.channel.send(text);
}
}
}

View file

@ -0,0 +1,39 @@
/* eslint-disable no-unused-vars */
/* eslint-disable indent */
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Tags', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
trigger: {
type: Sequelize.TEXT,
unique: true
},
response: {
type: Sequelize.TEXT,
},
ownerID: {
type: Sequelize.BIGINT,
},
serverID: {
type: Sequelize.BIGINT,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Tags');
}
};

View file

@ -0,0 +1,35 @@
/* eslint-disable no-unused-vars */
/* eslint-disable indent */
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('autoresponses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
trigger: {
type: Sequelize.TEXT
},
response: {
type: Sequelize.TEXT
},
type: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('autoresponses');
}
};

View file

@ -0,0 +1,32 @@
/* eslint-disable no-unused-vars */
/* eslint-disable indent */
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('autoresponseStats', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
serverID: {
type: Sequelize.BIGINT
},
stat: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('autoresponseStats');
}
};

14
models/autoresponse.js Normal file
View file

@ -0,0 +1,14 @@
/* eslint-disable no-unused-vars */
/* eslint-disable indent */
'use strict';
module.exports = (sequelize, DataTypes) => {
const autoresponse = sequelize.define('autoresponse', {
trigger: DataTypes.STRING,
response: DataTypes.STRING,
type: DataTypes.STRING
}, {});
autoresponse.associate = function(models) {
// associations can be defined here
};
return autoresponse;
};

View file

@ -0,0 +1,13 @@
/* eslint-disable no-unused-vars */
/* eslint-disable indent */
'use strict';
module.exports = (sequelize, DataTypes) => {
const autoresponseStat = sequelize.define('autoresponseStat', {
serverID: DataTypes.STRING,
stat: DataTypes.STRING
}, {});
autoresponseStat.associate = function(models) {
// associations can be defined here
};
return autoresponseStat;
};

39
models/index.js Normal file
View file

@ -0,0 +1,39 @@
/* eslint-disable no-unused-vars */
/* eslint-disable indent */
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;

15
models/tag.js Normal file
View file

@ -0,0 +1,15 @@
/* eslint-disable no-unused-vars */
/* eslint-disable indent */
'use strict';
module.exports = (sequelize, DataTypes) => {
const Tag = sequelize.define('Tag', {
trigger: DataTypes.STRING,
response: DataTypes.STRING,
ownerID: DataTypes.BIGINT,
serverID: DataTypes.BIGINT
}, {});
Tag.associate = function(models) {
// associations can be defined here
};
return Tag;
};