Skip to content

Commit

Permalink
Merge pull request #475 from rubyowo/feat/pretty_name
Browse files Browse the repository at this point in the history
feat: pretty_name for automod
  • Loading branch information
Dragory authored May 19, 2024
2 parents 8ad12a9 + dfb2b82 commit 0a38440
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/src/data/DefaultLogMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"BOT_ALERT": "{timestamp} ⚠ **BOT ALERT:** {tmplEval(body)}",
"DM_FAILED": "{timestamp} \uD83D\uDEA7 Failed to send DM ({source}) to {userMention(user)}",

"AUTOMOD_ACTION": "{timestamp} \uD83E\uDD16 Automod rule **{rule}** triggered by {userMention(users)}\n{matchSummary}\nActions taken: **{actionsTaken}**",
"AUTOMOD_ACTION": "{timestamp} \uD83E\uDD16 Automod rule **{if(not(prettyName), rule, prettyName)}** triggered by {userMention(users)}\n{matchSummary}\nActions taken: **{actionsTaken}**",
"SET_ANTIRAID_USER": "{timestamp} ⚔ {userMention(user)} set anti-raid to **{level}**",
"SET_ANTIRAID_AUTO": "{timestamp} ⚔ Anti-raid automatically set to **{level}**"
}
4 changes: 3 additions & 1 deletion backend/src/plugins/Automod/actions/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const configSchema = z.object({
export const AlertAction = automodAction({
configSchema,

async apply({ pluginData, contexts, actionConfig, ruleName, matchResult }) {
async apply({ pluginData, contexts, actionConfig, ruleName, matchResult, prettyName }) {
const channel = pluginData.guild.channels.cache.get(actionConfig.channel as Snowflake);
const logs = pluginData.getPlugin(LogsPlugin);

Expand All @@ -55,6 +55,7 @@ export const AlertAction = automodAction({
users: safeUsers,
actionsTaken,
matchSummary: matchResult.summary ?? "",
prettyName,
}),
);

Expand All @@ -69,6 +70,7 @@ export const AlertAction = automodAction({
text,
actionsTaken,
matchSummary: matchResult.summary,
prettyName,
messageLink: theMessageLink,
logMessage: validateAndParseMessageContent(logMessage)?.content,
}),
Expand Down
3 changes: 2 additions & 1 deletion backend/src/plugins/Automod/actions/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { automodAction } from "../helpers.js";
export const LogAction = automodAction({
configSchema: z.boolean().default(true),

async apply({ pluginData, contexts, ruleName, matchResult }) {
async apply({ pluginData, contexts, ruleName, matchResult, prettyName }) {
const users = unique(contexts.map((c) => c.user)).filter(isTruthy);
const user = users[0];
const actionsTaken = Object.keys(pluginData.config.get().rules[ruleName].actions).join(", ");

pluginData.getPlugin(LogsPlugin).logAutomodAction({
rule: ruleName,
prettyName,
user,
users,
actionsTaken,
Expand Down
8 changes: 7 additions & 1 deletion backend/src/plugins/Automod/functions/runAutomod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
});

for (const [ruleName, rule] of Object.entries(config.rules)) {
const prettyName = rule.pretty_name;

if (rule.enabled === false) continue;
if (
!rule.affects_bots &&
Expand Down Expand Up @@ -100,6 +102,7 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
contexts,
actionConfig: true,
matchResult,
prettyName,
});
return;
}
Expand All @@ -113,7 +116,9 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
triggerConfig,
})) ?? "";

matchResult.fullSummary = `Triggered automod rule **${ruleName}**\n${matchResult.summary}`.trim();
matchResult.fullSummary = `Triggered automod rule **${prettyName ?? ruleName}**\n${
matchResult.summary
}`.trim();
}

if (profilingEnabled()) {
Expand Down Expand Up @@ -147,6 +152,7 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
contexts,
actionConfig,
matchResult,
prettyName,
});

if (profilingEnabled()) {
Expand Down
1 change: 1 addition & 0 deletions backend/src/plugins/Automod/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type AutomodActionApplyFn<TConfigType> = (meta: {
contexts: AutomodContext[];
actionConfig: TConfigType;
matchResult: AutomodTriggerMatchResult;
prettyName: string | undefined;
}) => Awaitable<void>;

export interface AutomodActionBlueprint<TConfigSchema extends ZodTypeAny> {
Expand Down
1 change: 1 addition & 0 deletions backend/src/plugins/Automod/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const zRule = z.strictObject({
}
return ruleName;
}),
pretty_name: z.string().optional(),
presets: z.array(z.string().max(100)).max(25).default([]),
affects_bots: z.boolean().default(false),
affects_self: z.boolean().default(false),
Expand Down
2 changes: 2 additions & 0 deletions backend/src/plugins/Logs/logFunctions/logAutomodAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { log } from "../util/log.js";

export interface LogAutomodActionData {
rule: string;
prettyName: string | undefined;
user?: User | null;
users: User[];
actionsTaken: string;
Expand All @@ -20,6 +21,7 @@ export function logAutomodAction(pluginData: GuildPluginData<LogsPluginType>, da
LogType.AUTOMOD_ACTION,
createTypedTemplateSafeValueContainer({
rule: data.rule,
prettyName: data.prettyName,
user: data.user ? userToTemplateSafeUser(data.user) : null,
users: data.users.map((user) => userToTemplateSafeUser(user)),
actionsTaken: data.actionsTaken,
Expand Down

0 comments on commit 0a38440

Please sign in to comment.