ramirez/src/modules/greeting.js

35 lines
1000 B
JavaScript
Raw Normal View History

2017-02-10 00:04:23 -05:00
const path = require('path');
const fs = require('fs');
const config = require('../config');
2017-02-10 00:04:23 -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;
2019-06-09 08:56:04 -04:00
channel.createMessage(message || '', file)
.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
}
});
};