From fc74a7e74658ccb72b099ee9234d38743f951525 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Wed, 27 Nov 2019 12:16:30 +0100 Subject: [PATCH] Use sql instead of json --- commands/admin/bye.js | 53 ++++++++++++------- commands/admin/welcome.js | 53 ++++++++++++------- commands/fun/fakejoin.js | 41 ++++++-------- commands/fun/fakeleave.js | 22 +++----- event/listeners/guildMemberAdd.js | 11 ++-- event/listeners/guildMemberRemove.js | 11 ++-- .../20191127105740-create-join-channel.js | 33 ++++++++++++ .../20191127105810-create-leave-channel.js | 33 ++++++++++++ models/joinchannel.js | 12 +++++ models/leavechannel.js | 12 +++++ 10 files changed, 195 insertions(+), 86 deletions(-) create mode 100644 migrations/20191127105740-create-join-channel.js create mode 100644 migrations/20191127105810-create-leave-channel.js create mode 100644 models/joinchannel.js create mode 100644 models/leavechannel.js diff --git a/commands/admin/bye.js b/commands/admin/bye.js index ea72b0b8..c3f97099 100644 --- a/commands/admin/bye.js +++ b/commands/admin/bye.js @@ -1,5 +1,5 @@ const { Command } = require('discord-akairo'); -const fs = require('fs'); +const leaveChannel = require('../../models').leaveChannel; class byeCommand extends Command { constructor() { @@ -31,27 +31,44 @@ class byeCommand extends Command { } async exec(message, args) { - let byeChannel = message.channel.id; + const leave = await leaveChannel.findOne({where: {guildID: message.guild.id}}); if (args.remove) { - fs.unlink(`./welcome/${message.guild.id}.json`, (err) => { - if (err) { - console.error(err); - return message.channel.send('An error has occured, there is most likely no welcome message set!'); - } else { - return message.channel.send('Disabled unwelcome message'); - } - }); + if (leave) { + leave.destroy({where: {guildID: message.guild.id, channelID: message.channel.id}}); + return message.channel.send('successfully deleted the leave message'); + } else { + return message.channel.send('Did not find the a leave message, are you sure you have one setup?'); + } } - fs.writeFile(`./bye/${message.guild.id}.json`, `{"channel": "${byeChannel}", "message": "${args.message}"}`, function (err) { - if (err) { - console.log(err); - return message.channel.send('An error has occured! im gonna be honest with you, i do not know what happened yet! but fear not! i will look into it!'); - } - }); - - return message.channel.send(`This channel will now be used to send message when user leave with the following message: ${args.message}`); + if (!args.message) { + return message.channel.send('Please provide a message'); + } + + if (!leave) { + const body = {guildID: message.guild.id, channelID: message.channel.id, message: args.message}; + await leaveChannel.create(body); + return message.channel.send(`The leave message have been set with ${args.message}`); + } else { + message.channel.send('The server already have a leave message, do you want to replace it? y/n'); + const filter = m => m.content && m.author.id == message.author.id; + message.channel.awaitMessages(filter, {time: 5000, max: 1, errors: ['time'] }) + .then(async messages => { + let messageContent = messages.map(messages => messages.content); + if (messageContent == 'y' || messageContent == 'yes') { + const body = {guildID: message.guild.id, channelID: message.channel.id, message: args.message}; + await leave.update(body, {where: {guildID: message.guild.id}}); + return message.channel.send(`The leave message have been set with ${args.message}`); + } else { + return message.channel.send('Not updating.'); + } + }) + .catch(err => { + console.error(err); + return message.channel.send('Took too long to answer. didin\'t update anything.'); + }); + } } } diff --git a/commands/admin/welcome.js b/commands/admin/welcome.js index 7ecfd8e4..171fbbe1 100644 --- a/commands/admin/welcome.js +++ b/commands/admin/welcome.js @@ -1,5 +1,5 @@ const { Command } = require('discord-akairo'); -const fs = require('fs'); +const joinChannel = require('../../models').joinChannel; class welcomeCommand extends Command { constructor() { @@ -30,27 +30,44 @@ class welcomeCommand extends Command { } async exec(message, args) { - let welcomeChannel = message.channel.id; + const join = await joinChannel.findOne({where: {guildID: message.guild.id}}); if (args.remove) { - fs.unlink(`./welcome/${message.guild.id}.json`, (err) => { - if (err) { - console.error(err); - return message.channel.send('An error has occured, there is most likely no welcome message set!'); - } else { - return message.channel.send('Disabled unwelcome message'); - } - }); + if (join) { + join.destroy({where: {guildID: message.guild.id, channelID: message.channel.id}}); + return message.channel.send('successfully deleted the join message'); + } else { + return message.channel.send('Did not find the a join message, are you sure you have one setup?'); + } } - fs.writeFile(`./welcome/${message.guild.id}.json`, `{"channel": "${welcomeChannel}", "message": "${args.message}"}`, function (err) { - if (err) { - console.log(err); - return message.channel.send('An error has occured! im gonna be honest with you, i do not know what happened yet! but fear not! i will look into it!'); - } - }); - - return message.channel.send(`This channel will now be used to welcome new user with the following message: ${args.message}`); + if (!args.message) { + return message.channel.send('Please provide a message'); + } + + if (!join) { + const body = {guildID: message.guild.id, channelID: message.channel.id, message: args.message}; + await joinChannel.create(body); + return message.channel.send(`The join message have been set with ${args.message}`); + } else { + message.channel.send('The server already have a join message, do you want to replace it? y/n'); + const filter = m => m.content && m.author.id == message.author.id; + message.channel.awaitMessages(filter, {time: 5000, max: 1, errors: ['time'] }) + .then(async messages => { + let messageContent = messages.map(messages => messages.content); + if (messageContent == 'y' || messageContent == 'yes') { + const body = {guildID: message.guild.id, channelID: message.channel.id, message: args.message}; + await join.update(body, {where: {guildID: message.guild.id}}); + return message.channel.send(`The join message have been set with ${args.message}`); + } else { + return message.channel.send('Not updating.'); + } + }) + .catch(err => { + console.error(err); + return message.channel.send('Took too long to answer. didin\'t update anything.'); + }); + } } } diff --git a/commands/fun/fakejoin.js b/commands/fun/fakejoin.js index 0cdae1aa..8ef07188 100644 --- a/commands/fun/fakejoin.js +++ b/commands/fun/fakejoin.js @@ -1,5 +1,5 @@ const { Command } = require('discord-akairo'); -const fs = require('fs'); +const joinChannel = require('../../models').joinChannel; const rand = require('../../rand.js'); class fakejoinCommand extends Command { @@ -25,45 +25,38 @@ class fakejoinCommand extends Command { } async exec(message, args) { - if (fs.existsSync(`./welcome/${message.guild.id}.json`)) { - let member; - if (args.member) { - member = args.member; - } else { - member = message.author.username; - } + const join = await joinChannel.findOne({where: {guildID: message.guild.id}}); - let welcome = require(`../../welcome/${message.guild.id}.json`); + if (join) { + const channel = this.client.channels.get(join.get('channelID')); - const channel = this.client.channels.get(welcome['channel']); - - let byeMessage = welcome['message']; - - byeMessage = byeMessage.replace(/\[member\]/, member); - byeMessage = byeMessage.replace(/\[server\]/, message.guild.name); + let welcomeMessage = join.get('message'); + welcomeMessage = welcomeMessage.replace(/\[member\]/, args.member); + welcomeMessage = welcomeMessage.replace(/\[server\]/, message.guild.name); + let attach; - if (byeMessage.includes('[attach:')) { - attach = byeMessage.split(/(\[attach:.*?])/); + if (welcomeMessage.includes('[attach:')) { + attach = welcomeMessage.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; } } - byeMessage = byeMessage.replace(/(\[attach:.*?])/, ''); + welcomeMessage = welcomeMessage.replace(/(\[attach:.*?])/, ''); } - - byeMessage = rand.random(byeMessage); - + + welcomeMessage = rand.random(welcomeMessage); + message.delete(); if (attach) { - return channel.send(byeMessage, {files: [attach]}); + return channel.send(welcomeMessage, {files: [attach]}); } else { - return channel.send(byeMessage); + return channel.send(welcomeMessage); } } else { - return message.channel.send('The server need a join message first!'); + return message.channel.send('Are you sure this server have a join message?'); } } } diff --git a/commands/fun/fakeleave.js b/commands/fun/fakeleave.js index 4d227cbe..f5daa965 100644 --- a/commands/fun/fakeleave.js +++ b/commands/fun/fakeleave.js @@ -1,5 +1,5 @@ const { Command } = require('discord-akairo'); -const fs = require('fs'); +const leaveChannel = require('../../models').leaveChannel; const rand = require('../../rand.js'); class fakeleaveCommand extends Command { @@ -25,21 +25,13 @@ class fakeleaveCommand extends Command { } async exec(message, args) { - if (fs.existsSync(`./bye/${message.guild.id}.json`)) { - let member; - if (args.member) { - member = args.member.username; - } else { - member = message.author.username; - } + const leave = await leaveChannel.findOne({where: {guildID: message.guild.id}}); + if (leave) { + const channel = this.client.channels.get(leave.get('channelID')); - let bye = require(`../../bye/${message.guild.id}.json`); + let byeMessage = leave.get('message'); - const channel = this.client.channels.get(bye['channel']); - - let byeMessage = bye['message']; - - byeMessage = byeMessage.replace(/\[member\]/, member); + byeMessage = byeMessage.replace(/\[member\]/, args.member); byeMessage = byeMessage.replace(/\[server\]/, message.guild.name); let attach; @@ -63,7 +55,7 @@ class fakeleaveCommand extends Command { return channel.send(byeMessage); } } else { - return message.channel.send('The server need a leave message first!'); + return message.channel.send('Are you sure this server have a leave message?'); } } } diff --git a/event/listeners/guildMemberAdd.js b/event/listeners/guildMemberAdd.js index 0d54c05a..6532ba6a 100644 --- a/event/listeners/guildMemberAdd.js +++ b/event/listeners/guildMemberAdd.js @@ -1,5 +1,5 @@ const { Listener } = require('discord-akairo'); -const fs = require('fs'); +const joinChannel = require('../../models').joinChannel; const rand = require('../../rand.js'); class guildMemberAddListener extends Listener { @@ -15,11 +15,12 @@ class guildMemberAddListener extends Listener { guild.setNickname('fart piss'); } - if (fs.existsSync(`./welcome/${guild.guild.id}.json`)) { - let welcome = require(`../../welcome/${guild.guild.id}.json`); - const channel = this.client.channels.get(welcome['channel']); + const join = await joinChannel.findOne({where: {guildID: guild.id}}); - let welcomeMessage = welcome['message']; + if (join) { + const channel = this.client.channels.get(join.get('channelID')); + + let welcomeMessage = join.get('message'); welcomeMessage = welcomeMessage.replace(/\[member\]/, guild.user.username); welcomeMessage = welcomeMessage.replace(/\[server\]/, guild.guild.name); diff --git a/event/listeners/guildMemberRemove.js b/event/listeners/guildMemberRemove.js index 1a6c8e5f..1d213ab6 100644 --- a/event/listeners/guildMemberRemove.js +++ b/event/listeners/guildMemberRemove.js @@ -1,5 +1,5 @@ const { Listener } = require('discord-akairo'); -const fs = require('fs'); +const leaveChannel = require('../../models').leaveChannel; const rand = require('../../rand.js'); class guildMemberRemoveListener extends Listener { @@ -11,12 +11,11 @@ class guildMemberRemoveListener extends Listener { } async exec(guild) { - if (fs.existsSync(`./bye/${guild.guild.id}.json`)) { - let bye = require(`../../bye/${guild.guild.id}.json`); + const leave = await leaveChannel.findOne({where: {guildID: guild.id}}); + if (leave) { + const channel = this.client.channels.get(leave.get('channelID')); - const channel = this.client.channels.get(bye['channel']); - - let byeMessage = bye['message']; + let byeMessage = leave.get('message'); byeMessage = byeMessage.replace(/\[member\]/, guild.user.username); byeMessage = byeMessage.replace(/\[server\]/, guild.guild.name); diff --git a/migrations/20191127105740-create-join-channel.js b/migrations/20191127105740-create-join-channel.js new file mode 100644 index 00000000..3a494a30 --- /dev/null +++ b/migrations/20191127105740-create-join-channel.js @@ -0,0 +1,33 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('joinChannels', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + channelID: { + type: Sequelize.BIGINT + }, + guildID: { + type: Sequelize.BIGINT + }, + message: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('joinChannels'); + } +}; \ No newline at end of file diff --git a/migrations/20191127105810-create-leave-channel.js b/migrations/20191127105810-create-leave-channel.js new file mode 100644 index 00000000..054abe4f --- /dev/null +++ b/migrations/20191127105810-create-leave-channel.js @@ -0,0 +1,33 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.createTable('leaveChannels', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + channelID: { + type: Sequelize.BIGINT + }, + guildID: { + type: Sequelize.BIGINT + }, + message: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('leaveChannels'); + } +}; \ No newline at end of file diff --git a/models/joinchannel.js b/models/joinchannel.js new file mode 100644 index 00000000..6269a24e --- /dev/null +++ b/models/joinchannel.js @@ -0,0 +1,12 @@ +'use strict'; +module.exports = (sequelize, DataTypes) => { + const joinChannel = sequelize.define('joinChannel', { + channelID: DataTypes.BIGINT, + guildID: DataTypes.BIGINT, + message: DataTypes.STRING + }, {}); + joinChannel.associate = function(models) { + // associations can be defined here + }; + return joinChannel; +}; \ No newline at end of file diff --git a/models/leavechannel.js b/models/leavechannel.js new file mode 100644 index 00000000..c942c666 --- /dev/null +++ b/models/leavechannel.js @@ -0,0 +1,12 @@ +'use strict'; +module.exports = (sequelize, DataTypes) => { + const leaveChannel = sequelize.define('leaveChannel', { + channelID: DataTypes.BIGINT, + guildID: DataTypes.BIGINT, + message: DataTypes.STRING + }, {}); + leaveChannel.associate = function(models) { + // associations can be defined here + }; + return leaveChannel; +}; \ No newline at end of file