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

Typed log functions + more

This commit is contained in:
Dragory 2021-08-18 01:51:42 +03:00
parent d2ac700143
commit bed6589d48
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
166 changed files with 4021 additions and 869 deletions

View file

@ -23,7 +23,7 @@ export const AddRolesAction = automodAction({
const missingPermissions = getMissingPermissions(me.permissions, p.MANAGE_ROLES);
if (missingPermissions) {
const logs = pluginData.getPlugin(LogsPlugin);
logs.log(LogType.BOT_ALERT, {
logs.logBotAlert({
body: `Cannot add roles in Automod rule **${ruleName}**. ${missingPermissionError(missingPermissions)}`,
});
return;
@ -44,7 +44,7 @@ export const AddRolesAction = automodAction({
roleId => pluginData.guild.roles.cache.get(roleId as Snowflake)?.name || roleId,
);
const logs = pluginData.getPlugin(LogsPlugin);
logs.log(LogType.BOT_ALERT, {
logs.logBotAlert({
body: `Unable to assign the following roles in Automod rule **${ruleName}**: **${roleNamesWeCannotAssign.join(
"**, **",
)}**`,

View file

@ -2,6 +2,7 @@ import * as t from "io-ts";
import { LogType } from "../../../data/LogType";
import { CountersPlugin } from "../../Counters/CountersPlugin";
import { automodAction } from "../helpers";
import { LogsPlugin } from "../../Logs/LogsPlugin";
export const AddToCounterAction = automodAction({
configType: t.type({
@ -14,7 +15,7 @@ export const AddToCounterAction = automodAction({
async apply({ pluginData, contexts, actionConfig, matchResult, ruleName }) {
const countersPlugin = pluginData.getPlugin(CountersPlugin);
if (!countersPlugin.counterExists(actionConfig.counter)) {
pluginData.state.logs.log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Unknown counter \`${actionConfig.counter}\` in \`add_to_counter\` action of Automod rule \`${ruleName}\``,
});
return;

View file

@ -2,17 +2,24 @@ import { Snowflake, TextChannel } from "discord.js";
import * as t from "io-ts";
import { erisAllowedMentionsToDjsMentionOptions } from "src/utils/erisAllowedMentionsToDjsMentionOptions";
import { LogType } from "../../../data/LogType";
import { renderTemplate, TemplateParseError } from "../../../templateFormatter";
import {
createTypedTemplateSafeValueContainer,
renderTemplate,
TemplateParseError,
TemplateSafeValueContainer,
} from "../../../templateFormatter";
import {
createChunkedMessage,
messageLink,
stripObjectToScalars,
tAllowedMentions,
tNormalizedNullOptional,
isTruthy,
verboseChannelMention,
} from "../../../utils";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { automodAction } from "../helpers";
import { TemplateSafeUser, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
export const AlertAction = automodAction({
configType: t.type({
@ -32,33 +39,39 @@ export const AlertAction = automodAction({
const theMessageLink =
contexts[0].message && messageLink(pluginData.guild.id, contexts[0].message.channel_id, contexts[0].message.id);
const safeUsers = contexts.map(c => c.user && stripObjectToScalars(c.user)).filter(Boolean);
const safeUsers = contexts.map(c => (c.user ? userToTemplateSafeUser(c.user) : null)).filter(isTruthy);
const safeUser = safeUsers[0];
const actionsTaken = Object.keys(pluginData.config.get().rules[ruleName].actions).join(", ");
const logMessage = await logs.getLogMessage(LogType.AUTOMOD_ACTION, {
rule: ruleName,
user: safeUser,
users: safeUsers,
actionsTaken,
matchSummary: matchResult.summary,
});
let rendered;
try {
rendered = await renderTemplate(actionConfig.text, {
const logMessage = await logs.getLogMessage(
LogType.AUTOMOD_ACTION,
createTypedTemplateSafeValueContainer({
rule: ruleName,
user: safeUser,
users: safeUsers,
text,
actionsTaken,
matchSummary: matchResult.summary,
messageLink: theMessageLink,
logMessage,
});
matchSummary: matchResult.summary ?? "",
}),
);
let rendered;
try {
rendered = await renderTemplate(
actionConfig.text,
new TemplateSafeValueContainer({
rule: ruleName,
user: safeUser,
users: safeUsers,
text,
actionsTaken,
matchSummary: matchResult.summary,
messageLink: theMessageLink,
logMessage: logMessage?.content,
}),
);
} catch (err) {
if (err instanceof TemplateParseError) {
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Error in alert format of automod rule ${ruleName}: ${err.message}`,
});
return;
@ -75,13 +88,13 @@ export const AlertAction = automodAction({
);
} catch (err) {
if (err.code === 50001) {
logs.log(LogType.BOT_ALERT, {
logs.logBotAlert({
body: `Missing access to send alert to channel ${verboseChannelMention(
channel,
)} in automod rule **${ruleName}**`,
});
} else {
logs.log(LogType.BOT_ALERT, {
logs.logBotAlert({
body: `Error ${err.code || "UNKNOWN"} when sending alert to channel ${verboseChannelMention(
channel,
)} in automod rule **${ruleName}**`,
@ -89,7 +102,7 @@ export const AlertAction = automodAction({
}
}
} else {
logs.log(LogType.BOT_ALERT, {
logs.logBotAlert({
body: `Invalid channel id \`${actionConfig.channel}\` for alert action in automod rule **${ruleName}**`,
});
}

View file

@ -22,7 +22,7 @@ export const ChangeNicknameAction = automodAction({
const newName = typeof actionConfig === "string" ? actionConfig : actionConfig.name;
member.edit({ nick: newName }).catch(err => {
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Failed to change the nickname of \`${member.id}\``,
});
});

View file

@ -1,24 +1,23 @@
import * as t from "io-ts";
import { LogType } from "../../../data/LogType";
import { stripObjectToScalars, unique } from "../../../utils";
import { isTruthy, stripObjectToScalars, unique } from "../../../utils";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { automodAction } from "../helpers";
import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
export const LogAction = automodAction({
configType: t.boolean,
defaultConfig: true,
async apply({ pluginData, contexts, ruleName, matchResult }) {
const safeUsers = unique(contexts.map(c => c.user))
.filter(Boolean)
.map(user => stripObjectToScalars(user));
const safeUser = safeUsers[0];
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).log(LogType.AUTOMOD_ACTION, {
pluginData.getPlugin(LogsPlugin).logAutomodAction({
rule: ruleName,
user: safeUser,
users: safeUsers,
user,
users,
actionsTaken,
matchSummary: matchResult.summary,
});

View file

@ -55,7 +55,7 @@ export const MuteAction = automodAction({
);
} catch (e) {
if (e instanceof RecoverablePluginError && e.code === ERRORS.NO_MUTE_ROLE_IN_CONFIG) {
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Failed to mute <@!${userId}> in Automod rule \`${ruleName}\` because a mute role has not been specified in server config`,
});
} else {

View file

@ -24,7 +24,7 @@ export const RemoveRolesAction = automodAction({
const missingPermissions = getMissingPermissions(me.permissions, p.MANAGE_ROLES);
if (missingPermissions) {
const logs = pluginData.getPlugin(LogsPlugin);
logs.log(LogType.BOT_ALERT, {
logs.logBotAlert({
body: `Cannot add roles in Automod rule **${ruleName}**. ${missingPermissionError(missingPermissions)}`,
});
return;
@ -45,7 +45,7 @@ export const RemoveRolesAction = automodAction({
roleId => pluginData.guild.roles.cache.get(roleId as Snowflake)?.name || roleId,
);
const logs = pluginData.getPlugin(LogsPlugin);
logs.log(LogType.BOT_ALERT, {
logs.logBotAlert({
body: `Unable to remove the following roles in Automod rule **${ruleName}**: **${roleNamesWeCannotRemove.join(
"**, **",
)}**`,

View file

@ -1,6 +1,6 @@
import { MessageOptions, Permissions, Snowflake, TextChannel, User } from "discord.js";
import * as t from "io-ts";
import { userToConfigAccessibleUser } from "../../../utils/configAccessibleObjects";
import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { LogType } from "../../../data/LogType";
import { renderTemplate } from "../../../templateFormatter";
import {
@ -16,6 +16,7 @@ import {
import { hasDiscordPermissions } from "../../../utils/hasDiscordPermissions";
import { automodAction } from "../helpers";
import { AutomodContext } from "../types";
import { LogsPlugin } from "../../Logs/LogsPlugin";
export const ReplyAction = automodAction({
configType: t.union([
@ -48,7 +49,7 @@ export const ReplyAction = automodAction({
const renderReplyText = async str =>
renderTemplate(str, {
user: userToConfigAccessibleUser(user),
user: userToTemplateSafeUser(user),
});
const formatted =
typeof actionConfig === "string"
@ -65,7 +66,7 @@ export const ReplyAction = automodAction({
Permissions.FLAGS.SEND_MESSAGES | Permissions.FLAGS.VIEW_CHANNEL,
)
) {
pluginData.state.logs.log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Missing permissions to reply in ${verboseChannelMention(channel)} in Automod rule \`${ruleName}\``,
});
continue;
@ -76,7 +77,7 @@ export const ReplyAction = automodAction({
typeof formatted !== "string" &&
!hasDiscordPermissions(channel.permissionsFor(pluginData.client.user!.id), Permissions.FLAGS.EMBED_LINKS)
) {
pluginData.state.logs.log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Missing permissions to reply **with an embed** in ${verboseChannelMention(
channel,
)} in Automod rule \`${ruleName}\``,

View file

@ -2,6 +2,7 @@ import * as t from "io-ts";
import { LogType } from "../../../data/LogType";
import { CountersPlugin } from "../../Counters/CountersPlugin";
import { automodAction } from "../helpers";
import { LogsPlugin } from "../../Logs/LogsPlugin";
export const SetCounterAction = automodAction({
configType: t.type({
@ -14,7 +15,7 @@ export const SetCounterAction = automodAction({
async apply({ pluginData, contexts, actionConfig, matchResult, ruleName }) {
const countersPlugin = pluginData.getPlugin(CountersPlugin);
if (!countersPlugin.counterExists(actionConfig.counter)) {
pluginData.state.logs.log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Unknown counter \`${actionConfig.counter}\` in \`add_to_counter\` action of Automod rule \`${ruleName}\``,
});
return;

View file

@ -4,6 +4,7 @@ import { ChannelTypeStrings } from "src/types";
import { LogType } from "../../../data/LogType";
import { convertDelayStringToMS, isDiscordAPIError, tDelayString, tNullable } from "../../../utils";
import { automodAction } from "../helpers";
import { LogsPlugin } from "../../Logs/LogsPlugin";
export const SetSlowmodeAction = automodAction({
configType: t.type({
@ -53,7 +54,7 @@ export const SetSlowmodeAction = automodAction({
? `Duration is greater than maximum native slowmode duration`
: e.message;
pluginData.state.logs.log(LogType.BOT_ALERT, {
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Unable to set slowmode for channel ${channel.id} to ${slowmodeSeconds} seconds: ${errorMessage}`,
});
}