Add new user greetings

master
Miikka Virtanen 2017-02-10 07:04:23 +02:00
parent 6a13724c6a
commit 513439746f
3 changed files with 71 additions and 14 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/.idea /.idea
/node_modules /node_modules
/config.json /config.json
/welcome.png

32
src/greeting.js Normal file
View File

@ -0,0 +1,32 @@
const path = require('path');
const fs = require('fs');
const config = require('../config');
function enable(bot) {
if (! config.enableGreeting) return;
bot.on('guildMemberAdd', (guild, member) => {
if (guild.id !== config.greetingGuildId) return;
function sendGreeting(file) {
bot.getDMChannel(member.id).then(channel => {
if (! channel) return;
channel.createMessage(config.greetingMessage, file);
});
}
if (config.greetingAttachment) {
const filename = path.basename(config.greetingAttachment);
fs.readFile(config.greetingAttachment, (err, data) => {
const file = {file: data, name: filename};
sendGreeting(file);
});
} else {
sendGreeting();
}
});
}
module.exports = {
enable,
};

View File

@ -9,6 +9,7 @@ const threads = require('./threads');
const logs = require('./logs'); const logs = require('./logs');
const attachments = require('./attachments'); const attachments = require('./attachments');
const webserver = require('./webserver'); const webserver = require('./webserver');
const greeting = require('./greeting');
const bot = new Eris.CommandClient(config.token, {}, { const bot = new Eris.CommandClient(config.token, {}, {
prefix: config.prefix || '!', prefix: config.prefix || '!',
@ -52,6 +53,9 @@ bot.on('messageCreate', msg => {
if (msg.author.id === bot.user.id) return; if (msg.author.id === bot.user.id) return;
if (msg.mentions.some(user => user.id === bot.user.id)) { if (msg.mentions.some(user => user.id === bot.user.id)) {
// If the person who mentioned the modmail bot is on the modmail server, don't ping about it
if (utils.getModmailGuild(bot).members.get(msg.author.id)) return;
blocked.isBlocked(msg.author.id).then(isBlocked => { blocked.isBlocked(msg.author.id).then(isBlocked => {
if (isBlocked) return; if (isBlocked) return;
@ -113,11 +117,8 @@ bot.on('messageCreate', (msg) => {
} }
// Ping mods of the new thread // Ping mods of the new thread
let creationNotificationMessage = `New modmail thread: <#${thread.channelId}>`;
if (config.pingCreationNotification) creationNotificationMessage = `@here ${creationNotificationMessage}`;
bot.createMessage(utils.getModmailGuild(bot).id, { bot.createMessage(utils.getModmailGuild(bot).id, {
content: creationNotificationMessage, content: `@here New modmail thread: <#${thread.channelId}>`,
disableEveryone: false, disableEveryone: false,
}); });
@ -159,23 +160,22 @@ bot.on('messageUpdate', (msg, oldMessage) => {
}); });
}); });
// Mods can reply to modmail threads using !r or !reply function reply(msg, text, anonymous = false) {
// These messages get relayed back to the DM thread between the bot and the user
bot.registerCommand('reply', (msg, args) => {
if (! msg.channel.guild) return;
if (msg.channel.guild.id !== utils.getModmailGuild(bot).id) return;
if (! msg.member.permission.has('manageRoles')) return;
threads.getByChannelId(msg.channel.id).then(thread => { threads.getByChannelId(msg.channel.id).then(thread => {
if (! thread) return; if (! thread) return;
attachments.saveAttachmentsInMessage(msg).then(() => { attachments.saveAttachmentsInMessage(msg).then(() => {
bot.getDMChannel(thread.userId).then(dmChannel => { bot.getDMChannel(thread.userId).then(dmChannel => {
let modUsername;
const mainRole = utils.getMainRole(msg.member); const mainRole = utils.getMainRole(msg.member);
const roleStr = (mainRole ? `(${mainRole.name}) ` : '');
let argMsg = args.join(' ').trim(); if (anonymous) {
let content = `**${roleStr}${msg.author.username}:** ${argMsg}`; modUsername = (mainRole ? mainRole.name : 'Moderator');
} else {
modUsername = (mainRole ? `(${mainRole.name}) ${msg.author.username}` : msg.author.username);
}
let content = `**${modUsername}:** ${text}`;
function sendMessage(file, attachmentUrl) { function sendMessage(file, attachmentUrl) {
dmChannel.createMessage(content, file).then(() => { dmChannel.createMessage(content, file).then(() => {
@ -211,10 +211,33 @@ bot.registerCommand('reply', (msg, args) => {
}); });
}); });
}); });
}
// Mods can reply to modmail threads using !r or !reply
// These messages get relayed back to the DM thread between the bot and the user
bot.registerCommand('reply', (msg, args) => {
if (! msg.channel.guild) return;
if (msg.channel.guild.id !== utils.getModmailGuild(bot).id) return;
if (! msg.member.permission.has('manageRoles')) return;
const text = args.join(' ').trim();
reply(msg, text, false);
}); });
bot.registerCommandAlias('r', 'reply'); bot.registerCommandAlias('r', 'reply');
// Anonymous replies only show the role, not the username
bot.registerCommand('anonreply', (msg, args) => {
if (! msg.channel.guild) return;
if (msg.channel.guild.id !== utils.getModmailGuild(bot).id) return;
if (! msg.member.permission.has('manageRoles')) return;
const text = args.join(' ').trim();
reply(msg, text, true);
});
bot.registerCommandAlias('ar', 'anonreply');
bot.registerCommand('close', (msg, args) => { bot.registerCommand('close', (msg, args) => {
if (! msg.channel.guild) return; if (! msg.channel.guild) return;
if (msg.channel.guild.id !== utils.getModmailGuild(bot).id) return; if (msg.channel.guild.id !== utils.getModmailGuild(bot).id) return;
@ -326,3 +349,4 @@ bot.registerCommand('logs', (msg, args) => {
bot.connect(); bot.connect();
webserver.run(); webserver.run();
greeting.enable(bot);