use emit to trigger add/remove event

This commit is contained in:
loicbersier 2019-12-16 17:34:57 +01:00
parent 915bf38c07
commit 325687a244
2 changed files with 16 additions and 70 deletions

View file

@ -1,6 +1,4 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const joinChannel = require('../../models').joinChannel;
const rand = require('../../rand.js');
class fakejoinCommand extends Command { class fakejoinCommand extends Command {
constructor() { constructor() {
@ -11,8 +9,8 @@ class fakejoinCommand extends Command {
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'], clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
args: [ args: [
{ {
id: 'member', id: 'user',
type: 'string', type: 'user',
match: 'rest' match: 'rest'
} }
], ],
@ -25,39 +23,14 @@ class fakejoinCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
const join = await joinChannel.findOne({where: {guildID: message.guild.id}}); let member;
if (args.user)
member = message.guild.members.get(args.user.id);
else
member = message.guild.members.get(message.author.id);
if (join) { this.client.emit('guildMemberAdd', member);
const channel = this.client.channels.get(join.get('channelID'));
let welcomeMessage = join.get('message');
welcomeMessage = welcomeMessage.replace(/\[member\]/, args.member);
welcomeMessage = welcomeMessage.replace(/\[server\]/, message.guild.name);
let 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;
}
}
welcomeMessage = welcomeMessage.replace(/(\[attach:.*?])/, '');
}
welcomeMessage = rand.random(welcomeMessage);
message.delete();
if (attach) {
return channel.send(welcomeMessage, {files: [attach]});
} else {
return channel.send(welcomeMessage);
}
} else {
return message.channel.send('Are you sure this server have a join message?');
}
} }
} }

View file

@ -1,6 +1,4 @@
const { Command } = require('discord-akairo'); const { Command } = require('discord-akairo');
const leaveChannel = require('../../models').leaveChannel;
const rand = require('../../rand.js');
class fakeleaveCommand extends Command { class fakeleaveCommand extends Command {
constructor() { constructor() {
@ -11,7 +9,7 @@ class fakeleaveCommand extends Command {
clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'], clientPermissions: ['SEND_MESSAGES', 'ATTACH_FILES'],
args: [ args: [
{ {
id: 'member', id: 'user',
type: 'user', type: 'user',
match: 'rest' match: 'rest'
} }
@ -25,38 +23,13 @@ class fakeleaveCommand extends Command {
} }
async exec(message, args) { async exec(message, args) {
const leave = await leaveChannel.findOne({where: {guildID: message.guild.id}}); let member;
if (leave) { if (args.user)
const channel = this.client.channels.get(leave.get('channelID')); member = message.guild.members.get(args.user.id);
else
member = message.guild.members.get(message.author.id);
let byeMessage = leave.get('message'); this.client.emit('guildMemberRemove', member);
byeMessage = byeMessage.replace(/\[member\]/, args.member.username);
byeMessage = byeMessage.replace(/\[server\]/, message.guild.name);
let attach;
if (byeMessage.includes('[attach:')) {
attach = byeMessage.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:.*?])/, '');
}
byeMessage = rand.random(byeMessage);
message.delete();
if (attach) {
return channel.send(byeMessage, {files: [attach]});
} else {
return channel.send(byeMessage);
}
} else {
return message.channel.send('Are you sure this server have a leave message?');
}
} }
} }