ModActions: move user messaging to its own function

This commit is contained in:
Dragory 2018-07-12 01:20:20 +03:00
parent 38295e8aa7
commit fcf682a039

View file

@ -226,27 +226,14 @@ export class ModActionsPlugin extends Plugin {
.replace("{guildName}", this.guild.name) .replace("{guildName}", this.guild.name)
.replace("{reason}", args.reason); .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 (!messageSent) {
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) {
const failedMsg = await msg.channel.createMessage( const failedMsg = await msg.channel.createMessage(
"Failed to message the user. Log the warning anyway?" "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 // Message the user informing them of the mute
let messagingFailed = false; let messageSent = false;
if (args.reason) { if (args.reason) {
const muteMessage = formatTemplateString( const muteMessage = formatTemplateString(
this.configValue("mute_message"), this.configValue("mute_message"),
@ -326,49 +313,29 @@ export class ModActionsPlugin extends Plugin {
} }
); );
try { messageSent = await this.tryToMessageUser(
if (this.configValue("dm_on_mute")) { args.member.user,
const dmChannel = await this.bot.getDMChannel(args.member.id); muteMessage,
await dmChannel.createMessage(muteMessage); this.configValue("dm_on_mute"),
} this.configValue("message_on_mute")
);
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;
}
} }
// Confirm the action to the moderator // Confirm the action to the moderator
let response;
if (muteTime) { if (muteTime) {
const unmuteTime = moment() const unmuteTime = moment()
.add(muteTime, "ms") .add(muteTime, "ms")
.format("YYYY-MM-DD HH:mm:ss"); .format("YYYY-MM-DD HH:mm:ss");
msg.channel.createMessage( response = `Member muted until ${unmuteTime}`;
successMessage(
`Member muted until ${unmuteTime}${
messagingFailed ? " (failed to message user)" : ""
}`
)
);
} else { } else {
msg.channel.createMessage( response = `Member muted indefinitely`;
successMessage(
`Member muted indefinitely${
messagingFailed ? " (failed to message user)" : ""
}`
)
);
} }
if (!messageSent) response += " (failed to message user)";
msg.channel.createMessage(successMessage(response));
this.serverLogs.log(LogType.MEMBER_MUTE, { this.serverLogs.log(LogType.MEMBER_MUTE, {
mod: stripObjectToScalars(msg.member, ["user"]), mod: stripObjectToScalars(msg.member, ["user"]),
member: stripObjectToScalars(args.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<boolean> {
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. * Shows information about the specified action in a message embed.
* If no channelId is specified, uses the channel id from config. * If no channelId is specified, uses the channel id from config.