2021-08-18 20:47:24 +03:00
|
|
|
import { Snowflake, TextChannel, ThreadChannel } from "discord.js";
|
2020-07-28 21:34:01 +03:00
|
|
|
import * as t from "io-ts";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { erisAllowedMentionsToDjsMentionOptions } from "src/utils/erisAllowedMentionsToDjsMentionOptions";
|
2020-07-28 21:34:01 +03:00
|
|
|
import { LogType } from "../../../data/LogType";
|
2021-08-18 01:51:42 +03:00
|
|
|
import {
|
|
|
|
createTypedTemplateSafeValueContainer,
|
|
|
|
renderTemplate,
|
|
|
|
TemplateParseError,
|
|
|
|
TemplateSafeValueContainer,
|
|
|
|
} from "../../../templateFormatter";
|
2020-09-15 01:57:46 +03:00
|
|
|
import {
|
2021-06-08 02:23:30 +02:00
|
|
|
createChunkedMessage,
|
|
|
|
messageLink,
|
|
|
|
stripObjectToScalars,
|
|
|
|
tAllowedMentions,
|
|
|
|
tNormalizedNullOptional,
|
2021-08-18 01:51:42 +03:00
|
|
|
isTruthy,
|
2021-06-08 02:23:30 +02:00
|
|
|
verboseChannelMention,
|
2020-09-15 01:57:46 +03:00
|
|
|
} from "../../../utils";
|
2020-07-28 21:51:58 +03:00
|
|
|
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { automodAction } from "../helpers";
|
2021-08-18 01:51:42 +03:00
|
|
|
import { TemplateSafeUser, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
export const AlertAction = automodAction({
|
|
|
|
configType: t.type({
|
|
|
|
channel: t.string,
|
|
|
|
text: t.string,
|
2021-04-29 00:50:25 +03:00
|
|
|
allowed_mentions: tNormalizedNullOptional(tAllowedMentions),
|
2020-07-28 21:34:01 +03:00
|
|
|
}),
|
|
|
|
|
2020-07-30 01:45:14 +03:00
|
|
|
defaultConfig: {},
|
|
|
|
|
2020-07-28 21:34:01 +03:00
|
|
|
async apply({ pluginData, contexts, actionConfig, ruleName, matchResult }) {
|
2021-06-30 04:56:56 +02:00
|
|
|
const channel = pluginData.guild.channels.cache.get(actionConfig.channel as Snowflake);
|
2020-07-28 21:51:58 +03:00
|
|
|
const logs = pluginData.getPlugin(LogsPlugin);
|
2020-07-28 21:34:01 +03:00
|
|
|
|
2021-08-18 20:47:24 +03:00
|
|
|
if (channel && (channel instanceof TextChannel || channel instanceof ThreadChannel)) {
|
2020-07-28 21:34:01 +03:00
|
|
|
const text = actionConfig.text;
|
|
|
|
const theMessageLink =
|
|
|
|
contexts[0].message && messageLink(pluginData.guild.id, contexts[0].message.channel_id, contexts[0].message.id);
|
|
|
|
|
2021-08-18 01:51:42 +03:00
|
|
|
const safeUsers = contexts.map(c => (c.user ? userToTemplateSafeUser(c.user) : null)).filter(isTruthy);
|
2020-07-28 21:34:01 +03:00
|
|
|
const safeUser = safeUsers[0];
|
2020-07-29 22:43:33 +03:00
|
|
|
const actionsTaken = Object.keys(pluginData.config.get().rules[ruleName].actions).join(", ");
|
2020-07-28 21:34:01 +03:00
|
|
|
|
2021-08-18 01:51:42 +03:00
|
|
|
const logMessage = await logs.getLogMessage(
|
|
|
|
LogType.AUTOMOD_ACTION,
|
|
|
|
createTypedTemplateSafeValueContainer({
|
2020-12-17 03:44:42 +02:00
|
|
|
rule: ruleName,
|
|
|
|
user: safeUser,
|
|
|
|
users: safeUsers,
|
|
|
|
actionsTaken,
|
2021-08-18 01:51:42 +03:00
|
|
|
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,
|
|
|
|
}),
|
|
|
|
);
|
2020-12-17 03:44:42 +02:00
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TemplateParseError) {
|
2021-08-18 01:51:42 +03:00
|
|
|
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
2020-12-17 03:44:42 +02:00
|
|
|
body: `Error in alert format of automod rule ${ruleName}: ${err.message}`,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
2020-09-15 01:57:46 +03:00
|
|
|
|
2021-04-04 22:30:21 +03:00
|
|
|
try {
|
2021-06-01 02:05:55 +02:00
|
|
|
await createChunkedMessage(
|
|
|
|
channel,
|
|
|
|
rendered,
|
|
|
|
erisAllowedMentionsToDjsMentionOptions(actionConfig.allowed_mentions),
|
|
|
|
);
|
2021-04-04 22:30:21 +03:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 50001) {
|
2021-08-18 01:51:42 +03:00
|
|
|
logs.logBotAlert({
|
2021-04-04 22:30:21 +03:00
|
|
|
body: `Missing access to send alert to channel ${verboseChannelMention(
|
|
|
|
channel,
|
|
|
|
)} in automod rule **${ruleName}**`,
|
|
|
|
});
|
|
|
|
} else {
|
2021-08-18 01:51:42 +03:00
|
|
|
logs.logBotAlert({
|
2021-04-04 22:30:21 +03:00
|
|
|
body: `Error ${err.code || "UNKNOWN"} when sending alert to channel ${verboseChannelMention(
|
|
|
|
channel,
|
|
|
|
)} in automod rule **${ruleName}**`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 21:34:01 +03:00
|
|
|
} else {
|
2021-08-18 01:51:42 +03:00
|
|
|
logs.logBotAlert({
|
2020-07-28 21:51:58 +03:00
|
|
|
body: `Invalid channel id \`${actionConfig.channel}\` for alert action in automod rule **${ruleName}**`,
|
|
|
|
});
|
2020-07-28 21:34:01 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|