3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

Port CustomEventsPlugin

This commit is contained in:
Dragory 2020-07-30 02:47:47 +03:00
parent 28de8a592b
commit 4c4496600b
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
9 changed files with 268 additions and 0 deletions

View file

@ -0,0 +1,36 @@
import { PluginData } from "knub";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import * as t from "io-ts";
import { renderTemplate } from "../../../templateFormatter";
import { resolveMember } from "../../../utils";
import { ActionError } from "../ActionError";
import { canActOn } from "../../../pluginUtils";
import { Message } from "eris";
export const AddRoleAction = t.type({
type: t.literal("add_role"),
target: t.string,
role: t.union([t.string, t.array(t.string)]),
});
export type TAddRoleAction = t.TypeOf<typeof AddRoleAction>;
export async function addRoleAction(
pluginData: PluginData<CustomEventsPluginType>,
action: TAddRoleAction,
values: any,
event: TCustomEvent,
eventData: any,
) {
const targetId = await renderTemplate(action.target, values, false);
const target = await resolveMember(pluginData.client, pluginData.guild, targetId);
if (!target) throw new ActionError(`Unknown target member: ${targetId}`);
if (event.trigger.type === "command" && !canActOn(pluginData, (eventData.msg as Message).member, target)) {
throw new ActionError("Missing permissions");
}
const rolesToAdd = Array.isArray(action.role) ? action.role : [action.role];
await target.edit({
roles: Array.from(new Set([...target.roles, ...rolesToAdd])),
});
}

View file

@ -0,0 +1,41 @@
import { PluginData } from "knub";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import * as t from "io-ts";
import { renderTemplate } from "../../../templateFormatter";
import { CaseTypes } from "../../../data/CaseTypes";
import { ActionError } from "../ActionError";
import { CasesPlugin } from "../../Cases/CasesPlugin";
export const CreateCaseAction = t.type({
type: t.literal("create_case"),
case_type: t.string,
mod: t.string,
target: t.string,
reason: t.string,
});
export type TCreateCaseAction = t.TypeOf<typeof CreateCaseAction>;
export async function createCaseAction(
pluginData: PluginData<CustomEventsPluginType>,
action: TCreateCaseAction,
values: any,
event: TCustomEvent,
eventData: any,
) {
const modId = await renderTemplate(action.mod, values, false);
const targetId = await renderTemplate(action.target, values, false);
const reason = await renderTemplate(action.reason, values, false);
if (CaseTypes[action.case_type] == null) {
throw new ActionError(`Invalid case type: ${action.type}`);
}
const casesPlugin = pluginData.getPlugin(CasesPlugin);
await casesPlugin.createCase({
userId: targetId,
modId,
type: CaseTypes[action.case_type],
reason: `__[${event.name}]__ ${reason}`,
});
}

View file

@ -0,0 +1,26 @@
import { PluginData } from "knub";
import { CustomEventsPluginType } from "../types";
import * as t from "io-ts";
import { renderTemplate } from "../../../templateFormatter";
import { ActionError } from "../ActionError";
import { TextChannel } from "eris";
export const MessageAction = t.type({
type: t.literal("message"),
channel: t.string,
content: t.string,
});
export type TMessageAction = t.TypeOf<typeof MessageAction>;
export async function messageAction(
pluginData: PluginData<CustomEventsPluginType>,
action: TMessageAction,
values: any,
) {
const targetChannelId = await renderTemplate(action.channel, values, false);
const targetChannel = pluginData.guild.channels.get(targetChannelId);
if (!targetChannel) throw new ActionError("Unknown target channel");
if (!(targetChannel instanceof TextChannel)) throw new ActionError("Target channel is not a text channel");
await targetChannel.createMessage({ content: action.content });
}

View file

@ -0,0 +1,41 @@
import { PluginData } from "knub";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import * as t from "io-ts";
import { renderTemplate } from "../../../templateFormatter";
import { resolveMember } from "../../../utils";
import { ActionError } from "../ActionError";
import { canActOn } from "../../../pluginUtils";
import { Message, VoiceChannel } from "eris";
export const MoveToVoiceChannelAction = t.type({
type: t.literal("move_to_vc"),
target: t.string,
channel: t.string,
});
export type TMoveToVoiceChannelAction = t.TypeOf<typeof MoveToVoiceChannelAction>;
export async function moveToVoiceChannelAction(
pluginData: PluginData<CustomEventsPluginType>,
action: TMoveToVoiceChannelAction,
values: any,
event: TCustomEvent,
eventData: any,
) {
const targetId = await renderTemplate(action.target, values, false);
const target = await resolveMember(pluginData.client, pluginData.guild, targetId);
if (!target) throw new ActionError("Unknown target member");
if (event.trigger.type === "command" && !canActOn(pluginData, (eventData.msg as Message).member, target)) {
throw new ActionError("Missing permissions");
}
const targetChannelId = await renderTemplate(action.channel, values, false);
const targetChannel = pluginData.guild.channels.get(targetChannelId);
if (!targetChannel) throw new ActionError("Unknown target channel");
if (!(targetChannel instanceof VoiceChannel)) throw new ActionError("Target channel is not a voice channel");
if (!target.voiceState.channelID) return;
await target.edit({
channelID: targetChannel.id,
});
}