2017-02-10 00:04:23 -05:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2018-02-11 14:54:30 -05:00
|
|
|
const config = require('../config');
|
2017-02-10 00:04:23 -05:00
|
|
|
|
2018-02-11 14:54:30 -05:00
|
|
|
module.exports = bot => {
|
2017-02-10 00:04:23 -05:00
|
|
|
if (! config.enableGreeting) return;
|
|
|
|
|
|
|
|
bot.on('guildMemberAdd', (guild, member) => {
|
2019-06-09 08:56:04 -04:00
|
|
|
const guildGreeting = config.guildGreetings[guild.id];
|
|
|
|
if (! guildGreeting || (! guildGreeting.message && ! guildGreeting.attachment)) return;
|
2017-02-10 00:04:23 -05:00
|
|
|
|
2019-06-09 08:56:04 -04:00
|
|
|
function sendGreeting(message, file) {
|
2017-02-10 00:04:23 -05:00
|
|
|
bot.getDMChannel(member.id).then(channel => {
|
|
|
|
if (! channel) return;
|
2018-04-21 07:31:58 -04:00
|
|
|
|
2019-06-09 08:56:04 -04:00
|
|
|
channel.createMessage(message || '', file)
|
2018-04-21 07:31:58 -04:00
|
|
|
.catch(e => {
|
|
|
|
if (e.code === 50007) return;
|
|
|
|
throw e;
|
|
|
|
});
|
2017-02-10 00:04:23 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-09 08:56:04 -04:00
|
|
|
if (guildGreeting.attachment) {
|
|
|
|
const filename = path.basename(guildGreeting.attachment);
|
|
|
|
fs.readFile(guildGreeting.attachment, (err, data) => {
|
2017-02-10 00:04:23 -05:00
|
|
|
const file = {file: data, name: filename};
|
2019-06-09 08:56:04 -04:00
|
|
|
sendGreeting(guildGreeting.message, file);
|
2017-02-10 00:04:23 -05:00
|
|
|
});
|
|
|
|
} else {
|
2019-06-09 08:56:04 -04:00
|
|
|
sendGreeting(guildGreeting.message);
|
2017-02-10 00:04:23 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|