From 03ead92c19809637a0db7920d27781cffd8e5a55 Mon Sep 17 00:00:00 2001 From: Almeida Date: Fri, 7 May 2021 20:06:31 +0100 Subject: [PATCH 01/16] added join and leave voice channel triggers --- backend/src/plugins/Automod/AutomodPlugin.ts | 3 +- .../events/runAutomodOnVoiceStateUpdate.ts | 58 +++++++++++++++++++ .../Automod/triggers/availableTriggers.ts | 11 ++++ .../Automod/triggers/joinVoiceChannel.ts | 37 ++++++++++++ .../Automod/triggers/leaveVoiceChannel.ts | 37 ++++++++++++ .../Automod/triggers/moveVoiceChannel.ts | 58 +++++++++++++++++++ backend/src/plugins/Automod/types.ts | 4 ++ 7 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts create mode 100644 backend/src/plugins/Automod/triggers/joinVoiceChannel.ts create mode 100644 backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts create mode 100644 backend/src/plugins/Automod/triggers/moveVoiceChannel.ts diff --git a/backend/src/plugins/Automod/AutomodPlugin.ts b/backend/src/plugins/Automod/AutomodPlugin.ts index 72cb7aa5..48a0afbf 100644 --- a/backend/src/plugins/Automod/AutomodPlugin.ts +++ b/backend/src/plugins/Automod/AutomodPlugin.ts @@ -29,6 +29,7 @@ import { RunAutomodOnThreadDelete, RunAutomodOnThreadUpdate, } from "./events/runAutomodOnThreadEvents"; +import { RunAutomodOnVoiceStateUpdate } from "./events/runAutomodOnVoiceStateUpdate"; import { clearOldRecentNicknameChanges } from "./functions/clearOldNicknameChanges"; import { clearOldRecentActions } from "./functions/clearOldRecentActions"; import { clearOldRecentSpam } from "./functions/clearOldRecentSpam"; @@ -207,7 +208,6 @@ export const AutomodPlugin = zeppelinGuildPlugin()({ }, }, - // prettier-ignore events: [ RunAutomodOnJoinEvt, RunAutomodOnMemberUpdate, @@ -215,6 +215,7 @@ export const AutomodPlugin = zeppelinGuildPlugin()({ RunAutomodOnThreadCreate, RunAutomodOnThreadDelete, RunAutomodOnThreadUpdate + RunAutomodOnVoiceStateUpdate, // Messages use message events from SavedMessages, see onLoad below ], diff --git a/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts b/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts new file mode 100644 index 00000000..1726f35c --- /dev/null +++ b/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts @@ -0,0 +1,58 @@ +import { typedGuildEventListener } from "knub"; +import { AutomodContext, AutomodPluginType } from "../types"; +import { runAutomod } from "../functions/runAutomod"; +import { noop } from "../../../utils"; + +export const RunAutomodOnVoiceStateUpdate = typedGuildEventListener()({ + event: "voiceStateUpdate", + async listener({ pluginData, args: { newState, oldState } }) { + const oldChannel = newState.channel; + const { channel: newChannel, guild } = newState; + const timestamp = Date.now(); + + const member = newState.member ?? oldState.member ?? (await guild.members.fetch(newState.id).catch(noop)); + if (!member) return; + + if (!oldChannel && newChannel) { + const context: AutomodContext = { + member, + timestamp, + voiceChannel: { + joined: newChannel, + }, + user: member.user, + }; + + pluginData.state.queue.add(() => { + runAutomod(pluginData, context); + }); + } else if (oldChannel && !newChannel) { + const context: AutomodContext = { + member, + timestamp, + voiceChannel: { + left: oldChannel, + }, + user: member.user, + }; + + pluginData.state.queue.add(() => { + runAutomod(pluginData, context); + }); + } else if (oldChannel?.id && newChannel?.id && oldChannel.id === newChannel.id) { + const context: AutomodContext = { + member, + timestamp, + voiceChannel: { + left: oldChannel, + joined: newChannel, + }, + user: member.user, + }; + + pluginData.state.queue.add(() => { + runAutomod(pluginData, context); + }); + } + }, +}); diff --git a/backend/src/plugins/Automod/triggers/availableTriggers.ts b/backend/src/plugins/Automod/triggers/availableTriggers.ts index ea8b6f04..bd2e79a8 100644 --- a/backend/src/plugins/Automod/triggers/availableTriggers.ts +++ b/backend/src/plugins/Automod/triggers/availableTriggers.ts @@ -7,7 +7,9 @@ import { BanTrigger } from "./ban"; import { CharacterSpamTrigger } from "./characterSpam"; import { CounterTrigger } from "./counterTrigger"; import { EmojiSpamTrigger } from "./emojiSpam"; +import { JoinVoiceChannelTrigger } from "./joinVoiceChannel"; import { KickTrigger } from "./kick"; +import { LeaveVoiceChannelTrigger } from "./leaveVoiceChannel"; import { LineSpamTrigger } from "./lineSpam"; import { LinkSpamTrigger } from "./linkSpam"; import { MatchAttachmentTypeTrigger } from "./matchAttachmentType"; @@ -21,6 +23,7 @@ import { MemberJoinSpamTrigger } from "./memberJoinSpam"; import { MemberLeaveTrigger } from "./memberLeave"; import { MentionSpamTrigger } from "./mentionSpam"; import { MessageSpamTrigger } from "./messageSpam"; +import { MoveVoiceChannelTrigger } from "./moveVoiceChannel"; import { MuteTrigger } from "./mute"; import { NoteTrigger } from "./note"; import { RoleAddedTrigger } from "./roleAdded"; @@ -69,6 +72,10 @@ export const availableTriggers: Record ban: BanTrigger, unban: UnbanTrigger, + join_voice_channel: JoinVoiceChannelTrigger, + leave_voice_channel: LeaveVoiceChannelTrigger, + move_voice_channel: MoveVoiceChannelTrigger, + antiraid_level: AntiraidLevelTrigger, thread_create: ThreadCreateTrigger, @@ -112,6 +119,10 @@ export const AvailableTriggers = t.type({ ban: BanTrigger.configType, unban: UnbanTrigger.configType, + join_voice_channel: JoinVoiceChannelTrigger.configType, + leave_voice_channel: LeaveVoiceChannelTrigger.configType, + move_voice_channel: MoveVoiceChannelTrigger.configType, + antiraid_level: AntiraidLevelTrigger.configType, thread_create: ThreadCreateTrigger.configType, diff --git a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts new file mode 100644 index 00000000..ecd34e3e --- /dev/null +++ b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts @@ -0,0 +1,37 @@ +import * as t from "io-ts"; +import { automodTrigger } from "../helpers"; + +interface JoinVoiceChannelResult { + matchedChannelId: string; +} + +export const JoinVoiceChannelTrigger = automodTrigger()({ + configType: t.union([t.string, t.array(t.string)]), + + defaultConfig: "", + + async match({ triggerConfig, context }) { + if (!context.member || !context.voiceChannel) { + return; + } + + const triggerChannels = Array.isArray(triggerConfig) ? triggerConfig : [triggerConfig]; + if (!triggerChannels.includes(context.voiceChannel.id)) { + return; + } + + return { + extra: { + matchedChannelId: context.voiceChannel.id, + }, + }; + }, + + renderMatchInformation({ matchResult, pluginData, contexts }) { + const channel = pluginData.guild.channels.get(matchResult.extra.matchedChannelId); + const channelName = channel?.name || "Unknown"; + const member = contexts[0].member!; + const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`; + return `${memberName} has joined the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) voice channel`; + }, +}); diff --git a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts new file mode 100644 index 00000000..51b6467e --- /dev/null +++ b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts @@ -0,0 +1,37 @@ +import * as t from "io-ts"; +import { automodTrigger } from "../helpers"; + +interface LeaveVoiceChannelResult { + matchedChannelId: string; +} + +export const LeaveVoiceChannelTrigger = automodTrigger()({ + configType: t.union([t.string, t.array(t.string)]), + + defaultConfig: "", + + async match({ triggerConfig, context }) { + if (!context.member || !context.voiceChannel) { + return; + } + + const triggerChannels = Array.isArray(triggerConfig) ? triggerConfig : [triggerConfig]; + if (!triggerChannels.includes(context.voiceChannel.id)) { + return; + } + + return { + extra: { + matchedChannelId: context.voiceChannel.id, + }, + }; + }, + + renderMatchInformation({ matchResult, pluginData, contexts }) { + const channel = pluginData.guild.channels.get(matchResult.extra.matchedChannelId); + const channelName = channel?.name || "Unknown"; + const member = contexts[0].member!; + const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`; + return `${memberName} has left the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) voice channel`; + }, +}); diff --git a/backend/src/plugins/Automod/triggers/moveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/moveVoiceChannel.ts new file mode 100644 index 00000000..b31ea256 --- /dev/null +++ b/backend/src/plugins/Automod/triggers/moveVoiceChannel.ts @@ -0,0 +1,58 @@ +import * as t from "io-ts"; +import { ChannelTypeStrings } from "../../../types"; +import { automodTrigger } from "../helpers"; + +interface MoveVoiceChannelResult { + oldChannelId: string; + newChannelId: string; +} + +export const MoveVoiceChannelTrigger = automodTrigger()({ + configType: t.type({ + old_channel: t.union([t.string, t.array(t.string)]), + new_channel: t.union([t.string, t.array(t.string)]), + }), + + defaultConfig: {}, + + async match({ triggerConfig, context }) { + const oldChannelId = context.voiceChannel?.left?.id; + const newChannelId = context.voiceChannel?.joined?.id; + + if (!context.member || !oldChannelId || !newChannelId) { + return; + } + + const triggerOldChannels = Array.isArray(triggerConfig.old_channel) + ? triggerConfig.old_channel + : [triggerConfig.old_channel]; + + const triggerNewChannels = Array.isArray(triggerConfig.new_channel) + ? triggerConfig.new_channel + : [triggerConfig.new_channel]; + + if (!triggerOldChannels.includes(oldChannelId) || !triggerNewChannels.includes(newChannelId)) { + return; + } + + return { + extra: { + oldChannelId, + newChannelId, + }, + }; + }, + + renderMatchInformation({ matchResult, pluginData, contexts }) { + const { newChannelId, oldChannelId } = matchResult.extra; + const oldChannel = pluginData.guild.channels.resolve(oldChannelId); + const newChannel = pluginData.guild.channels.resolve(newChannelId); + const member = contexts[0].member!; + const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`; + const oldChannelName = oldChannel?.name ?? "Unknown"; + const newChannelName = newChannel?.name ?? "Unknown"; + const oldChannelVoiceOrStage = oldChannel?.type === ChannelTypeStrings.STAGE ? "stage" : "voice"; + const newChannelVoiceOrStage = newChannel?.type === ChannelTypeStrings.STAGE ? "stage" : "voice"; + return `${memberName} has moved from the the ${oldChannelName} (\`${oldChannelId}\`) ${oldChannelVoiceOrStage} channel to the ${newChannelName} (\`${newChannelId}\`) ${newChannelVoiceOrStage} channel`; + }, +}); diff --git a/backend/src/plugins/Automod/types.ts b/backend/src/plugins/Automod/types.ts index ed7cf56e..c85c2d51 100644 --- a/backend/src/plugins/Automod/types.ts +++ b/backend/src/plugins/Automod/types.ts @@ -140,6 +140,10 @@ export interface AutomodContext { unlocked?: ThreadChannel; }; channel?: TextChannel | ThreadChannel; + voiceChannel?: { + joined?: VoiceChannel | StageChannel; + left?: VoiceChannel | StageChannel; + }; } export interface RecentAction { From ca7b05ce78626fc8711be4ce7293a0085ae78186 Mon Sep 17 00:00:00 2001 From: Almeida Date: Fri, 7 May 2021 20:32:33 +0100 Subject: [PATCH 02/16] fix: leave_voice_channel not triggering --- .../src/plugins/Automod/triggers/joinVoiceChannel.ts | 11 +++++++---- .../src/plugins/Automod/triggers/leaveVoiceChannel.ts | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts index ecd34e3e..9064fb45 100644 --- a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts @@ -1,5 +1,6 @@ import * as t from "io-ts"; import { automodTrigger } from "../helpers"; +import { Constants } from "eris"; interface JoinVoiceChannelResult { matchedChannelId: string; @@ -11,18 +12,19 @@ export const JoinVoiceChannelTrigger = automodTrigger()( defaultConfig: "", async match({ triggerConfig, context }) { - if (!context.member || !context.voiceChannel) { + const matchedChannelId = context.voiceChannel?.joined?.id; + if (!context.member || !matchedChannelId) { return; } const triggerChannels = Array.isArray(triggerConfig) ? triggerConfig : [triggerConfig]; - if (!triggerChannels.includes(context.voiceChannel.id)) { + if (!triggerChannels.includes(matchedChannelId)) { return; } return { extra: { - matchedChannelId: context.voiceChannel.id, + matchedChannelId, }, }; }, @@ -32,6 +34,7 @@ export const JoinVoiceChannelTrigger = automodTrigger()( const channelName = channel?.name || "Unknown"; const member = contexts[0].member!; const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`; - return `${memberName} has joined the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) voice channel`; + const voiceOrStage = channel?.type === Constants.ChannelTypes.GUILD_STAGE ? "stage" : "voice"; + return `${memberName} has joined the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) ${voiceOrStage} channel`; }, }); diff --git a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts index 51b6467e..d4125d6f 100644 --- a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts @@ -1,3 +1,4 @@ +import { Constants } from "eris"; import * as t from "io-ts"; import { automodTrigger } from "../helpers"; @@ -11,18 +12,19 @@ export const LeaveVoiceChannelTrigger = automodTrigger( defaultConfig: "", async match({ triggerConfig, context }) { - if (!context.member || !context.voiceChannel) { + const matchedChannelId = context.voiceChannel?.left?.id; + if (!context.member || !matchedChannelId) { return; } const triggerChannels = Array.isArray(triggerConfig) ? triggerConfig : [triggerConfig]; - if (!triggerChannels.includes(context.voiceChannel.id)) { + if (!triggerChannels.includes(matchedChannelId)) { return; } return { extra: { - matchedChannelId: context.voiceChannel.id, + matchedChannelId, }, }; }, @@ -32,6 +34,7 @@ export const LeaveVoiceChannelTrigger = automodTrigger( const channelName = channel?.name || "Unknown"; const member = contexts[0].member!; const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`; - return `${memberName} has left the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) voice channel`; + const voiceOrStage = channel?.type === Constants.ChannelTypes.GUILD_STAGE ? "stage" : "voice"; + return `${memberName} has left the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) ${voiceOrStage} channel`; }, }); From 0f4f7b446d9c13c3fa8bb8103e09461f3debf7be Mon Sep 17 00:00:00 2001 From: Almeida Date: Sat, 8 May 2021 14:14:44 +0100 Subject: [PATCH 03/16] added move_voice_channel trigger --- backend/src/plugins/Automod/triggers/joinVoiceChannel.ts | 2 +- backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts index 9064fb45..16723584 100644 --- a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts @@ -13,7 +13,7 @@ export const JoinVoiceChannelTrigger = automodTrigger()( async match({ triggerConfig, context }) { const matchedChannelId = context.voiceChannel?.joined?.id; - if (!context.member || !matchedChannelId) { + if (!context.member || !matchedChannelId || context.voiceChannel?.left) { return; } diff --git a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts index d4125d6f..4c8d2e09 100644 --- a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts @@ -13,7 +13,7 @@ export const LeaveVoiceChannelTrigger = automodTrigger( async match({ triggerConfig, context }) { const matchedChannelId = context.voiceChannel?.left?.id; - if (!context.member || !matchedChannelId) { + if (!context.member || !matchedChannelId || context.voiceChannel?.joined) { return; } From d74afbee5cae8bf011af423c75aa0b608e0e8601 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sat, 8 May 2021 14:17:45 +0100 Subject: [PATCH 04/16] merged all events into one file --- .../Automod/events/runAutomodOnVoiceEvents.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts diff --git a/backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts b/backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts new file mode 100644 index 00000000..fd0db61f --- /dev/null +++ b/backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts @@ -0,0 +1,58 @@ +import { guildEventListener } from "knub"; +import { AutomodContext, AutomodPluginType } from "../types"; +import { runAutomod } from "../functions/runAutomod"; + +export const RunAutomodOnVoiceJoin = guildEventListener()( + "voiceChannelJoin", + ({ pluginData, args: { member, newChannel } }) => { + const context: AutomodContext = { + member, + timestamp: Date.now(), + voiceChannel: { + joined: newChannel, + }, + user: member.user, + }; + + pluginData.state.queue.add(() => { + runAutomod(pluginData, context); + }); + }, +); + +export const RunAutomodOnVoiceLeave = guildEventListener()( + "voiceChannelLeave", + ({ pluginData, args: { member, oldChannel } }) => { + const context: AutomodContext = { + member, + timestamp: Date.now(), + voiceChannel: { + left: oldChannel, + }, + user: member.user, + }; + + pluginData.state.queue.add(() => { + runAutomod(pluginData, context); + }); + }, +); + +export const RunAutomodOnVoiceSwitch = guildEventListener()( + "voiceChannelSwitch", + ({ pluginData, args: { member, oldChannel, newChannel } }) => { + const context: AutomodContext = { + member, + timestamp: Date.now(), + voiceChannel: { + left: oldChannel, + joined: newChannel, + }, + user: member.user, + }; + + pluginData.state.queue.add(() => { + runAutomod(pluginData, context); + }); + }, +); From ec2eb4a8bb50373debd81abee4bf6dac943fd127 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sat, 8 May 2021 14:19:39 +0100 Subject: [PATCH 05/16] fix: large bug where the event files werent camelcase --- backend/src/plugins/Automod/AutomodPlugin.ts | 2 +- ...{RunAutomodOnMemberUpdate.ts => runAutomodOnMemberUpdate.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename backend/src/plugins/Automod/events/{RunAutomodOnMemberUpdate.ts => runAutomodOnMemberUpdate.ts} (100%) diff --git a/backend/src/plugins/Automod/AutomodPlugin.ts b/backend/src/plugins/Automod/AutomodPlugin.ts index 48a0afbf..a5bb50dd 100644 --- a/backend/src/plugins/Automod/AutomodPlugin.ts +++ b/backend/src/plugins/Automod/AutomodPlugin.ts @@ -21,7 +21,7 @@ import { SetAntiraidCmd } from "./commands/SetAntiraidCmd"; import { ViewAntiraidCmd } from "./commands/ViewAntiraidCmd"; import { runAutomodOnCounterTrigger } from "./events/runAutomodOnCounterTrigger"; import { RunAutomodOnJoinEvt, RunAutomodOnLeaveEvt } from "./events/RunAutomodOnJoinLeaveEvt"; -import { RunAutomodOnMemberUpdate } from "./events/RunAutomodOnMemberUpdate"; +import { RunAutomodOnMemberUpdate } from "./events/runAutomodOnMemberUpdate"; import { runAutomodOnMessage } from "./events/runAutomodOnMessage"; import { runAutomodOnModAction } from "./events/runAutomodOnModAction"; import { diff --git a/backend/src/plugins/Automod/events/RunAutomodOnMemberUpdate.ts b/backend/src/plugins/Automod/events/runAutomodOnMemberUpdate.ts similarity index 100% rename from backend/src/plugins/Automod/events/RunAutomodOnMemberUpdate.ts rename to backend/src/plugins/Automod/events/runAutomodOnMemberUpdate.ts From f949a3825a7b248d7a800bf4b55e470e13e37379 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sat, 8 May 2021 14:45:38 +0100 Subject: [PATCH 06/16] added include_moves option to join_voice_channel and leave_voice_channel --- .../plugins/Automod/triggers/joinVoiceChannel.ts | 14 ++++++++++---- .../plugins/Automod/triggers/leaveVoiceChannel.ts | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts index 16723584..55ff01ac 100644 --- a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts @@ -7,17 +7,23 @@ interface JoinVoiceChannelResult { } export const JoinVoiceChannelTrigger = automodTrigger()({ - configType: t.union([t.string, t.array(t.string)]), + configType: t.type({ + channels: t.union([t.string, t.array(t.string)]), + include_moves: t.boolean, + }), - defaultConfig: "", + defaultConfig: {}, async match({ triggerConfig, context }) { const matchedChannelId = context.voiceChannel?.joined?.id; - if (!context.member || !matchedChannelId || context.voiceChannel?.left) { + const includeMoves = + typeof triggerConfig === "object" && !Array.isArray(triggerConfig) && triggerConfig.include_moves; + + if (!context.member || !matchedChannelId || (context.voiceChannel?.left && !includeMoves)) { return; } - const triggerChannels = Array.isArray(triggerConfig) ? triggerConfig : [triggerConfig]; + const triggerChannels = Array.isArray(triggerConfig.channels) ? triggerConfig.channels : [triggerConfig.channels]; if (!triggerChannels.includes(matchedChannelId)) { return; } diff --git a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts index 4c8d2e09..91ffb27d 100644 --- a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts @@ -7,17 +7,23 @@ interface LeaveVoiceChannelResult { } export const LeaveVoiceChannelTrigger = automodTrigger()({ - configType: t.union([t.string, t.array(t.string)]), + configType: t.type({ + channels: t.union([t.string, t.array(t.string)]), + include_moves: t.boolean, + }), - defaultConfig: "", + defaultConfig: {}, async match({ triggerConfig, context }) { const matchedChannelId = context.voiceChannel?.left?.id; - if (!context.member || !matchedChannelId || context.voiceChannel?.joined) { + const includeMoves = + typeof triggerConfig === "object" && !Array.isArray(triggerConfig) && triggerConfig.include_moves; + + if (!context.member || !matchedChannelId || (context.voiceChannel?.joined && !includeMoves)) { return; } - const triggerChannels = Array.isArray(triggerConfig) ? triggerConfig : [triggerConfig]; + const triggerChannels = Array.isArray(triggerConfig.channels) ? triggerConfig.channels : [triggerConfig.channels]; if (!triggerChannels.includes(matchedChannelId)) { return; } From bb27680a963b2297c44c882c29919167841da8b2 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sat, 14 Aug 2021 16:20:23 +0100 Subject: [PATCH 07/16] set file name to lowercase --- .../{RunAutomodOnJoinLeaveEvt.ts => runAutomodOnJoinLeaveEvt.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename backend/src/plugins/Automod/events/{RunAutomodOnJoinLeaveEvt.ts => runAutomodOnJoinLeaveEvt.ts} (100%) diff --git a/backend/src/plugins/Automod/events/RunAutomodOnJoinLeaveEvt.ts b/backend/src/plugins/Automod/events/runAutomodOnJoinLeaveEvt.ts similarity index 100% rename from backend/src/plugins/Automod/events/RunAutomodOnJoinLeaveEvt.ts rename to backend/src/plugins/Automod/events/runAutomodOnJoinLeaveEvt.ts From dcbe8c9eb2e15829de86293da4038025da3fb571 Mon Sep 17 00:00:00 2001 From: almeidx Date: Sat, 14 Aug 2021 16:23:05 +0100 Subject: [PATCH 08/16] eris leftovers --- backend/src/plugins/Automod/AutomodPlugin.ts | 2 +- .../Automod/events/runAutomodOnVoiceEvents.ts | 58 ------------------- .../Automod/triggers/joinVoiceChannel.ts | 8 +-- .../Automod/triggers/leaveVoiceChannel.ts | 8 +-- 4 files changed, 9 insertions(+), 67 deletions(-) delete mode 100644 backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts diff --git a/backend/src/plugins/Automod/AutomodPlugin.ts b/backend/src/plugins/Automod/AutomodPlugin.ts index a5bb50dd..b8d9be04 100644 --- a/backend/src/plugins/Automod/AutomodPlugin.ts +++ b/backend/src/plugins/Automod/AutomodPlugin.ts @@ -20,7 +20,7 @@ import { AntiraidClearCmd } from "./commands/AntiraidClearCmd"; import { SetAntiraidCmd } from "./commands/SetAntiraidCmd"; import { ViewAntiraidCmd } from "./commands/ViewAntiraidCmd"; import { runAutomodOnCounterTrigger } from "./events/runAutomodOnCounterTrigger"; -import { RunAutomodOnJoinEvt, RunAutomodOnLeaveEvt } from "./events/RunAutomodOnJoinLeaveEvt"; +import { RunAutomodOnJoinEvt, RunAutomodOnLeaveEvt } from "./events/runAutomodOnJoinLeaveEvt"; import { RunAutomodOnMemberUpdate } from "./events/runAutomodOnMemberUpdate"; import { runAutomodOnMessage } from "./events/runAutomodOnMessage"; import { runAutomodOnModAction } from "./events/runAutomodOnModAction"; diff --git a/backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts b/backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts deleted file mode 100644 index fd0db61f..00000000 --- a/backend/src/plugins/Automod/events/runAutomodOnVoiceEvents.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { guildEventListener } from "knub"; -import { AutomodContext, AutomodPluginType } from "../types"; -import { runAutomod } from "../functions/runAutomod"; - -export const RunAutomodOnVoiceJoin = guildEventListener()( - "voiceChannelJoin", - ({ pluginData, args: { member, newChannel } }) => { - const context: AutomodContext = { - member, - timestamp: Date.now(), - voiceChannel: { - joined: newChannel, - }, - user: member.user, - }; - - pluginData.state.queue.add(() => { - runAutomod(pluginData, context); - }); - }, -); - -export const RunAutomodOnVoiceLeave = guildEventListener()( - "voiceChannelLeave", - ({ pluginData, args: { member, oldChannel } }) => { - const context: AutomodContext = { - member, - timestamp: Date.now(), - voiceChannel: { - left: oldChannel, - }, - user: member.user, - }; - - pluginData.state.queue.add(() => { - runAutomod(pluginData, context); - }); - }, -); - -export const RunAutomodOnVoiceSwitch = guildEventListener()( - "voiceChannelSwitch", - ({ pluginData, args: { member, oldChannel, newChannel } }) => { - const context: AutomodContext = { - member, - timestamp: Date.now(), - voiceChannel: { - left: oldChannel, - joined: newChannel, - }, - user: member.user, - }; - - pluginData.state.queue.add(() => { - runAutomod(pluginData, context); - }); - }, -); diff --git a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts index 55ff01ac..f594dd3d 100644 --- a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts @@ -1,6 +1,6 @@ import * as t from "io-ts"; +import { ChannelTypeStrings } from "../../../types"; import { automodTrigger } from "../helpers"; -import { Constants } from "eris"; interface JoinVoiceChannelResult { matchedChannelId: string; @@ -36,11 +36,11 @@ export const JoinVoiceChannelTrigger = automodTrigger()( }, renderMatchInformation({ matchResult, pluginData, contexts }) { - const channel = pluginData.guild.channels.get(matchResult.extra.matchedChannelId); - const channelName = channel?.name || "Unknown"; + const channel = pluginData.guild.channels.resolve(matchResult.extra.matchedChannelId); + const channelName = channel?.name ?? "Unknown"; const member = contexts[0].member!; const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`; - const voiceOrStage = channel?.type === Constants.ChannelTypes.GUILD_STAGE ? "stage" : "voice"; + const voiceOrStage = channel?.type === ChannelTypeStrings.STAGE ? "stage" : "voice"; return `${memberName} has joined the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) ${voiceOrStage} channel`; }, }); diff --git a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts index 91ffb27d..2e83e423 100644 --- a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts @@ -1,5 +1,5 @@ -import { Constants } from "eris"; import * as t from "io-ts"; +import { ChannelTypeStrings } from "../../../types"; import { automodTrigger } from "../helpers"; interface LeaveVoiceChannelResult { @@ -36,11 +36,11 @@ export const LeaveVoiceChannelTrigger = automodTrigger( }, renderMatchInformation({ matchResult, pluginData, contexts }) { - const channel = pluginData.guild.channels.get(matchResult.extra.matchedChannelId); - const channelName = channel?.name || "Unknown"; + const channel = pluginData.guild.channels.resolve(matchResult.extra.matchedChannelId); + const channelName = channel?.name ?? "Unknown"; const member = contexts[0].member!; const memberName = `**${member.user.username}#${member.user.discriminator}** (\`${member.id}\`)`; - const voiceOrStage = channel?.type === Constants.ChannelTypes.GUILD_STAGE ? "stage" : "voice"; + const voiceOrStage = channel?.type === ChannelTypeStrings.STAGE ? "stage" : "voice"; return `${memberName} has left the ${channelName} (\`${matchResult.extra.matchedChannelId}\`) ${voiceOrStage} channel`; }, }); From 9c437a9a984767c1e2d7059b56de4a8f38d60a77 Mon Sep 17 00:00:00 2001 From: almeidx Date: Sat, 14 Aug 2021 16:26:42 +0100 Subject: [PATCH 09/16] default include_moves to false --- backend/src/plugins/Automod/triggers/joinVoiceChannel.ts | 4 +++- backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts index f594dd3d..6e310510 100644 --- a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts @@ -12,7 +12,9 @@ export const JoinVoiceChannelTrigger = automodTrigger()( include_moves: t.boolean, }), - defaultConfig: {}, + defaultConfig: { + include_moves: false, + }, async match({ triggerConfig, context }) { const matchedChannelId = context.voiceChannel?.joined?.id; diff --git a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts index 2e83e423..309240ca 100644 --- a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts @@ -12,7 +12,9 @@ export const LeaveVoiceChannelTrigger = automodTrigger( include_moves: t.boolean, }), - defaultConfig: {}, + defaultConfig: { + include_moves: false, + }, async match({ triggerConfig, context }) { const matchedChannelId = context.voiceChannel?.left?.id; From 5ad461260ac29f7f403fbe2e95718c574d1068c9 Mon Sep 17 00:00:00 2001 From: almeidx Date: Sat, 14 Aug 2021 16:33:01 +0100 Subject: [PATCH 10/16] cleaned up the event --- .../events/runAutomodOnVoiceStateUpdate.ts | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts b/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts index 1726f35c..18c8c518 100644 --- a/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts +++ b/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts @@ -13,43 +13,27 @@ export const RunAutomodOnVoiceStateUpdate = typedGuildEventListener { - runAutomod(pluginData, context); - }); + context.voiceChannel!.joined = newChannel; + addToQueue = true; } else if (oldChannel && !newChannel) { - const context: AutomodContext = { - member, - timestamp, - voiceChannel: { - left: oldChannel, - }, - user: member.user, - }; - - pluginData.state.queue.add(() => { - runAutomod(pluginData, context); - }); + context.voiceChannel!.left = oldChannel; + addToQueue = true; } else if (oldChannel?.id && newChannel?.id && oldChannel.id === newChannel.id) { - const context: AutomodContext = { - member, - timestamp, - voiceChannel: { - left: oldChannel, - joined: newChannel, - }, - user: member.user, - }; + context.voiceChannel!.left = oldChannel; + context.voiceChannel!.joined = newChannel; + addToQueue = true; + } + if (addToQueue) { pluginData.state.queue.add(() => { runAutomod(pluginData, context); }); From b6d433cbb163823c163b355ddc847886bbb77fb7 Mon Sep 17 00:00:00 2001 From: almeidx Date: Sat, 14 Aug 2021 16:35:20 +0100 Subject: [PATCH 11/16] made the context initializer more clear --- .../plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts | 5 ++++- backend/src/plugins/Automod/types.ts | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts b/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts index 18c8c518..825df1ff 100644 --- a/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts +++ b/backend/src/plugins/Automod/events/runAutomodOnVoiceStateUpdate.ts @@ -16,7 +16,10 @@ export const RunAutomodOnVoiceStateUpdate = typedGuildEventListener Date: Sat, 14 Aug 2021 16:46:52 +0100 Subject: [PATCH 12/16] undo name changes --- .../{runAutomodOnJoinLeaveEvt.ts => RunAutomodOnJoinLeaveEvt.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename backend/src/plugins/Automod/events/{runAutomodOnJoinLeaveEvt.ts => RunAutomodOnJoinLeaveEvt.ts} (100%) diff --git a/backend/src/plugins/Automod/events/runAutomodOnJoinLeaveEvt.ts b/backend/src/plugins/Automod/events/RunAutomodOnJoinLeaveEvt.ts similarity index 100% rename from backend/src/plugins/Automod/events/runAutomodOnJoinLeaveEvt.ts rename to backend/src/plugins/Automod/events/RunAutomodOnJoinLeaveEvt.ts From 683f9fc5a495235e81f9e2832ff81b462cc60ad5 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sat, 14 Aug 2021 16:47:14 +0100 Subject: [PATCH 13/16] undo name changes --- .../{runAutomodOnMemberUpdate.ts => RunAutomodOnMemberUpdate.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename backend/src/plugins/Automod/events/{runAutomodOnMemberUpdate.ts => RunAutomodOnMemberUpdate.ts} (100%) diff --git a/backend/src/plugins/Automod/events/runAutomodOnMemberUpdate.ts b/backend/src/plugins/Automod/events/RunAutomodOnMemberUpdate.ts similarity index 100% rename from backend/src/plugins/Automod/events/runAutomodOnMemberUpdate.ts rename to backend/src/plugins/Automod/events/RunAutomodOnMemberUpdate.ts From 8a30c50fa87d2b0b4bb90776d2b5b3c437b640db Mon Sep 17 00:00:00 2001 From: almeidx Date: Sat, 14 Aug 2021 16:51:06 +0100 Subject: [PATCH 14/16] revert file name changes --- backend/src/plugins/Automod/AutomodPlugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Automod/AutomodPlugin.ts b/backend/src/plugins/Automod/AutomodPlugin.ts index b8d9be04..48a0afbf 100644 --- a/backend/src/plugins/Automod/AutomodPlugin.ts +++ b/backend/src/plugins/Automod/AutomodPlugin.ts @@ -20,8 +20,8 @@ import { AntiraidClearCmd } from "./commands/AntiraidClearCmd"; import { SetAntiraidCmd } from "./commands/SetAntiraidCmd"; import { ViewAntiraidCmd } from "./commands/ViewAntiraidCmd"; import { runAutomodOnCounterTrigger } from "./events/runAutomodOnCounterTrigger"; -import { RunAutomodOnJoinEvt, RunAutomodOnLeaveEvt } from "./events/runAutomodOnJoinLeaveEvt"; -import { RunAutomodOnMemberUpdate } from "./events/runAutomodOnMemberUpdate"; +import { RunAutomodOnJoinEvt, RunAutomodOnLeaveEvt } from "./events/RunAutomodOnJoinLeaveEvt"; +import { RunAutomodOnMemberUpdate } from "./events/RunAutomodOnMemberUpdate"; import { runAutomodOnMessage } from "./events/runAutomodOnMessage"; import { runAutomodOnModAction } from "./events/runAutomodOnModAction"; import { From c19ca674d8120f5dbf055ad6512cad84f792978c Mon Sep 17 00:00:00 2001 From: almeidx Date: Sun, 14 Nov 2021 14:42:02 +0000 Subject: [PATCH 15/16] rebase shenanigans --- backend/src/plugins/Automod/AutomodPlugin.ts | 2 +- backend/src/plugins/Automod/types.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Automod/AutomodPlugin.ts b/backend/src/plugins/Automod/AutomodPlugin.ts index 48a0afbf..1fff2a32 100644 --- a/backend/src/plugins/Automod/AutomodPlugin.ts +++ b/backend/src/plugins/Automod/AutomodPlugin.ts @@ -214,7 +214,7 @@ export const AutomodPlugin = zeppelinGuildPlugin()({ RunAutomodOnLeaveEvt, RunAutomodOnThreadCreate, RunAutomodOnThreadDelete, - RunAutomodOnThreadUpdate + RunAutomodOnThreadUpdate, RunAutomodOnVoiceStateUpdate, // Messages use message events from SavedMessages, see onLoad below ], diff --git a/backend/src/plugins/Automod/types.ts b/backend/src/plugins/Automod/types.ts index 601a442d..557d24c9 100644 --- a/backend/src/plugins/Automod/types.ts +++ b/backend/src/plugins/Automod/types.ts @@ -1,4 +1,12 @@ -import { GuildMember, PartialGuildMember, TextChannel, ThreadChannel, User } from "discord.js"; +import { + GuildMember, + PartialGuildMember, + StageChannel, + TextChannel, + ThreadChannel, + User, + VoiceChannel, +} from "discord.js"; import * as t from "io-ts"; import { BasePluginType, CooldownManager } from "knub"; import { SavedMessage } from "../../data/entities/SavedMessage"; From 3acb089f47435e665136840c9dc82187e4ccaf4f Mon Sep 17 00:00:00 2001 From: almeidx Date: Sun, 14 Nov 2021 14:47:43 +0000 Subject: [PATCH 16/16] remove channels config from join/leave vc --- backend/src/plugins/Automod/triggers/joinVoiceChannel.ts | 9 ++------- .../src/plugins/Automod/triggers/leaveVoiceChannel.ts | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts index 6e310510..e2093fa9 100644 --- a/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/joinVoiceChannel.ts @@ -1,5 +1,6 @@ import * as t from "io-ts"; import { ChannelTypeStrings } from "../../../types"; +import { tNullable } from "../../../utils"; import { automodTrigger } from "../helpers"; interface JoinVoiceChannelResult { @@ -8,8 +9,7 @@ interface JoinVoiceChannelResult { export const JoinVoiceChannelTrigger = automodTrigger()({ configType: t.type({ - channels: t.union([t.string, t.array(t.string)]), - include_moves: t.boolean, + include_moves: tNullable(t.boolean), }), defaultConfig: { @@ -25,11 +25,6 @@ export const JoinVoiceChannelTrigger = automodTrigger()( return; } - const triggerChannels = Array.isArray(triggerConfig.channels) ? triggerConfig.channels : [triggerConfig.channels]; - if (!triggerChannels.includes(matchedChannelId)) { - return; - } - return { extra: { matchedChannelId, diff --git a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts index 309240ca..cde57d7c 100644 --- a/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts +++ b/backend/src/plugins/Automod/triggers/leaveVoiceChannel.ts @@ -1,5 +1,6 @@ import * as t from "io-ts"; import { ChannelTypeStrings } from "../../../types"; +import { tNullable } from "../../../utils"; import { automodTrigger } from "../helpers"; interface LeaveVoiceChannelResult { @@ -8,8 +9,7 @@ interface LeaveVoiceChannelResult { export const LeaveVoiceChannelTrigger = automodTrigger()({ configType: t.type({ - channels: t.union([t.string, t.array(t.string)]), - include_moves: t.boolean, + include_moves: tNullable(t.boolean), }), defaultConfig: { @@ -25,11 +25,6 @@ export const LeaveVoiceChannelTrigger = automodTrigger( return; } - const triggerChannels = Array.isArray(triggerConfig.channels) ? triggerConfig.channels : [triggerConfig.channels]; - if (!triggerChannels.includes(matchedChannelId)) { - return; - } - return { extra: { matchedChannelId,