2021-06-30 04:56:56 +02:00
|
|
|
import { MessageOptions, Permissions, Snowflake, TextChannel, User } from "discord.js";
|
2020-07-28 21:34:01 +03:00
|
|
|
import * as t from "io-ts";
|
2021-07-21 22:14:09 +02:00
|
|
|
import { userToConfigAccessibleUser } from "src/utils/configAccessibleObjects";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { LogType } from "../../../data/LogType";
|
|
|
|
import { renderTemplate } from "../../../templateFormatter";
|
2020-07-28 21:34:01 +03:00
|
|
|
import {
|
2021-06-08 02:23:30 +02:00
|
|
|
convertDelayStringToMS,
|
|
|
|
noop,
|
|
|
|
renderRecursively,
|
|
|
|
tDelayString,
|
|
|
|
tMessageContent,
|
|
|
|
tNullable,
|
|
|
|
unique,
|
|
|
|
verboseChannelMention,
|
2021-06-06 23:51:32 +02:00
|
|
|
} from "../../../utils";
|
2021-04-11 14:13:25 +03:00
|
|
|
import { hasDiscordPermissions } from "../../../utils/hasDiscordPermissions";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { automodAction } from "../helpers";
|
|
|
|
import { AutomodContext } from "../types";
|
|
|
|
|
2020-07-28 21:34:01 +03:00
|
|
|
export const ReplyAction = automodAction({
|
|
|
|
configType: t.union([
|
|
|
|
t.string,
|
|
|
|
t.type({
|
|
|
|
text: tMessageContent,
|
|
|
|
auto_delete: tNullable(t.union([tDelayString, t.number])),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
|
2020-07-30 01:45:14 +03:00
|
|
|
defaultConfig: {},
|
|
|
|
|
2021-04-11 14:13:25 +03:00
|
|
|
async apply({ pluginData, contexts, actionConfig, ruleName }) {
|
2020-07-28 21:34:01 +03:00
|
|
|
const contextsWithTextChannels = contexts
|
|
|
|
.filter(c => c.message?.channel_id)
|
2021-06-30 04:56:56 +02:00
|
|
|
.filter(c => pluginData.guild.channels.cache.get(c.message!.channel_id as Snowflake) instanceof TextChannel);
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
const contextsByChannelId = contextsWithTextChannels.reduce((map: Map<string, AutomodContext[]>, context) => {
|
2020-11-09 20:03:57 +02:00
|
|
|
if (!map.has(context.message!.channel_id)) {
|
|
|
|
map.set(context.message!.channel_id, []);
|
2020-07-28 21:34:01 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
map.get(context.message!.channel_id)!.push(context);
|
2020-07-28 21:34:01 +03:00
|
|
|
return map;
|
|
|
|
}, new Map());
|
|
|
|
|
|
|
|
for (const [channelId, _contexts] of contextsByChannelId.entries()) {
|
2021-04-11 14:13:25 +03:00
|
|
|
const users = unique(Array.from(new Set(_contexts.map(c => c.user).filter(Boolean)))) as User[];
|
2020-07-28 21:34:01 +03:00
|
|
|
const user = users[0];
|
|
|
|
|
|
|
|
const renderReplyText = async str =>
|
|
|
|
renderTemplate(str, {
|
2021-07-21 22:14:09 +02:00
|
|
|
user: userToConfigAccessibleUser(user),
|
2020-07-28 21:34:01 +03:00
|
|
|
});
|
|
|
|
const formatted =
|
|
|
|
typeof actionConfig === "string"
|
|
|
|
? await renderReplyText(actionConfig)
|
2021-06-01 02:05:55 +02:00
|
|
|
: ((await renderRecursively(actionConfig.text, renderReplyText)) as MessageOptions);
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
if (formatted) {
|
2021-06-30 04:56:56 +02:00
|
|
|
const channel = pluginData.guild.channels.cache.get(channelId as Snowflake) as TextChannel;
|
2021-04-11 14:13:25 +03:00
|
|
|
|
|
|
|
// Check for basic Send Messages and View Channel permissions
|
|
|
|
if (
|
|
|
|
!hasDiscordPermissions(
|
2021-06-01 02:05:55 +02:00
|
|
|
channel.permissionsFor(pluginData.client.user!.id),
|
|
|
|
Permissions.FLAGS.SEND_MESSAGES | Permissions.FLAGS.VIEW_CHANNEL,
|
2021-04-11 14:13:25 +03:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
|
|
|
body: `Missing permissions to reply in ${verboseChannelMention(channel)} in Automod rule \`${ruleName}\``,
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the message is an embed, check for embed permissions
|
|
|
|
if (
|
|
|
|
typeof formatted !== "string" &&
|
2021-06-01 02:05:55 +02:00
|
|
|
!hasDiscordPermissions(channel.permissionsFor(pluginData.client.user!.id), Permissions.FLAGS.EMBED_LINKS)
|
2021-04-11 14:13:25 +03:00
|
|
|
) {
|
|
|
|
pluginData.state.logs.log(LogType.BOT_ALERT, {
|
|
|
|
body: `Missing permissions to reply **with an embed** in ${verboseChannelMention(
|
|
|
|
channel,
|
|
|
|
)} in Automod rule \`${ruleName}\``,
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-01 02:05:55 +02:00
|
|
|
const messageContent: MessageOptions = typeof formatted === "string" ? { content: formatted } : formatted;
|
|
|
|
const replyMsg = await channel.send({
|
2021-04-29 00:56:56 +03:00
|
|
|
...messageContent,
|
|
|
|
allowedMentions: {
|
|
|
|
users: [user.id],
|
|
|
|
},
|
|
|
|
});
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
if (typeof actionConfig === "object" && actionConfig.auto_delete) {
|
2020-11-09 20:03:57 +02:00
|
|
|
const delay = convertDelayStringToMS(String(actionConfig.auto_delete))!;
|
2020-07-28 21:34:01 +03:00
|
|
|
setTimeout(() => replyMsg.delete().catch(noop), delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|