From e1499f64fc123f43c5df9ba0bb0608180ee8567e Mon Sep 17 00:00:00 2001 From: metal Date: Wed, 8 Sep 2021 10:07:12 +0000 Subject: [PATCH 1/6] initial --- .../Automod/actions/availableActions.ts | 3 ++ .../Automod/actions/crosspostMessage.ts | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 backend/src/plugins/Automod/actions/crosspostMessage.ts diff --git a/backend/src/plugins/Automod/actions/availableActions.ts b/backend/src/plugins/Automod/actions/availableActions.ts index 76b2a60e9..8b8b75d3f 100644 --- a/backend/src/plugins/Automod/actions/availableActions.ts +++ b/backend/src/plugins/Automod/actions/availableActions.ts @@ -7,6 +7,7 @@ import { ArchiveThreadAction } from "./archiveThread"; import { BanAction } from "./ban"; import { ChangeNicknameAction } from "./changeNickname"; import { CleanAction } from "./clean"; +import { CrosspostMessageAction } from "./crosspostMessage"; import { KickAction } from "./kick"; import { LogAction } from "./log"; import { MuteAction } from "./mute"; @@ -34,6 +35,7 @@ export const availableActions: Record> = { set_counter: SetCounterAction, set_slowmode: SetSlowmodeAction, archive_thread: ArchiveThreadAction, + crosspost_message: CrosspostMessageAction, }; export const AvailableActions = t.type({ @@ -53,4 +55,5 @@ export const AvailableActions = t.type({ set_counter: SetCounterAction.configType, set_slowmode: SetSlowmodeAction.configType, archive_thread: ArchiveThreadAction.configType, + crosspost_message: CrosspostMessageAction.configType, }); diff --git a/backend/src/plugins/Automod/actions/crosspostMessage.ts b/backend/src/plugins/Automod/actions/crosspostMessage.ts new file mode 100644 index 000000000..0c1e306f5 --- /dev/null +++ b/backend/src/plugins/Automod/actions/crosspostMessage.ts @@ -0,0 +1,29 @@ +import { Message } from "discord.js"; +import * as t from "io-ts"; +import { ChannelTypeStrings } from "src/types"; +import { automodAction } from "../helpers"; + +export const CrosspostMessageAction = automodAction({ + configType: t.type({}), + defaultConfig: {}, + + async apply({ pluginData, contexts, actionConfig }) { + const messages = await Promise.all( + contexts + .filter(c => c.message?.id) + .map(async c => { + const channel = pluginData.guild.channels.cache.get(c.message!.channel_id); + if (channel?.type === ChannelTypeStrings.NEWS && channel.isText()) { + // .isText() to fix the typings + const msg = await channel.messages.fetch(c.message!.id); + return msg && msg.crosspostable ? msg : null; + } + return null; + }), + ); + + for (const msg of messages) { + await msg?.crosspost(); + } + }, +}); From 8c3d232eb9482dcd1cfb03ef6ccc5213c95f6d92 Mon Sep 17 00:00:00 2001 From: metal Date: Wed, 8 Sep 2021 10:14:07 +0000 Subject: [PATCH 2/6] .catch --- backend/src/plugins/Automod/actions/crosspostMessage.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/plugins/Automod/actions/crosspostMessage.ts b/backend/src/plugins/Automod/actions/crosspostMessage.ts index 0c1e306f5..bd079ee41 100644 --- a/backend/src/plugins/Automod/actions/crosspostMessage.ts +++ b/backend/src/plugins/Automod/actions/crosspostMessage.ts @@ -1,6 +1,7 @@ import { Message } from "discord.js"; import * as t from "io-ts"; import { ChannelTypeStrings } from "src/types"; +import { noop } from "src/utils"; import { automodAction } from "../helpers"; export const CrosspostMessageAction = automodAction({ @@ -23,7 +24,7 @@ export const CrosspostMessageAction = automodAction({ ); for (const msg of messages) { - await msg?.crosspost(); + await msg?.crosspost().catch(noop); } }, }); From 5b73253962a8d4b03e952610b55aa104984f5565 Mon Sep 17 00:00:00 2001 From: metal Date: Wed, 8 Sep 2021 11:39:54 +0100 Subject: [PATCH 3/6] Update backend/src/plugins/Automod/actions/crosspostMessage.ts Co-authored-by: Almeida --- .../Automod/actions/crosspostMessage.ts | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/backend/src/plugins/Automod/actions/crosspostMessage.ts b/backend/src/plugins/Automod/actions/crosspostMessage.ts index bd079ee41..4b6b5c7c7 100644 --- a/backend/src/plugins/Automod/actions/crosspostMessage.ts +++ b/backend/src/plugins/Automod/actions/crosspostMessage.ts @@ -9,22 +9,19 @@ export const CrosspostMessageAction = automodAction({ defaultConfig: {}, async apply({ pluginData, contexts, actionConfig }) { - const messages = await Promise.all( - contexts - .filter(c => c.message?.id) - .map(async c => { - const channel = pluginData.guild.channels.cache.get(c.message!.channel_id); - if (channel?.type === ChannelTypeStrings.NEWS && channel.isText()) { - // .isText() to fix the typings - const msg = await channel.messages.fetch(c.message!.id); - return msg && msg.crosspostable ? msg : null; - } - return null; - }), - ); + const messages = contexts + .filter(c => c.message?.id) + .map(c => { + const channel = pluginData.guild.channels.cache.get(c.message!.channel_id); + if (channel?.type === ChannelTypeStrings.NEWS && channel.isText()) { + // .isText() to fix the typings + return channel.messages.fetch(c.message!.id); + } + return null; + }); - for (const msg of messages) { - await msg?.crosspost().catch(noop); + for await (const msg of messages) { + if (msg?.crosspostable) await msg?.crosspost().catch(noop); } }, }); From 9282f3834c9c7c0ea7bc4effc16f99ddcfdb5606 Mon Sep 17 00:00:00 2001 From: metal Date: Wed, 8 Sep 2021 11:40:02 +0100 Subject: [PATCH 4/6] Update backend/src/plugins/Automod/actions/crosspostMessage.ts Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/crosspostMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/plugins/Automod/actions/crosspostMessage.ts b/backend/src/plugins/Automod/actions/crosspostMessage.ts index 4b6b5c7c7..fa78cd02b 100644 --- a/backend/src/plugins/Automod/actions/crosspostMessage.ts +++ b/backend/src/plugins/Automod/actions/crosspostMessage.ts @@ -8,7 +8,7 @@ export const CrosspostMessageAction = automodAction({ configType: t.type({}), defaultConfig: {}, - async apply({ pluginData, contexts, actionConfig }) { + async apply({ pluginData, contexts }) { const messages = contexts .filter(c => c.message?.id) .map(c => { From 4278a0582463a7332cf3a6eb8f3c97f89825cca6 Mon Sep 17 00:00:00 2001 From: metal Date: Wed, 8 Sep 2021 11:40:09 +0100 Subject: [PATCH 5/6] Update backend/src/plugins/Automod/actions/crosspostMessage.ts Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/crosspostMessage.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/src/plugins/Automod/actions/crosspostMessage.ts b/backend/src/plugins/Automod/actions/crosspostMessage.ts index fa78cd02b..79a360b66 100644 --- a/backend/src/plugins/Automod/actions/crosspostMessage.ts +++ b/backend/src/plugins/Automod/actions/crosspostMessage.ts @@ -1,4 +1,3 @@ -import { Message } from "discord.js"; import * as t from "io-ts"; import { ChannelTypeStrings } from "src/types"; import { noop } from "src/utils"; From ddf685a5a22a33576a6af29d15c7cf4f0ab5640c Mon Sep 17 00:00:00 2001 From: metal Date: Wed, 8 Sep 2021 11:40:15 +0100 Subject: [PATCH 6/6] Update backend/src/plugins/Automod/actions/crosspostMessage.ts Co-authored-by: Almeida --- backend/src/plugins/Automod/actions/crosspostMessage.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Automod/actions/crosspostMessage.ts b/backend/src/plugins/Automod/actions/crosspostMessage.ts index 79a360b66..9217da833 100644 --- a/backend/src/plugins/Automod/actions/crosspostMessage.ts +++ b/backend/src/plugins/Automod/actions/crosspostMessage.ts @@ -1,6 +1,6 @@ import * as t from "io-ts"; -import { ChannelTypeStrings } from "src/types"; -import { noop } from "src/utils"; +import { ChannelTypeStrings } from "../../../types"; +import { noop } from "../../../utils"; import { automodAction } from "../helpers"; export const CrosspostMessageAction = automodAction({