From ed9ea57f12548e5889e77af579a7b14e5fab69f0 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Fri, 30 Jun 2023 15:15:34 +1200 Subject: [PATCH] add webhook creation utility --- db/ooye.db | Bin 131072 -> 143360 bytes m2d/actions/register-webhook.js | 50 ++++++++++++++++++++++++++++++++ m2d/actions/send-message.js | 23 +++++++++++++++ test/test.js | 1 + types.d.ts | 5 ++++ 5 files changed, 79 insertions(+) create mode 100644 m2d/actions/register-webhook.js create mode 100644 m2d/actions/send-message.js diff --git a/db/ooye.db b/db/ooye.db index 9c7d480b489cab00620de78d776874b511700f41..be5650515d1dd449ecdae32f120a2e170559af4d 100644 GIT binary patch delta 623 zcmZo@;AnWjF+p1J00RSqG7!Un)eHcVJJSS$WvT=Cw@$%l=ENJkJ zZE_F$UgJg!ZFX^KX~rh^lEkE()biA%jQsp;D5dBe( zV;|s<664~8NyV4sXQ$>VafY}?giP+{lHB}_ql$@-O`8quCUY$|ac|YhAGjqZpXcV? zyqMd7Nsv!mRh6-c8)UhzE}OWcG)P(;YI?j8l%cGv1CtYk$r(Z!N}Fx?P82e$F+82j z-r&dtq&ymCvo>J~W*eEkOyIaxI*u=uf%+$op%+kcr(%8_{SU<5Wu_UpmSl`$p zCB@j(GBMH6C@DF`)CkBkG&3a!fXxDJEHzmYVC4>sXRpP?Y84ni-JkT3Qv5QIMlHS#g$Fv(L=! zJ~J72m`QPS9%SIJ;QPRr!TW&Mn`asiA9owqKdv&)gTOdh%*obhqy}=hVlmj^R^&T; sa$=c+G%iOqvZ_vJyv^vo{lG0o2URv+Q1l3F7Ie7DKXHR18y7w<0F)fLnE(I) delta 297 zcmZp8z|qjaF+o~z9|Hq}0uaN1=0qK1o_!2@Sr>S@eHer|Ste$yvT?_N;ATOCZ)}r$ z*!NEU%fYSzl-(k(s>;};T9TNQlbW27n3tED6JL~{pNq_9nw-Ly#HcX&1fS++7S0@| z$^W=H*vz%q#JyD~f8dswe4d+k^I~oTCe<9UVcY}_v*K#B(O?(X*Jo@}-)zHoqL5jY zv0yTLgX3hchS?K0^lmO_oWjSf%4s&4eS+g=uL*lOCT=jC%sx}B*=Oc Promise} callback + * @returns Promise + * @template T + */ +async function withWebhook(channelID, callback) { + const webhook = await ensureWebhook(channelID, false) + return callback(webhook).catch(e => { + console.error(e) + // TODO: check if the error was webhook-related and if webhook.created === false, then: const webhook = ensureWebhook(channelID, true); return callback(webhook) + throw new Error(e) + }) +} + +module.exports.ensureWebhook = ensureWebhook +module.exports.withWebhook = withWebhook diff --git a/m2d/actions/send-message.js b/m2d/actions/send-message.js new file mode 100644 index 0000000..9c61107 --- /dev/null +++ b/m2d/actions/send-message.js @@ -0,0 +1,23 @@ +// @ts-check + +const assert = require("assert").strict +const DiscordTypes = require("discord-api-types/v10") +const passthrough = require("../../passthrough") +const {sync, discord, db} = passthrough + +/** @type {import("./register-webhook")} */ +const registerWebhook = sync.require("./register-webhook") + +/** + * @param {string} channelID + * @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {name: string, file: Buffer}[]} data + */ +// param {DiscordTypes.RESTPostAPIWebhookWithTokenQuery & {wait: true, disableEveryone?: boolean}} options +async function sendMessage(channelID, data) { + const result = await registerWebhook.withWebhook(channelID, async webhook => { + return discord.snow.webhook.executeWebhook(webhook.id, webhook.token, data, {wait: true, disableEveryone: true}) + }) + return result +} + +module.exports.sendMessage = sendMessage diff --git a/test/test.js b/test/test.js index ae6aea6..5f06ae4 100644 --- a/test/test.js +++ b/test/test.js @@ -17,3 +17,4 @@ require("../matrix/read-registration.test") require("../d2m/converters/message-to-event.test") require("../d2m/actions/create-room.test") require("../d2m/converters/user-to-mxid.test") +require("../d2m/actions/register-user.test") diff --git a/types.d.ts b/types.d.ts index bc24329..aaa8db1 100644 --- a/types.d.ts +++ b/types.d.ts @@ -9,6 +9,11 @@ export type AppServiceRegistrationConfig = { rate_limited: boolean } +export type WebhookCreds = { + id: string + token: string +} + namespace Event { export type BaseStateEvent = { type: string