switch to mysql
This commit is contained in:
parent
e64f4a9eb1
commit
1b66983fb7
11 changed files with 349 additions and 255 deletions
|
@ -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);
|
||||
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 {
|
||||
|
||||
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);
|
||||
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('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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return message.channel.send(`Autoresponse have been ${text.toLowerCase()}d`);
|
||||
} else {
|
||||
return message.channel.send('You didin\'t type a valid input');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
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 {
|
||||
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('The tag already exist!');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return message.channel.send(`autoresponse have been set to ${trigger} : ${response}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
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 {
|
||||
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('Did not find the specified tag, are you sure it exist?');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return message.channel.send(`The following autoresponse have been deleted: ${trigger}`);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,37 +14,37 @@ 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') {
|
||||
if(autoresponseStat.get('stat') == 'enable' && autoresponseStat.get('serverID') == message.guild.id) {
|
||||
// Reply with images as attachement
|
||||
if(imgResponseObject[message_content]) {
|
||||
message.channel.send({files: [imgResponseObject[message_content]]});
|
||||
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]});
|
||||
}
|
||||
// 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');
|
||||
}
|
||||
|
||||
}
|
||||
// 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];
|
||||
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]')) {
|
||||
|
@ -165,16 +163,10 @@ class messageListener extends Listener {
|
|||
return message.channel.send(embed);
|
||||
}
|
||||
}
|
||||
|
||||
if (attach) {
|
||||
return message.channel.send(text, {files: [attach]});
|
||||
} else {
|
||||
return message.channel.send(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = messageListener;
|
39
migrations/20190707001840-create-tag.js
Normal file
39
migrations/20190707001840-create-tag.js
Normal 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');
|
||||
}
|
||||
};
|
35
migrations/20190708171608-create-autoresponse.js
Normal file
35
migrations/20190708171608-create-autoresponse.js
Normal 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');
|
||||
}
|
||||
};
|
32
migrations/20190708171617-create-autoresponse-stat.js
Normal file
32
migrations/20190708171617-create-autoresponse-stat.js
Normal 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
14
models/autoresponse.js
Normal 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;
|
||||
};
|
13
models/autoresponsestat.js
Normal file
13
models/autoresponsestat.js
Normal 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
39
models/index.js
Normal 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
15
models/tag.js
Normal 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;
|
||||
};
|
Loading…
Reference in a new issue