feat: add editing support to InternalPoster
This commit is contained in:
parent
ecd9a5863c
commit
31f18ba27f
6 changed files with 113 additions and 39 deletions
46
backend/src/plugins/InternalPoster/functions/editMessage.ts
Normal file
46
backend/src/plugins/InternalPoster/functions/editMessage.ts
Normal 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);
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue