auto_reactions: check permissions ahead-of-time
This commit is contained in:
parent
d276d8e28a
commit
a404c7a97f
5 changed files with 36 additions and 17 deletions
|
@ -3,40 +3,51 @@ import { isDiscordRESTError } from "src/utils";
|
||||||
import { LogType } from "src/data/LogType";
|
import { LogType } from "src/data/LogType";
|
||||||
import { logger } from "../../../logger";
|
import { logger } from "../../../logger";
|
||||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||||
|
import { Constants, GuildChannel } from "eris";
|
||||||
|
import { memberHasChannelPermissions } from "../../../utils/memberHasChannelPermissions";
|
||||||
|
|
||||||
|
const p = Constants.Permissions;
|
||||||
|
|
||||||
export const AddReactionsEvt = autoReactionsEvt({
|
export const AddReactionsEvt = autoReactionsEvt({
|
||||||
event: "messageCreate",
|
event: "messageCreate",
|
||||||
allowBots: true,
|
allowBots: true,
|
||||||
allowSelf: true,
|
allowSelf: true,
|
||||||
|
|
||||||
async listener(meta) {
|
async listener({ pluginData, args: { message } }) {
|
||||||
const pluginData = meta.pluginData;
|
const autoReaction = await pluginData.state.autoReactions.getForChannel(message.channel.id);
|
||||||
const msg = meta.args.message;
|
|
||||||
|
|
||||||
const autoReaction = await pluginData.state.autoReactions.getForChannel(msg.channel.id);
|
|
||||||
if (!autoReaction) return;
|
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) {
|
for (const reaction of autoReaction.reactions) {
|
||||||
try {
|
try {
|
||||||
await msg.addReaction(reaction);
|
await message.addReaction(reaction);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isDiscordRESTError(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);
|
const logs = pluginData.getPlugin(LogsPlugin);
|
||||||
if (e.code === 10008) {
|
if (e.code === 10008) {
|
||||||
logs.log(LogType.BOT_ALERT, {
|
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 {
|
} else {
|
||||||
logs.log(LogType.BOT_ALERT, {
|
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 {
|
} else {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { errorMessage } from "../../../utils";
|
||||||
import { getBaseUrl, sendErrorMessage } from "../../../pluginUtils";
|
import { getBaseUrl, sendErrorMessage } from "../../../pluginUtils";
|
||||||
import moment from "moment-timezone";
|
import moment from "moment-timezone";
|
||||||
import { Constants, TextChannel } from "eris";
|
import { Constants, TextChannel } from "eris";
|
||||||
import { hasPermissions } from "../../../utils/hasPermissions";
|
import { hasChannelPermissions } from "../../../utils/hasChannelPermissions";
|
||||||
import { canReadChannel } from "../../../utils/canReadChannel";
|
import { canReadChannel } from "../../../utils/canReadChannel";
|
||||||
|
|
||||||
export const SourceCmd = utilityCmd({
|
export const SourceCmd = utilityCmd({
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { Constants, GuildChannel } from "eris";
|
import { Constants, GuildChannel } from "eris";
|
||||||
import { hasPermissions } from "./hasPermissions";
|
import { hasChannelPermissions } from "./hasChannelPermissions";
|
||||||
|
|
||||||
export function canReadChannel(channel: GuildChannel, memberId: string) {
|
export function canReadChannel(channel: GuildChannel, memberId: string) {
|
||||||
const channelPermissions = channel.permissionsOf(memberId);
|
const channelPermissions = channel.permissionsOf(memberId);
|
||||||
return hasPermissions(channelPermissions, [
|
return hasChannelPermissions(channelPermissions, [
|
||||||
Constants.Permissions.readMessages,
|
Constants.Permissions.readMessages,
|
||||||
Constants.Permissions.readMessageHistory,
|
Constants.Permissions.readMessageHistory,
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Constants, Permission } from "eris";
|
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)) {
|
if (Boolean(channelPermissions.allow & Constants.Permissions.administrator)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
8
backend/src/utils/memberHasChannelPermissions.ts
Normal file
8
backend/src/utils/memberHasChannelPermissions.ts
Normal 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);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue