From 843c23f33b9710a3eb0210b7d36ac56012a8eb31 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Mon, 15 Feb 2021 01:47:14 +0100 Subject: [PATCH] Allow the automod action "reply" to reply in DMs Also allows the sending of a second message in the server channel to, i.e. let them know they received a DM. If the DM fails, falls back to in-server reply. DM messages are not affected by auto_delete, all other "new" messages are. --- backend/src/plugins/Automod/actions/reply.ts | 25 ++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Automod/actions/reply.ts b/backend/src/plugins/Automod/actions/reply.ts index 0a451d00..b98b9ae1 100644 --- a/backend/src/plugins/Automod/actions/reply.ts +++ b/backend/src/plugins/Automod/actions/reply.ts @@ -20,6 +20,8 @@ export const ReplyAction = automodAction({ t.type({ text: tMessageContent, auto_delete: tNullable(t.union([tDelayString, t.number])), + as_dm: tNullable(t.boolean), + channel_text_on_dm: tNullable(tMessageContent), }), ]), @@ -53,8 +55,27 @@ export const ReplyAction = automodAction({ : await renderRecursively(actionConfig.text, renderReplyText); if (formatted) { - const channel = pluginData.guild.channels.get(channelId) as TextChannel; - const replyMsg = await channel.createMessage(formatted); + let replyMsg; + const serverChannel = pluginData.guild.channels.get(channelId) as TextChannel; + + if (typeof actionConfig !== "string" && actionConfig.as_dm) { + let fallback = false; + try { + const dmChannel = await user!.getDMChannel(); + await dmChannel.createMessage(formatted); // Don't store in replyMsg as to not delete a DM + } catch (e) { + // Fallback to in-server reply + replyMsg = await serverChannel.createMessage(formatted); + fallback = true; + } + + if (actionConfig.channel_text_on_dm && !fallback) { + const channelFormatted = await renderReplyText(actionConfig.channel_text_on_dm); + replyMsg = await serverChannel.createMessage(channelFormatted); + } + } else { + replyMsg = await serverChannel.createMessage(formatted); + } if (typeof actionConfig === "object" && actionConfig.auto_delete) { const delay = convertDelayStringToMS(String(actionConfig.auto_delete))!;