From 2ddba07bd24f4e7a0919f4fb555a1f70f31db67c Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 16 Jul 2024 16:51:00 +1200 Subject: [PATCH] Recover from webhooks being deleted --- m2d/actions/channel-webhook.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/m2d/actions/channel-webhook.js b/m2d/actions/channel-webhook.js index d52f174..977e186 100644 --- a/m2d/actions/channel-webhook.js +++ b/m2d/actions/channel-webhook.js @@ -1,6 +1,7 @@ // @ts-check const assert = require("assert").strict +const {DiscordAPIError} = require("snowtransfer") const DiscordTypes = require("discord-api-types/v10") const {Readable} = require("stream") const passthrough = require("../../passthrough") @@ -44,8 +45,13 @@ async function ensureWebhook(channelID, forceCreate = false) { */ async function withWebhook(channelID, callback) { const webhook = await ensureWebhook(channelID, false) - return callback(webhook).catch(e => { - // TODO: check if the error was webhook-related and if webhook.created === false, then: const webhook = ensureWebhook(channelID, true); return callback(webhook) + return callback(webhook).catch(async e => { + if (e.message === `{"message": "Unknown Webhook", "code": 10015}`) { // pathetic error handling from SnowTransfer + // Our webhook is gone. Maybe somebody deleted it, or removed and re-added OOYE from the guild. + const newWebhook = await ensureWebhook(channelID, true) + return callback(newWebhook) // not caught; if the error happens again just throw it instead of looping + } + throw e }) }