diff --git a/backend/src/plugins/CustomEvents/actions/makeRoleMentionableAction.ts b/backend/src/plugins/CustomEvents/actions/makeRoleMentionableAction.ts new file mode 100644 index 00000000..90ddd45b --- /dev/null +++ b/backend/src/plugins/CustomEvents/actions/makeRoleMentionableAction.ts @@ -0,0 +1,44 @@ +import { GuildPluginData } from "knub"; +import { CustomEventsPluginType, TCustomEvent } from "../types"; +import * as t from "io-ts"; +import { convertDelayStringToMS, noop, tDelayString } from "../../../utils"; +import { ActionError } from "../ActionError"; + +export const MakeRoleMentionableAction = t.type({ + type: t.literal("make_role_mentionable"), + role: t.string, + timeout: tDelayString, +}); +export type TMakeRoleMentionableAction = t.TypeOf; + +export async function makeRoleMentionableAction( + pluginData: GuildPluginData, + action: TMakeRoleMentionableAction, + values: any, + event: TCustomEvent, + eventData: any, +) { + const role = pluginData.guild.roles.get(action.role); + if (!role) { + throw new ActionError(`Unknown role: ${role}`); + } + + await role.edit( + { + mentionable: true, + }, + `Custom event: ${event.name}`, + ); + + const timeout = convertDelayStringToMS(action.timeout)!; + setTimeout(() => { + role + .edit( + { + mentionable: false, + }, + `Custom event: ${event.name}`, + ) + .catch(noop); + }, timeout); +} diff --git a/backend/src/plugins/CustomEvents/actions/makeRoleUnmentionableAction.ts b/backend/src/plugins/CustomEvents/actions/makeRoleUnmentionableAction.ts new file mode 100644 index 00000000..0ceef330 --- /dev/null +++ b/backend/src/plugins/CustomEvents/actions/makeRoleUnmentionableAction.ts @@ -0,0 +1,30 @@ +import { GuildPluginData } from "knub"; +import { CustomEventsPluginType, TCustomEvent } from "../types"; +import * as t from "io-ts"; +import { ActionError } from "../ActionError"; + +export const MakeRoleUnmentionableAction = t.type({ + type: t.literal("make_role_unmentionable"), + role: t.string, +}); +export type TMakeRoleUnmentionableAction = t.TypeOf; + +export async function makeRoleUnmentionableAction( + pluginData: GuildPluginData, + action: TMakeRoleUnmentionableAction, + values: any, + event: TCustomEvent, + eventData: any, +) { + const role = pluginData.guild.roles.get(action.role); + if (!role) { + throw new ActionError(`Unknown role: ${role}`); + } + + await role.edit( + { + mentionable: false, + }, + `Custom event: ${event.name}`, + ); +} diff --git a/backend/src/plugins/CustomEvents/functions/runEvent.ts b/backend/src/plugins/CustomEvents/functions/runEvent.ts index 56d3a9aa..bd0b3ef5 100644 --- a/backend/src/plugins/CustomEvents/functions/runEvent.ts +++ b/backend/src/plugins/CustomEvents/functions/runEvent.ts @@ -7,6 +7,8 @@ import { addRoleAction } from "../actions/addRoleAction"; import { createCaseAction } from "../actions/createCaseAction"; import { moveToVoiceChannelAction } from "../actions/moveToVoiceChannelAction"; import { messageAction } from "../actions/messageAction"; +import { makeRoleMentionableAction } from "../actions/makeRoleMentionableAction"; +import { makeRoleUnmentionableAction } from "../actions/makeRoleUnmentionableAction"; export async function runEvent( pluginData: GuildPluginData, @@ -24,6 +26,10 @@ export async function runEvent( await moveToVoiceChannelAction(pluginData, action, values, event, eventData); } else if (action.type === "message") { await messageAction(pluginData, action, values); + } else if (action.type === "make_role_mentionable") { + await makeRoleMentionableAction(pluginData, action, values, event, eventData); + } else if (action.type === "make_role_unmentionable") { + await makeRoleUnmentionableAction(pluginData, action, values, event, eventData); } } } catch (e) { diff --git a/backend/src/plugins/CustomEvents/types.ts b/backend/src/plugins/CustomEvents/types.ts index 7db0b14d..8bd0ffdf 100644 --- a/backend/src/plugins/CustomEvents/types.ts +++ b/backend/src/plugins/CustomEvents/types.ts @@ -4,6 +4,8 @@ import { AddRoleAction } from "./actions/addRoleAction"; import { CreateCaseAction } from "./actions/createCaseAction"; import { MoveToVoiceChannelAction } from "./actions/moveToVoiceChannelAction"; import { MessageAction } from "./actions/messageAction"; +import { MakeRoleMentionableAction } from "./actions/makeRoleMentionableAction"; +import { MakeRoleUnmentionableAction } from "./actions/makeRoleUnmentionableAction"; // Triggers const CommandTrigger = t.type({ @@ -17,7 +19,14 @@ type TCommandTrigger = t.TypeOf; const AnyTrigger = CommandTrigger; // TODO: Make into a union once we have more triggers type TAnyTrigger = t.TypeOf; -const AnyAction = t.union([AddRoleAction, CreateCaseAction, MoveToVoiceChannelAction, MessageAction]); +const AnyAction = t.union([ + AddRoleAction, + CreateCaseAction, + MoveToVoiceChannelAction, + MessageAction, + MakeRoleMentionableAction, + MakeRoleUnmentionableAction, +]); type TAnyAction = t.TypeOf; export const CustomEvent = t.type({