feat: add editing support to InternalPoster

This commit is contained in:
Dragory 2021-11-02 23:10:37 +02:00
parent ecd9a5863c
commit 31f18ba27f
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
6 changed files with 113 additions and 39 deletions

View file

@ -0,0 +1,46 @@
import { Message, MessageOptions, NewsChannel, TextChannel, WebhookClient } from "discord.js";
import { GuildPluginData } from "knub";
import { InternalPosterPluginType } from "../types";
import { isDiscordAPIError, noop } from "../../../utils";
/**
* Sends a message using a webhook or direct API requests, preferring webhooks when possible.
*/
export async function editMessage(
pluginData: GuildPluginData<InternalPosterPluginType>,
message: Message,
content: MessageOptions,
): Promise<void> {
if (!(message.channel instanceof TextChannel || message.channel instanceof NewsChannel)) {
return;
}
const channel = message.channel as TextChannel | NewsChannel;
await pluginData.state.queue.add(async () => {
if (message.webhookId) {
const webhook = await pluginData.state.webhooks.find(message.webhookId);
if (!webhook) {
// Webhook message but we're missing the token -> can't edit
return;
}
const webhookClient = new WebhookClient({
id: webhook.id,
token: webhook.token,
});
await webhookClient.editMessage(message.id, content).catch(async (err) => {
// Unknown Webhook, remove from DB
if (isDiscordAPIError(err) && err.code === 10015) {
await pluginData.state.webhooks.delete(webhookClient.id);
return;
}
throw err;
});
return;
}
await message.edit(content).catch(noop);
});
}