Starboard: ignore error when fetching message in onMessageReactionAdd

This commit is contained in:
Dragory 2019-02-09 14:38:50 +02:00
parent 2e245ab16b
commit de05352f18

View file

@ -9,7 +9,7 @@ import {
getUrlsInString, getUrlsInString,
noop, noop,
snowflakeRegex, snowflakeRegex,
successMessage successMessage,
} from "../utils"; } from "../utils";
import { Starboard } from "../data/entities/Starboard"; import { Starboard } from "../data/entities/Starboard";
import path from "path"; import path from "path";
@ -28,17 +28,17 @@ export class StarboardPlugin extends ZeppelinPlugin {
getDefaultOptions() { getDefaultOptions() {
return { return {
permissions: { permissions: {
manage: false manage: false,
}, },
overrides: [ overrides: [
{ {
level: ">=100", level: ">=100",
permissions: { permissions: {
manage: true manage: true,
} },
} },
] ],
}; };
} }
@ -64,7 +64,7 @@ export class StarboardPlugin extends ZeppelinPlugin {
const cancelMsg = () => msg.channel.createMessage("Cancelled"); const cancelMsg = () => msg.channel.createMessage("Cancelled");
msg.channel.createMessage( msg.channel.createMessage(
`⭐ Let's make a starboard! What channel should we use as the board? ("cancel" to cancel)` `⭐ Let's make a starboard! What channel should we use as the board? ("cancel" to cancel)`,
); );
let starboardChannel; let starboardChannel;
@ -109,7 +109,7 @@ export class StarboardPlugin extends ZeppelinPlugin {
} while (emoji == null); } while (emoji == null);
msg.channel.createMessage( msg.channel.createMessage(
`And how many reactions are required to immortalize a message in the starboard? ("cancel" to cancel)` `And how many reactions are required to immortalize a message in the starboard? ("cancel" to cancel)`,
); );
let requiredReactions; let requiredReactions;
@ -136,7 +136,7 @@ export class StarboardPlugin extends ZeppelinPlugin {
} while (requiredReactions == null); } while (requiredReactions == null);
msg.channel.createMessage( msg.channel.createMessage(
`And finally, which channels can messages be starred in? "All" for any channel. ("cancel" to cancel)` `And finally, which channels can messages be starred in? "All" for any channel. ("cancel" to cancel)`,
); );
let channelWhitelist; let channelWhitelist;
@ -193,7 +193,12 @@ export class StarboardPlugin extends ZeppelinPlugin {
async onMessageReactionAdd(msg: Message, emoji: { id: string; name: string }) { async onMessageReactionAdd(msg: Message, emoji: { id: string; name: string }) {
if (!msg.author) { if (!msg.author) {
// Message is not cached, fetch it // Message is not cached, fetch it
msg = await msg.channel.getMessage(msg.id); try {
msg = await msg.channel.getMessage(msg.id);
} catch (e) {
// Sometimes we get this event for messages we can't fetch with getMessage; ignore silently
return;
}
} }
const emojiStr = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name; const emojiStr = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
@ -211,7 +216,7 @@ export class StarboardPlugin extends ZeppelinPlugin {
// If the message has already been posted to this starboard, we don't need to do anything else here // If the message has already been posted to this starboard, we don't need to do anything else here
const existingSavedMessage = await this.starboards.getStarboardMessageByStarboardIdAndMessageId( const existingSavedMessage = await this.starboards.getStarboardMessageByStarboardIdAndMessageId(
starboard.id, starboard.id,
msg.id msg.id,
); );
if (existingSavedMessage) return; if (existingSavedMessage) return;
@ -248,11 +253,11 @@ export class StarboardPlugin extends ZeppelinPlugin {
const embed: any = { const embed: any = {
footer: { footer: {
text: `#${(msg.channel as GuildChannel).name} - ${time}` text: `#${(msg.channel as GuildChannel).name} - ${time}`,
}, },
author: { author: {
name: `${msg.author.username}#${msg.author.discriminator}` name: `${msg.author.username}#${msg.author.discriminator}`,
} },
}; };
if (msg.author.avatarURL) { if (msg.author.avatarURL) {
@ -293,7 +298,7 @@ export class StarboardPlugin extends ZeppelinPlugin {
const starboardMessage = await (channel as TextChannel).createMessage({ const starboardMessage = await (channel as TextChannel).createMessage({
content: `https://discordapp.com/channels/${this.guildId}/${msg.channel.id}/${msg.id}`, content: `https://discordapp.com/channels/${this.guildId}/${msg.channel.id}/${msg.id}`,
embed embed,
}); });
await this.starboards.createStarboardMessage(starboard.id, msg.id, starboardMessage.id); await this.starboards.createStarboardMessage(starboard.id, msg.id, starboardMessage.id);
} }
@ -347,7 +352,7 @@ export class StarboardPlugin extends ZeppelinPlugin {
for (const pin of pins) { for (const pin of pins) {
const existingStarboardMessage = await this.starboards.getStarboardMessageByStarboardIdAndMessageId( const existingStarboardMessage = await this.starboards.getStarboardMessageByStarboardIdAndMessageId(
starboard.id, starboard.id,
pin.id pin.id,
); );
if (existingStarboardMessage) continue; if (existingStarboardMessage) continue;