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

Type fixes + use template safe values for renderTemplate() everywhere

This commit is contained in:
Dragory 2021-08-18 20:32:45 +03:00
parent e16eb8c8d1
commit d109a58cb7
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
21 changed files with 190 additions and 98 deletions

View file

@ -1,9 +1,18 @@
import { parseSignature, typedGuildCommand } from "knub";
import { commandTypes } from "../../commandTypes";
import { stripObjectToScalars } from "../../utils";
import { stripObjectToScalars, UnknownUser } from "../../utils";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { runEvent } from "./functions/runEvent";
import { ConfigSchema, CustomEventsPluginType } from "./types";
import { createTypedTemplateSafeValueContainer, TemplateSafeValueContainer } from "../../templateFormatter";
import { Channel, GuildChannel, GuildMember, ThreadChannel, User } from "discord.js";
import {
channelToTemplateSafeChannel,
memberToTemplateSafeMember,
messageToTemplateSafeMessage,
userToTemplateSafeUser,
} from "../../utils/templateSafeObjects";
import { isScalar } from "../../utils/isScalar";
const defaultOptions = {
config: {
@ -28,8 +37,25 @@ export const CustomEventsPlugin = zeppelinGuildPlugin<CustomEventsPluginType>()(
permission: `events.${key}.trigger.can_use`,
signature,
run({ message, args }) {
const strippedMsg = stripObjectToScalars(message, ["channel", "author"]);
runEvent(pluginData, event, { msg: message, args }, { args, msg: strippedMsg });
const safeArgs = new TemplateSafeValueContainer();
for (const [argKey, argValue] of Object.entries(args as Record<string, unknown>)) {
if (argValue instanceof User || argValue instanceof UnknownUser) {
safeArgs[argKey] = userToTemplateSafeUser(argValue);
} else if (argValue instanceof GuildMember) {
safeArgs[argKey] = memberToTemplateSafeMember(argValue);
} else if (argValue instanceof GuildChannel || argValue instanceof ThreadChannel) {
safeArgs[argKey] = channelToTemplateSafeChannel(argValue);
} else if (isScalar(argValue)) {
safeArgs[argKey] = argValue;
}
}
const values = createTypedTemplateSafeValueContainer({
...args,
msg: messageToTemplateSafeMessage(message),
});
runEvent(pluginData, event, { msg: message, args }, values);
},
});
pluginData.commands.add(eventCommand);

View file

@ -2,7 +2,7 @@ import { Snowflake } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import { canActOn } from "../../../pluginUtils";
import { renderTemplate } from "../../../templateFormatter";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import { resolveMember } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
@ -17,7 +17,7 @@ export type TAddRoleAction = t.TypeOf<typeof AddRoleAction>;
export async function addRoleAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
action: TAddRoleAction,
values: any,
values: TemplateSafeValueContainer,
event: TCustomEvent,
eventData: any,
) {

View file

@ -1,7 +1,7 @@
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import { CaseTypes } from "../../../data/CaseTypes";
import { renderTemplate } from "../../../templateFormatter";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
@ -18,7 +18,7 @@ export type TCreateCaseAction = t.TypeOf<typeof CreateCaseAction>;
export async function createCaseAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
action: TCreateCaseAction,
values: any,
values: TemplateSafeValueContainer,
event: TCustomEvent,
eventData: any,
) {

View file

@ -4,6 +4,7 @@ import { GuildPluginData } from "knub";
import { convertDelayStringToMS, noop, tDelayString } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import { TemplateSafeValueContainer } from "../../../templateFormatter";
export const MakeRoleMentionableAction = t.type({
type: t.literal("make_role_mentionable"),
@ -15,7 +16,7 @@ export type TMakeRoleMentionableAction = t.TypeOf<typeof MakeRoleMentionableActi
export async function makeRoleMentionableAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
action: TMakeRoleMentionableAction,
values: any,
values: TemplateSafeValueContainer,
event: TCustomEvent,
eventData: any,
) {

View file

@ -3,6 +3,7 @@ import * as t from "io-ts";
import { GuildPluginData } from "knub";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import { TemplateSafeValueContainer } from "../../../templateFormatter";
export const MakeRoleUnmentionableAction = t.type({
type: t.literal("make_role_unmentionable"),
@ -13,7 +14,7 @@ export type TMakeRoleUnmentionableAction = t.TypeOf<typeof MakeRoleUnmentionable
export async function makeRoleUnmentionableAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
action: TMakeRoleUnmentionableAction,
values: any,
values: TemplateSafeValueContainer,
event: TCustomEvent,
eventData: any,
) {

View file

@ -1,7 +1,7 @@
import { Snowflake, TextChannel } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import { renderTemplate } from "../../../templateFormatter";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType } from "../types";
@ -15,7 +15,7 @@ export type TMessageAction = t.TypeOf<typeof MessageAction>;
export async function messageAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
action: TMessageAction,
values: any,
values: TemplateSafeValueContainer,
) {
const targetChannelId = await renderTemplate(action.channel, values, false);
const targetChannel = pluginData.guild.channels.cache.get(targetChannelId as Snowflake);

View file

@ -2,7 +2,7 @@ import { Snowflake, VoiceChannel } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import { canActOn } from "../../../pluginUtils";
import { renderTemplate } from "../../../templateFormatter";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import { resolveMember } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
@ -17,7 +17,7 @@ export type TMoveToVoiceChannelAction = t.TypeOf<typeof MoveToVoiceChannelAction
export async function moveToVoiceChannelAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
action: TMoveToVoiceChannelAction,
values: any,
values: TemplateSafeValueContainer,
event: TCustomEvent,
eventData: any,
) {

View file

@ -3,6 +3,7 @@ import * as t from "io-ts";
import { GuildPluginData } from "knub";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import { TemplateSafeValueContainer } from "../../../templateFormatter";
export const SetChannelPermissionOverridesAction = t.type({
type: t.literal("set_channel_permission_overrides"),
@ -21,7 +22,7 @@ export type TSetChannelPermissionOverridesAction = t.TypeOf<typeof SetChannelPer
export async function setChannelPermissionOverridesAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
action: TSetChannelPermissionOverridesAction,
values: any,
values: TemplateSafeValueContainer,
event: TCustomEvent,
eventData: any,
) {

View file

@ -10,12 +10,13 @@ import { messageAction } from "../actions/messageAction";
import { moveToVoiceChannelAction } from "../actions/moveToVoiceChannelAction";
import { setChannelPermissionOverridesAction } from "../actions/setChannelPermissionOverrides";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import { TemplateSafeValueContainer } from "../../../templateFormatter";
export async function runEvent(
pluginData: GuildPluginData<CustomEventsPluginType>,
event: TCustomEvent,
eventData: any,
values: any,
values: TemplateSafeValueContainer,
) {
try {
for (const action of event.actions) {