From fcf682a03966b62cd17af5bfdd6829ea1f1bd7ce Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 12 Jul 2018 01:20:20 +0300 Subject: [PATCH] ModActions: move user messaging to its own function --- src/plugins/ModActions.ts | 106 +++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/src/plugins/ModActions.ts b/src/plugins/ModActions.ts index c395f8e4..3658105b 100644 --- a/src/plugins/ModActions.ts +++ b/src/plugins/ModActions.ts @@ -226,27 +226,14 @@ export class ModActionsPlugin extends Plugin { .replace("{guildName}", this.guild.name) .replace("{reason}", args.reason); - let failedToMessage = false; + const messageSent = await this.tryToMessageUser( + args.member.user, + warnMessage, + this.configValue("dm_on_warn"), + this.configValue("message_on_warn") + ); - try { - if (this.configValue("dm_on_warn")) { - const dmChannel = await this.bot.getDMChannel(args.member.id); - await dmChannel.createMessage(warnMessage); - } - - if (this.configValue("message_on_warn")) { - const channel = this.guild.channels.get( - this.configValue("message_channel") - ) as TextChannel; - if (channel) { - await channel.createMessage(`<@!${args.member.id}> ${warnMessage}`); - } - } - } catch (e) { - failedToMessage = true; - } - - if (failedToMessage) { + if (!messageSent) { const failedMsg = await msg.channel.createMessage( "Failed to message the user. Log the warning anyway?" ); @@ -316,7 +303,7 @@ export class ModActionsPlugin extends Plugin { ); // Message the user informing them of the mute - let messagingFailed = false; + let messageSent = false; if (args.reason) { const muteMessage = formatTemplateString( this.configValue("mute_message"), @@ -326,49 +313,29 @@ export class ModActionsPlugin extends Plugin { } ); - try { - if (this.configValue("dm_on_mute")) { - const dmChannel = await this.bot.getDMChannel(args.member.id); - await dmChannel.createMessage(muteMessage); - } - - if ( - this.configValue("message_on_mute") && - this.configValue("message_channel") - ) { - const channel = this.guild.channels.get( - this.configValue("message_channel") - ) as TextChannel; - await channel.createMessage(`<@!${args.member.id}> ${muteMessage}`); - } - } catch (e) { - messagingFailed = true; - } + messageSent = await this.tryToMessageUser( + args.member.user, + muteMessage, + this.configValue("dm_on_mute"), + this.configValue("message_on_mute") + ); } // Confirm the action to the moderator + let response; if (muteTime) { const unmuteTime = moment() .add(muteTime, "ms") .format("YYYY-MM-DD HH:mm:ss"); - msg.channel.createMessage( - successMessage( - `Member muted until ${unmuteTime}${ - messagingFailed ? " (failed to message user)" : "" - }` - ) - ); + response = `Member muted until ${unmuteTime}`; } else { - msg.channel.createMessage( - successMessage( - `Member muted indefinitely${ - messagingFailed ? " (failed to message user)" : "" - }` - ) - ); + response = `Member muted indefinitely`; } + if (!messageSent) response += " (failed to message user)"; + msg.channel.createMessage(successMessage(response)); + this.serverLogs.log(LogType.MEMBER_MUTE, { mod: stripObjectToScalars(msg.member, ["user"]), member: stripObjectToScalars(args.member, ["user"]) @@ -411,6 +378,39 @@ export class ModActionsPlugin extends Plugin { } } + /** + * Attempts to message the specified user through DMs and/or the message channel. + * Returns a promise that resolves to a boolean indicating whether we were able to message them or not. + */ + protected async tryToMessageUser( + user: User, + str: string, + useDM: boolean, + useChannel: boolean + ): Promise { + let messageSent = false; + + if (useDM) { + try { + const dmChannel = await this.bot.getDMChannel(user.id); + await dmChannel.createMessage(str); + messageSent = true; + } catch (e) {} // tslint:disable-line + } + + if (useChannel && this.configValue("message_channel")) { + try { + const channel = this.guild.channels.get( + this.configValue("message_channel") + ) as TextChannel; + await channel.createMessage(`<@!${user.id}> ${str}`); + messageSent = true; + } catch (e) {} // tslint:disable-line + } + + return messageSent; + } + /** * Shows information about the specified action in a message embed. * If no channelId is specified, uses the channel id from config.