Add 'make_role_mentionable' and 'make_role_unmentionable' custom event actions

This commit is contained in:
Dragory 2020-11-22 14:14:17 +02:00
parent bc5455bf9f
commit 7839cc7da1
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
4 changed files with 90 additions and 1 deletions

View file

@ -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<typeof MakeRoleMentionableAction>;
export async function makeRoleMentionableAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
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);
}

View file

@ -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<typeof MakeRoleUnmentionableAction>;
export async function makeRoleUnmentionableAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
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}`,
);
}

View file

@ -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<CustomEventsPluginType>,
@ -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) {

View file

@ -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<typeof CommandTrigger>;
const AnyTrigger = CommandTrigger; // TODO: Make into a union once we have more triggers
type TAnyTrigger = t.TypeOf<typeof AnyTrigger>;
const AnyAction = t.union([AddRoleAction, CreateCaseAction, MoveToVoiceChannelAction, MessageAction]);
const AnyAction = t.union([
AddRoleAction,
CreateCaseAction,
MoveToVoiceChannelAction,
MessageAction,
MakeRoleMentionableAction,
MakeRoleUnmentionableAction,
]);
type TAnyAction = t.TypeOf<typeof AnyAction>;
export const CustomEvent = t.type({