3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

automod.reply: allow embeds, add auto_delete option

This commit is contained in:
Dragory 2020-05-22 23:38:11 +03:00
parent 1cff4fb801
commit 53a9c58dd4
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
4 changed files with 55 additions and 30 deletions

View file

@ -13,6 +13,7 @@ import {
messageSummary,
MINUTES,
noop,
renderRecursively,
SECONDS,
stripObjectToScalars,
tDeepPartial,
@ -1297,12 +1298,24 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema, ICustomOverride
const channel = this.guild.channels.get(channelId);
if (channel && channel instanceof TextChannel) {
const user = await this.resolveUser(matchResult.userId);
const formatted = await renderTemplate(rule.actions.reply, {
user: stripObjectToScalars(user),
});
const renderReplyText = async str =>
renderTemplate(str, {
user: stripObjectToScalars(user),
});
const formatted =
typeof rule.actions.reply === "string"
? await renderReplyText(rule.actions.reply)
: await renderRecursively(rule.actions.reply.text, renderReplyText);
if (formatted) {
await channel.createMessage(formatted);
const replyMsg = await channel.createMessage(formatted);
actionsTaken.push("reply");
if (typeof rule.actions.reply === "object" && rule.actions.reply.auto_delete != null) {
const delay = convertDelayStringToMS(String(rule.actions.reply.auto_delete));
setTimeout(() => replyMsg.delete().catch(noop), delay);
console.log("deleting in", delay);
}
}
}
}

View file

@ -1,6 +1,6 @@
import * as t from "io-ts";
import { TSafeRegex } from "../../validatorUtils";
import { tDelayString, tNullable } from "../../utils";
import { tDelayString, tMessageContent, tNullable } from "../../utils";
export enum RecentActionType {
Message = 1,
@ -286,7 +286,13 @@ export const RemoveRolesAction = t.array(t.string);
export const SetAntiraidLevelAction = t.string;
export const ReplyAction = t.string;
export const ReplyAction = t.union([
t.string,
t.type({
text: tMessageContent,
auto_delete: tNullable(t.union([t.string, t.number])),
}),
]);
/**
* RULES

View file

@ -3,6 +3,7 @@ import { Member, Message, TextChannel } from "eris";
import {
convertDelayStringToMS,
errorMessage,
renderRecursively,
StrictMessageContent,
stripObjectToScalars,
tEmbed,
@ -353,29 +354,6 @@ export class TagsPlugin extends ZeppelinPlugin<TConfigSchema> {
const variableStr = str.slice(prefix.length + tagName.length).trim();
const tagArgs = parseArguments(variableStr).map(v => v.value);
// Renders strings in objects and arrays recursively, effectively supporting embeds for tags
const renderTagValue = async value => {
if (Array.isArray(value)) {
const result = [];
for (const item of value) {
result.push(await renderTagValue(item));
}
return result;
} else if (value == null) {
return null;
} else if (typeof value === "object") {
const result = {};
for (const [prop, _value] of Object.entries(value)) {
result[prop] = await renderTagValue(_value);
}
return result;
} else if (typeof value === "string") {
return renderTagString(value);
}
return value;
};
const renderTagString = async _str => {
let rendered = await this.renderTag(_str, tagArgs, {
member: stripObjectToScalars(member, ["user"]),
@ -388,7 +366,9 @@ export class TagsPlugin extends ZeppelinPlugin<TConfigSchema> {
// Format the string
try {
return typeof tagBody === "string" ? { content: await renderTagString(tagBody) } : await renderTagValue(tagBody);
return typeof tagBody === "string"
? { content: await renderTagString(tagBody) }
: await renderRecursively(tagBody, renderTagString);
} catch (e) {
if (e instanceof TemplateParseError) {
logger.warn(`Invalid tag format!\nError: ${e.message}\nFormat: ${tagBody}`);

View file

@ -204,6 +204,8 @@ export const tStrictMessageContent = t.type({
embed: tNullable(tEmbed),
});
export const tMessageContent = t.union([t.string, tStrictMessageContent]);
export function dropPropertiesByName(obj, propName) {
if (obj.hasOwnProperty(propName)) delete obj[propName];
for (const value of Object.values(obj)) {
@ -1126,3 +1128,27 @@ export function memoize<T>(fn: (...args: any[]) => T, key?, time?): T {
return value;
}
type RecursiveRenderFn = (str: string) => string | Promise<string>;
export async function renderRecursively(value, fn: RecursiveRenderFn) {
if (Array.isArray(value)) {
const result = [];
for (const item of value) {
result.push(await renderRecursively(item, fn));
}
return result;
} else if (value === null) {
return null;
} else if (typeof value === "object") {
const result = {};
for (const [prop, _value] of Object.entries(value)) {
result[prop] = await renderRecursively(_value, fn);
}
return result;
} else if (typeof value === "string") {
return fn(value);
}
return value;
}