From d78931859017f2755154c72820b04cf262af12f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bersier?= Date: Fri, 10 May 2019 16:36:17 +0200 Subject: [PATCH] Same as welcome but for people who leave --- commands/admin/bye.js | 39 ++++++++++++++++++++++++ commands/admin/unbye.js | 31 +++++++++++++++++++ event/listeners/guildMemberRemove.js | 45 ++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 commands/admin/bye.js create mode 100644 commands/admin/unbye.js create mode 100644 event/listeners/guildMemberRemove.js diff --git a/commands/admin/bye.js b/commands/admin/bye.js new file mode 100644 index 00000000..b2cc2aca --- /dev/null +++ b/commands/admin/bye.js @@ -0,0 +1,39 @@ +const { Command } = require('discord-akairo'); +const fs = require('fs'); + +class byeCommand extends Command { + constructor() { + super('bye', { + aliases: ['bye'], + category: 'admin', + channelRestriction: 'guild', + userPermissions: ['MANAGE_CHANNELS'], + args: [ + { + id: 'message', + type: 'string', + match: 'rest' + } + ], + description: { + content: 'Set bye message when user leave/get kicked, can use [member] to get the name of the user joining and [server] to get the name of the server', + usage: '[bye message]', + examples: ['everyone bye [adjectives] [member] and bye on [server]'] + } + }); + } + + async exec(message, args) { + let byeChannel = message.channel.id; + + fs.writeFile(`./bye/${message.guild.id}.json`, `{"channel": "${byeChannel}", "message": "${args.message}"}`, function (err) { + if (err) { + console.log(err); + } + }); + + return message.channel.send(`This channel will now be used to send message when user leave with the following message: ${args.message}`); + } +} + +module.exports = byeCommand; \ No newline at end of file diff --git a/commands/admin/unbye.js b/commands/admin/unbye.js new file mode 100644 index 00000000..95ea4c0e --- /dev/null +++ b/commands/admin/unbye.js @@ -0,0 +1,31 @@ +const { Command } = require('discord-akairo'); +const fs = require('fs'); + +class unbyeCommand extends Command { + constructor() { + super('unbye', { + aliases: ['unbye'], + category: 'admin', + channelRestriction: 'guild', + userPermissions: ['MANAGE_CHANNELS'], + description: { + content: 'Delete leaving message', + usage: '[]', + examples: [''] + } + }); + } + + async exec(message) { + fs.unlink(`./bye/${message.guild.id}.json`, (err) => { + if (err) { + console.error(err); + return message.channel.send('An error has occured, there is most likely no bye/leave message set!'); + } else { + return message.channel.send('Disabled bye/leave message'); + } + }); + } +} + +module.exports = unbyeCommand; \ No newline at end of file diff --git a/event/listeners/guildMemberRemove.js b/event/listeners/guildMemberRemove.js new file mode 100644 index 00000000..631c9c78 --- /dev/null +++ b/event/listeners/guildMemberRemove.js @@ -0,0 +1,45 @@ +const { Listener } = require('discord-akairo'); +const reload = require('auto-reload'); +const rand = require('../../rand.js'); + +class guildMemberRemoveListener extends Listener { + constructor() { + super('guildMemberRemove', { + emitter: 'client', + event: 'guildMemberRemove' + }); + } + + async exec(guild) { + let bye = reload(`../../bye/${guild.guild.id}.json`); + + const channel = this.client.channels.get(bye['channel']); + + let byeMessage = bye['message']; + + byeMessage = byeMessage.replace(/\[member\]/, guild.user.username); + byeMessage = byeMessage.replace(/\[server\]/, guild.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); + + + if (attach) { + return channel.send(byeMessage, {files: [attach]}); + } else { + return channel.send(byeMessage); + } + } +} +module.exports = guildMemberRemoveListener; \ No newline at end of file