3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

AutoReactions: send a bot alert if the bot is missing permissions to apply auto reactions (triggered by unknown messages, so usually 'read message history')

This commit is contained in:
Dragory 2020-01-12 17:05:30 +02:00
parent ca5c7fed64
commit 9b9b6a1518

View file

@ -1,11 +1,14 @@
import { decorators as d, IBasePluginConfig, IPluginOptions } from "knub";
import { decorators as d, IPluginOptions, logger } from "knub";
import { GuildSavedMessages } from "../data/GuildSavedMessages";
import { SavedMessage } from "../data/entities/SavedMessage";
import { GuildAutoReactions } from "../data/GuildAutoReactions";
import { Message } from "eris";
import { customEmojiRegex, errorMessage, isEmoji, successMessage } from "../utils";
import { customEmojiRegex, errorMessage, isEmoji } from "../utils";
import { CommandInfo, trimPluginDescription, ZeppelinPlugin } from "./ZeppelinPlugin";
import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disable-line
import * as t from "io-ts";
import { GuildLogs } from "../data/GuildLogs";
import { LogType } from "../data/LogType";
const ConfigSchema = t.type({
can_manage: t.boolean,
@ -25,6 +28,7 @@ export class AutoReactionsPlugin extends ZeppelinPlugin<TConfigSchema> {
protected savedMessages: GuildSavedMessages;
protected autoReactions: GuildAutoReactions;
protected logs: GuildLogs;
private onMessageCreateFn;
@ -46,6 +50,7 @@ export class AutoReactionsPlugin extends ZeppelinPlugin<TConfigSchema> {
}
onLoad() {
this.logs = new GuildLogs(this.guildId);
this.savedMessages = GuildSavedMessages.getGuildInstance(this.guildId);
this.autoReactions = GuildAutoReactions.getGuildInstance(this.guildId);
@ -119,8 +124,23 @@ export class AutoReactionsPlugin extends ZeppelinPlugin<TConfigSchema> {
const autoReaction = await this.autoReactions.getForChannel(msg.channel_id);
if (!autoReaction) return;
const realMsg = await this.bot.getMessage(msg.channel_id, msg.id);
if (!realMsg) return;
let realMsg;
try {
realMsg = await this.bot.getMessage(msg.channel_id, msg.id);
} catch (e) {
if (e instanceof DiscordRESTError && e.code === 10008) {
// Unknown message, post warning in logs
logger.warn(
`Could not apply auto-reactions to ${msg.channel_id}/${msg.id} in guild ${this.guild.name} (${this.guildId}) (error code 10008)`,
);
this.logs.log(LogType.BOT_ALERT, {
body: `Could not apply auto-reactions in <#${msg.channel_id}> for message \`${msg.id}\`. Make sure the bot has **Read Message History** permissions on the channel.`,
});
return;
} else {
throw e;
}
}
for (const reaction of autoReaction.reactions) {
realMsg.addReaction(reaction);