Haha-Yes/event/listeners/guildMemberRemove.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

const { Listener } = require('discord-akairo');
2019-11-27 12:16:30 +01:00
const leaveChannel = require('../../models').leaveChannel;
const rand = require('../../rand.js');
class guildMemberRemoveListener extends Listener {
constructor() {
super('guildMemberRemove', {
emitter: 'client',
event: 'guildMemberRemove'
});
}
2020-03-07 17:59:09 +01:00
async exec(member) {
const leave = await leaveChannel.findOne({where: {guildID: member.guild.id}});
2019-11-27 12:16:30 +01:00
if (leave) {
const channel = this.client.channels.resolve(leave.get('channelID'));
2019-11-27 12:16:30 +01:00
let byeMessage = leave.get('message');
2019-12-26 17:27:51 +01:00
let invite = new RegExp(/(https?:\/\/)?(www\.)?discord(?:app\.com|\.gg)[/invite/]?(?:(?!.*[Ii10OolL]).[a-zA-Z0-9]{5,6}|[a-zA-Z0-9-]{2,32})/g);
2020-03-07 17:59:09 +01:00
let username = member.user.username;
let user = member.user;
2019-12-26 17:29:22 +01:00
if (username.match(invite)) {
2019-12-26 17:27:51 +01:00
username = username.replace(/(https?:\/\/)?(www\.)?discord(?:app\.com|\.gg)[/invite/]?(?:(?!.*[Ii10OolL]).[a-zA-Z0-9]{5,6}|[a-zA-Z0-9-]{2,32})/g, '[REDACTED]');
user = username;
}
byeMessage = byeMessage.replace(/\[member\]/, username);
byeMessage = byeMessage.replace(/\[memberPing\]/, user);
2020-03-07 17:59:09 +01:00
byeMessage = byeMessage.replace(/\[server\]/, member.guild.name);
2019-06-28 17:05:01 +02:00
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;
}
}
2019-06-28 17:05:01 +02:00
byeMessage = byeMessage.replace(/(\[attach:.*?])/, '');
}
2019-06-28 17:05:01 +02:00
byeMessage = rand.random(byeMessage);
2019-06-28 17:05:01 +02:00
if (attach) {
return channel.send(byeMessage, {files: [attach]});
} else {
return channel.send(byeMessage);
}
}
}
}
module.exports = guildMemberRemoveListener;