auto_reactions: check permissions ahead-of-time

This commit is contained in:
Dragory 2020-08-07 00:09:05 +03:00
parent d276d8e28a
commit a404c7a97f
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
5 changed files with 36 additions and 17 deletions

View file

@ -3,40 +3,51 @@ import { isDiscordRESTError } from "src/utils";
import { LogType } from "src/data/LogType";
import { logger } from "../../../logger";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { Constants, GuildChannel } from "eris";
import { memberHasChannelPermissions } from "../../../utils/memberHasChannelPermissions";
const p = Constants.Permissions;
export const AddReactionsEvt = autoReactionsEvt({
event: "messageCreate",
allowBots: true,
allowSelf: true,
async listener(meta) {
const pluginData = meta.pluginData;
const msg = meta.args.message;
const autoReaction = await pluginData.state.autoReactions.getForChannel(msg.channel.id);
async listener({ pluginData, args: { message } }) {
const autoReaction = await pluginData.state.autoReactions.getForChannel(message.channel.id);
if (!autoReaction) return;
if (
!memberHasChannelPermissions(message.member, message.channel as GuildChannel, [
p.readMessages,
p.readMessageHistory,
p.addReactions,
])
) {
const logs = pluginData.getPlugin(LogsPlugin);
logs.log(LogType.BOT_ALERT, {
body: `Missing permissions to apply auto-reactions in <#${message.channel.id}>. Ensure I can read messages, read message history, and add reactions.`,
});
return;
}
for (const reaction of autoReaction.reactions) {
try {
await msg.addReaction(reaction);
await message.addReaction(reaction);
} catch (e) {
if (isDiscordRESTError(e)) {
logger.warn(
`Could not apply auto-reaction to ${msg.channel.id}/${msg.id} in guild ${pluginData.guild.name} (${pluginData.guild.id}) (error code ${e.code})`,
);
const logs = pluginData.getPlugin(LogsPlugin);
if (e.code === 10008) {
logs.log(LogType.BOT_ALERT, {
body: `Could not apply auto-reactions in <#${msg.channel.id}> for message \`${msg.id}\`. Make sure nothing is deleting the message before the reactions are applied.`,
body: `Could not apply auto-reactions in <#${message.channel.id}> for message \`${message.id}\`. Make sure nothing is deleting the message before the reactions are applied.`,
});
} else {
logs.log(LogType.BOT_ALERT, {
body: `Could not apply auto-reactions in <#${msg.channel.id}> for message \`${msg.id}\`. Error code ${e.code}.`,
body: `Could not apply auto-reactions in <#${message.channel.id}> for message \`${message.id}\`. Error code ${e.code}.`,
});
}
return;
break;
} else {
throw e;
}

View file

@ -4,7 +4,7 @@ import { errorMessage } from "../../../utils";
import { getBaseUrl, sendErrorMessage } from "../../../pluginUtils";
import moment from "moment-timezone";
import { Constants, TextChannel } from "eris";
import { hasPermissions } from "../../../utils/hasPermissions";
import { hasChannelPermissions } from "../../../utils/hasChannelPermissions";
import { canReadChannel } from "../../../utils/canReadChannel";
export const SourceCmd = utilityCmd({

View file

@ -1,9 +1,9 @@
import { Constants, GuildChannel } from "eris";
import { hasPermissions } from "./hasPermissions";
import { hasChannelPermissions } from "./hasChannelPermissions";
export function canReadChannel(channel: GuildChannel, memberId: string) {
const channelPermissions = channel.permissionsOf(memberId);
return hasPermissions(channelPermissions, [
return hasChannelPermissions(channelPermissions, [
Constants.Permissions.readMessages,
Constants.Permissions.readMessageHistory,
]);

View file

@ -1,6 +1,6 @@
import { Constants, Permission } from "eris";
export function hasPermissions(channelPermissions: Permission, permissions: number | number[]) {
export function hasChannelPermissions(channelPermissions: Permission, permissions: number | number[]) {
if (Boolean(channelPermissions.allow & Constants.Permissions.administrator)) {
return true;
}

View file

@ -0,0 +1,8 @@
import { Constants, GuildChannel, Member, Permission } from "eris";
import { PluginData } from "knub";
import { hasChannelPermissions } from "./hasChannelPermissions";
export function memberHasChannelPermissions(member: Member, channel: GuildChannel, permissions: number | number[]) {
const memberChannelPermissions = channel.permissionsOf(member.id);
return hasChannelPermissions(memberChannelPermissions, permissions);
}