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

Ugly workaround/hotfix for message fetching

This commit is contained in:
Dragory 2021-08-19 00:49:06 +03:00
parent 499511f79d
commit 8da47e53e6
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
9 changed files with 31 additions and 14 deletions

View file

@ -28,11 +28,11 @@ export class Queue<TQueueFunction extends AnyFn = AnyFn> {
return this.queue.length + (this.running ? 1 : 0);
}
public add(fn: TQueueFunction): Promise<void> {
const promise = new Promise<void>(resolve => {
public add(fn: TQueueFunction): Promise<any> {
const promise = new Promise<any>(resolve => {
this.queue.push(async () => {
await fn();
resolve();
const result = await fn();
resolve(result);
});
if (!this.running) this.next();

View file

@ -1,6 +1,7 @@
import { Message, Snowflake, TextChannel, ThreadChannel } from "discord.js";
import { GuildPluginData } from "knub";
import { MessageSaverPluginType } from "./types";
import { hotfixMessageFetch } from "../../utils/hotfixMessageFetch";
export async function saveMessagesToDB(
pluginData: GuildPluginData<MessageSaverPluginType>,
@ -15,7 +16,7 @@ export async function saveMessagesToDB(
let thisMsg: Message;
try {
thisMsg = await channel.messages.fetch(id as Snowflake);
thisMsg = await hotfixMessageFetch(channel, id as Snowflake);
if (!thisMsg) {
failed.push(id);

View file

@ -3,6 +3,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
import { isDiscordAPIError } from "../../../utils";
import { reactionRolesCmd } from "../types";
import { hotfixMessageFetch } from "../../../utils/hotfixMessageFetch";
export const ClearReactionRolesCmd = reactionRolesCmd({
trigger: "reaction_roles clear",
@ -23,7 +24,7 @@ export const ClearReactionRolesCmd = reactionRolesCmd({
let targetMessage: Message;
try {
targetMessage = await args.message.channel.messages.fetch(args.message.messageId as Snowflake);
targetMessage = await hotfixMessageFetch(args.message.channel, args.message.messageId);
} catch (err) {
if (isDiscordAPIError(err) && err.code === 50001) {
sendErrorMessage(pluginData, msg.channel, "Missing access to the specified message");

View file

@ -5,6 +5,7 @@ import { canUseEmoji, isDiscordAPIError, isValidEmoji, noop, trimPluginDescripti
import { canReadChannel } from "../../../utils/canReadChannel";
import { reactionRolesCmd, TReactionRolePair } from "../types";
import { applyReactionRoleReactionsToMessage } from "../util/applyReactionRoleReactionsToMessage";
import { hotfixMessageFetch } from "../../../utils/hotfixMessageFetch";
const CLEAR_ROLES_EMOJI = "❌";
@ -40,7 +41,7 @@ export const InitReactionRolesCmd = reactionRolesCmd({
let targetMessage;
try {
targetMessage = await args.message.channel.messages.fetch(args.message.messageId as Snowflake).catch(noop);
targetMessage = await hotfixMessageFetch(args.message.channel, args.message.messageId);
} catch (e) {
if (isDiscordAPIError(e)) {
sendErrorMessage(pluginData, msg.channel, `Error ${e.code} while getting message: ${e.message}`);

View file

@ -5,6 +5,7 @@ import { LogType } from "../../../data/LogType";
import { isDiscordAPIError, sleep } from "../../../utils";
import { LogsPlugin } from "../../Logs/LogsPlugin";
import { ReactionRolesPluginType } from "../types";
import { hotfixMessageFetch } from "../../../utils/hotfixMessageFetch";
const CLEAR_ROLES_EMOJI = "❌";
@ -25,7 +26,7 @@ export async function applyReactionRoleReactionsToMessage(
let targetMessage;
try {
targetMessage = await channel.messages.fetch(messageId as Snowflake);
targetMessage = await hotfixMessageFetch(channel, messageId as Snowflake);
} catch (e) {
if (isDiscordAPIError(e)) {
if (e.code === 10008) {

View file

@ -11,6 +11,7 @@ import { LogsPlugin } from "../../Logs/LogsPlugin";
import { BOT_SLOWMODE_PERMISSIONS } from "../requiredPermissions";
import { SlowmodePluginType } from "../types";
import { applyBotSlowmodeToUserId } from "./applyBotSlowmodeToUserId";
import { hotfixMessageFetch } from "../../../utils/hotfixMessageFetch";
export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePluginType>, msg: SavedMessage) {
if (msg.is_bot) return;
@ -49,7 +50,7 @@ export async function onMessageCreate(pluginData: GuildPluginData<SlowmodePlugin
// Delete any extra messages sent after a slowmode was already applied
const userHasSlowmode = await pluginData.state.slowmodes.userHasSlowmode(channel.id, msg.user_id);
if (userHasSlowmode) {
const message = await channel.messages.fetch(msg.id as Snowflake);
const message = await hotfixMessageFetch(channel, msg.id);
if (message) {
message.delete();
return thisMsgLock.interrupt();

View file

@ -4,6 +4,7 @@ import { allStarboardsLock } from "../../../utils/lockNameHelpers";
import { starboardEvt } from "../types";
import { saveMessageToStarboard } from "../util/saveMessageToStarboard";
import { updateStarboardMessageStarCount } from "../util/updateStarboardMessageStarCount";
import { hotfixMessageFetch } from "../../../utils/hotfixMessageFetch";
export const StarboardReactionAddEvt = starboardEvt({
event: "messageReactionAdd",
@ -18,7 +19,7 @@ export const StarboardReactionAddEvt = starboardEvt({
if (!msg.author) {
// Message is not cached, fetch it
try {
msg = await msg.channel.messages.fetch(msg.id);
msg = await hotfixMessageFetch(msg.channel as TextChannel, msg.id);
} catch {
// Sometimes we get this event for messages we can't fetch with getMessage; ignore silently
return;
@ -79,9 +80,7 @@ export const StarboardReactionAddEvt = starboardEvt({
const channel = pluginData.guild.channels.cache.get(
starboardMessage.starboard_channel_id as Snowflake,
) as TextChannel;
const realStarboardMessage = await channel.messages.fetch(
starboardMessage.starboard_message_id as Snowflake,
);
const realStarboardMessage = await hotfixMessageFetch(channel, starboardMessage.starboard_message_id);
await updateStarboardMessageStarCount(
starboard,
msg,

View file

@ -4,6 +4,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes";
import { getBaseUrl, sendErrorMessage } from "../../../pluginUtils";
import { canReadChannel } from "../../../utils/canReadChannel";
import { utilityCmd } from "../types";
import { hotfixMessageFetch } from "../../../utils/hotfixMessageFetch";
export const SourceCmd = utilityCmd({
trigger: "source",
@ -21,7 +22,7 @@ export const SourceCmd = utilityCmd({
return;
}
const message = await args.message.channel.messages.fetch(args.message.messageId as Snowflake).catch(() => null);
const message = await hotfixMessageFetch(args.message.channel, args.message.messageId);
if (!message) {
sendErrorMessage(pluginData, cmdMessage.channel, "Unknown message");
return;

View file

@ -0,0 +1,12 @@
import { GuildChannel, Message, TextChannel, ThreadChannel } from "discord.js";
import { Queue } from "../Queue";
import { sleep } from "../utils";
const queue = new Queue();
export function hotfixMessageFetch(channel: TextChannel | ThreadChannel, messageId: string): Promise<Message> {
return queue.add(async () => {
await sleep(3000);
return channel.messages.fetch(messageId);
});
}