2020-07-28 21:34:01 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { automodAction } from "../helpers";
|
|
|
|
import { LogType } from "../../../data/LogType";
|
2020-09-15 01:57:46 +03:00
|
|
|
import {
|
|
|
|
asyncMap,
|
|
|
|
createChunkedMessage,
|
|
|
|
isDiscordRESTError,
|
|
|
|
messageLink,
|
|
|
|
resolveMember,
|
|
|
|
stripObjectToScalars,
|
|
|
|
tNullable,
|
2021-04-04 22:30:21 +03:00
|
|
|
verboseChannelMention,
|
2020-09-15 01:57:46 +03:00
|
|
|
} from "../../../utils";
|
2020-07-28 21:34:01 +03:00
|
|
|
import { resolveActionContactMethods } from "../functions/resolveActionContactMethods";
|
|
|
|
import { ModActionsPlugin } from "../../ModActions/ModActionsPlugin";
|
|
|
|
import { TextChannel } from "eris";
|
2020-12-17 03:44:42 +02:00
|
|
|
import { renderTemplate, TemplateParseError } from "../../../templateFormatter";
|
2020-07-28 21:51:58 +03:00
|
|
|
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
export const AlertAction = automodAction({
|
|
|
|
configType: t.type({
|
|
|
|
channel: t.string,
|
|
|
|
text: t.string,
|
|
|
|
}),
|
|
|
|
|
2020-07-30 01:45:14 +03:00
|
|
|
defaultConfig: {},
|
|
|
|
|
2020-07-28 21:34:01 +03:00
|
|
|
async apply({ pluginData, contexts, actionConfig, ruleName, matchResult }) {
|
|
|
|
const channel = pluginData.guild.channels.get(actionConfig.channel);
|
2020-07-28 21:51:58 +03:00
|
|
|
const logs = pluginData.getPlugin(LogsPlugin);
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
if (channel && channel instanceof TextChannel) {
|
|
|
|
const text = actionConfig.text;
|
|
|
|
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 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-02-15 00:25:20 +02:00
|
|
|
const logMessage = await logs.getLogMessage(LogType.AUTOMOD_ACTION, {
|
2020-07-28 21:51:58 +03:00
|
|
|
rule: ruleName,
|
|
|
|
user: safeUser,
|
|
|
|
users: safeUsers,
|
|
|
|
actionsTaken,
|
|
|
|
matchSummary: matchResult.summary,
|
|
|
|
});
|
2020-07-28 21:34:01 +03:00
|
|
|
|
2020-12-17 03:44:42 +02:00
|
|
|
let rendered;
|
|
|
|
try {
|
|
|
|
rendered = await renderTemplate(actionConfig.text, {
|
|
|
|
rule: ruleName,
|
|
|
|
user: safeUser,
|
|
|
|
users: safeUsers,
|
|
|
|
text,
|
|
|
|
actionsTaken,
|
|
|
|
matchSummary: matchResult.summary,
|
|
|
|
messageLink: theMessageLink,
|
|
|
|
logMessage,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TemplateParseError) {
|
|
|
|
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
|
|
|
|
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 {
|
|
|
|
await createChunkedMessage(channel, rendered);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 50001) {
|
|
|
|
logs.log(LogType.BOT_ALERT, {
|
|
|
|
body: `Missing access to send alert to channel ${verboseChannelMention(
|
|
|
|
channel,
|
|
|
|
)} in automod rule **${ruleName}**`,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logs.log(LogType.BOT_ALERT, {
|
|
|
|
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 {
|
2020-07-28 21:51:58 +03:00
|
|
|
logs.log(LogType.BOT_ALERT, {
|
|
|
|
body: `Invalid channel id \`${actionConfig.channel}\` for alert action in automod rule **${ruleName}**`,
|
|
|
|
});
|
2020-07-28 21:34:01 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|