diff --git a/commands/admin/autoresponse.js b/commands/admin/autoresponse.js index 8c71130e..30ca89b8 100644 --- a/commands/admin/autoresponse.js +++ b/commands/admin/autoresponse.js @@ -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'); - } } } diff --git a/commands/admin/tag.js b/commands/admin/tag.js index 5dbb737a..4f0086d1 100644 --- a/commands/admin/tag.js +++ b/commands/admin/tag.js @@ -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!'); + } } } diff --git a/commands/admin/untag.js b/commands/admin/untag.js index 9a290516..50d6d157 100644 --- a/commands/admin/untag.js +++ b/commands/admin/untag.js @@ -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?'); + } } } diff --git a/event/listeners/message.js b/event/listeners/message.js index f91ded29..e0cba688 100644 --- a/event/listeners/message.js +++ b/event/listeners/message.js @@ -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`); - - 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 :^)'); - } + 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 :^)'); + } - 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; + 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; } } - 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:.*?])/); + 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; - } + 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('[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); } } } diff --git a/migrations/20190707001840-create-tag.js b/migrations/20190707001840-create-tag.js new file mode 100644 index 00000000..23c09acc --- /dev/null +++ b/migrations/20190707001840-create-tag.js @@ -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'); + } +}; \ No newline at end of file diff --git a/migrations/20190708171608-create-autoresponse.js b/migrations/20190708171608-create-autoresponse.js new file mode 100644 index 00000000..e8fdefb2 --- /dev/null +++ b/migrations/20190708171608-create-autoresponse.js @@ -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'); + } +}; \ No newline at end of file diff --git a/migrations/20190708171617-create-autoresponse-stat.js b/migrations/20190708171617-create-autoresponse-stat.js new file mode 100644 index 00000000..9b615906 --- /dev/null +++ b/migrations/20190708171617-create-autoresponse-stat.js @@ -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'); + } +}; \ No newline at end of file diff --git a/models/autoresponse.js b/models/autoresponse.js new file mode 100644 index 00000000..b102d144 --- /dev/null +++ b/models/autoresponse.js @@ -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; +}; \ No newline at end of file diff --git a/models/autoresponsestat.js b/models/autoresponsestat.js new file mode 100644 index 00000000..cdd072dd --- /dev/null +++ b/models/autoresponsestat.js @@ -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; +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 00000000..3fc37192 --- /dev/null +++ b/models/index.js @@ -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; diff --git a/models/tag.js b/models/tag.js new file mode 100644 index 00000000..86d1c2ab --- /dev/null +++ b/models/tag.js @@ -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; +}; \ No newline at end of file