Use sql instead of json

This commit is contained in:
loicbersier 2019-11-27 12:16:30 +01:00
parent d158928bfb
commit fc74a7e746
10 changed files with 195 additions and 86 deletions

View file

@ -1,5 +1,5 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const fs = require('fs'); const leaveChannel = require('../../models').leaveChannel;
class byeCommand extends Command { class byeCommand extends Command {
constructor() { constructor() {
@ -31,27 +31,44 @@ class byeCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
let byeChannel = message.channel.id; const leave = await leaveChannel.findOne({where: {guildID: message.guild.id}});
if (args.remove) { if (args.remove) {
fs.unlink(`./welcome/${message.guild.id}.json`, (err) => { if (leave) {
if (err) { leave.destroy({where: {guildID: message.guild.id, channelID: message.channel.id}});
console.error(err); return message.channel.send('successfully deleted the leave message');
return message.channel.send('An error has occured, there is most likely no welcome message set!'); } else {
} else { return message.channel.send('Did not find the a leave message, are you sure you have one setup?');
return message.channel.send('Disabled unwelcome message'); }
}
});
} }
fs.writeFile(`./bye/${message.guild.id}.json`, `{"channel": "${byeChannel}", "message": "${args.message}"}`, function (err) { if (!args.message) {
if (err) { return message.channel.send('Please provide a message');
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 (!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.');
});
}
} }
} }

View file

@ -1,5 +1,5 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const fs = require('fs'); const joinChannel = require('../../models').joinChannel;
class welcomeCommand extends Command { class welcomeCommand extends Command {
constructor() { constructor() {
@ -30,27 +30,44 @@ class welcomeCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
let welcomeChannel = message.channel.id; const join = await joinChannel.findOne({where: {guildID: message.guild.id}});
if (args.remove) { if (args.remove) {
fs.unlink(`./welcome/${message.guild.id}.json`, (err) => { if (join) {
if (err) { join.destroy({where: {guildID: message.guild.id, channelID: message.channel.id}});
console.error(err); return message.channel.send('successfully deleted the join message');
return message.channel.send('An error has occured, there is most likely no welcome message set!'); } else {
} else { return message.channel.send('Did not find the a join message, are you sure you have one setup?');
return message.channel.send('Disabled unwelcome message'); }
}
});
} }
fs.writeFile(`./welcome/${message.guild.id}.json`, `{"channel": "${welcomeChannel}", "message": "${args.message}"}`, function (err) { if (!args.message) {
if (err) { return message.channel.send('Please provide a message');
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 (!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.');
});
}
} }
} }

View file

@ -1,5 +1,5 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const fs = require('fs'); const joinChannel = require('../../models').joinChannel;
const rand = require('../../rand.js'); const rand = require('../../rand.js');
class fakejoinCommand extends Command { class fakejoinCommand extends Command {
@ -25,45 +25,38 @@ class fakejoinCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
if (fs.existsSync(`./welcome/${message.guild.id}.json`)) { const join = await joinChannel.findOne({where: {guildID: message.guild.id}});
let member;
if (args.member) {
member = args.member;
} else {
member = message.author.username;
}
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 welcomeMessage = join.get('message');
let byeMessage = welcome['message']; welcomeMessage = welcomeMessage.replace(/\[member\]/, args.member);
welcomeMessage = welcomeMessage.replace(/\[server\]/, message.guild.name);
byeMessage = byeMessage.replace(/\[member\]/, member);
byeMessage = byeMessage.replace(/\[server\]/, message.guild.name);
let attach; let attach;
if (byeMessage.includes('[attach:')) { if (welcomeMessage.includes('[attach:')) {
attach = byeMessage.split(/(\[attach:.*?])/); attach = welcomeMessage.split(/(\[attach:.*?])/);
for (let i = 0, l = attach.length; i < l; i++) { for (let i = 0, l = attach.length; i < l; i++) {
if (attach[i].includes('[attach:')) { if (attach[i].includes('[attach:')) {
attach = attach[i].replace('[attach:', '').slice(0, -1); attach = attach[i].replace('[attach:', '').slice(0, -1);
i = attach.length; i = attach.length;
} }
} }
byeMessage = byeMessage.replace(/(\[attach:.*?])/, ''); welcomeMessage = welcomeMessage.replace(/(\[attach:.*?])/, '');
} }
byeMessage = rand.random(byeMessage); welcomeMessage = rand.random(welcomeMessage);
message.delete(); message.delete();
if (attach) { if (attach) {
return channel.send(byeMessage, {files: [attach]}); return channel.send(welcomeMessage, {files: [attach]});
} else { } else {
return channel.send(byeMessage); return channel.send(welcomeMessage);
} }
} else { } 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?');
} }
} }
} }

View file

@ -1,5 +1,5 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const fs = require('fs'); const leaveChannel = require('../../models').leaveChannel;
const rand = require('../../rand.js'); const rand = require('../../rand.js');
class fakeleaveCommand extends Command { class fakeleaveCommand extends Command {
@ -25,21 +25,13 @@ class fakeleaveCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
if (fs.existsSync(`./bye/${message.guild.id}.json`)) { const leave = await leaveChannel.findOne({where: {guildID: message.guild.id}});
let member; if (leave) {
if (args.member) { const channel = this.client.channels.get(leave.get('channelID'));
member = args.member.username;
} else {
member = message.author.username;
}
let bye = require(`../../bye/${message.guild.id}.json`); let byeMessage = leave.get('message');
const channel = this.client.channels.get(bye['channel']); byeMessage = byeMessage.replace(/\[member\]/, args.member);
let byeMessage = bye['message'];
byeMessage = byeMessage.replace(/\[member\]/, member);
byeMessage = byeMessage.replace(/\[server\]/, message.guild.name); byeMessage = byeMessage.replace(/\[server\]/, message.guild.name);
let attach; let attach;
@ -63,7 +55,7 @@ class fakeleaveCommand extends Command {
return channel.send(byeMessage); return channel.send(byeMessage);
} }
} else { } 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?');
} }
} }
} }

View file

@ -1,5 +1,5 @@
const { Listener } = require('discord-akairo'); const { Listener } = require('discord-akairo');
const fs = require('fs'); const joinChannel = require('../../models').joinChannel;
const rand = require('../../rand.js'); const rand = require('../../rand.js');
class guildMemberAddListener extends Listener { class guildMemberAddListener extends Listener {
@ -15,11 +15,12 @@ class guildMemberAddListener extends Listener {
guild.setNickname('fart piss'); guild.setNickname('fart piss');
} }
if (fs.existsSync(`./welcome/${guild.guild.id}.json`)) { const join = await joinChannel.findOne({where: {guildID: guild.id}});
let welcome = require(`../../welcome/${guild.guild.id}.json`);
const channel = this.client.channels.get(welcome['channel']);
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(/\[member\]/, guild.user.username);
welcomeMessage = welcomeMessage.replace(/\[server\]/, guild.guild.name); welcomeMessage = welcomeMessage.replace(/\[server\]/, guild.guild.name);

View file

@ -1,5 +1,5 @@
const { Listener } = require('discord-akairo'); const { Listener } = require('discord-akairo');
const fs = require('fs'); const leaveChannel = require('../../models').leaveChannel;
const rand = require('../../rand.js'); const rand = require('../../rand.js');
class guildMemberRemoveListener extends Listener { class guildMemberRemoveListener extends Listener {
@ -11,12 +11,11 @@ class guildMemberRemoveListener extends Listener {
} }
async exec(guild) { async exec(guild) {
if (fs.existsSync(`./bye/${guild.guild.id}.json`)) { const leave = await leaveChannel.findOne({where: {guildID: guild.id}});
let bye = require(`../../bye/${guild.guild.id}.json`); if (leave) {
const channel = this.client.channels.get(leave.get('channelID'));
const channel = this.client.channels.get(bye['channel']); let byeMessage = leave.get('message');
let byeMessage = bye['message'];
byeMessage = byeMessage.replace(/\[member\]/, guild.user.username); byeMessage = byeMessage.replace(/\[member\]/, guild.user.username);
byeMessage = byeMessage.replace(/\[server\]/, guild.guild.name); byeMessage = byeMessage.replace(/\[server\]/, guild.guild.name);

View file

@ -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');
}
};

View file

@ -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');
}
};

12
models/joinchannel.js Normal file
View file

@ -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;
};

12
models/leavechannel.js Normal file
View file

@ -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;
};