From cc4913921b9ca5d8cd6b120767ad7a40fc8729c5 Mon Sep 17 00:00:00 2001 From: Tim Smart Date: Mon, 5 Dec 2022 22:54:44 +1300 Subject: [PATCH] refactor: add sync methods to ix builder --- examples/interactions.ts | 15 +++++++++++++++ src/Interactions/gateway.ts | 16 +++++++++------- src/Interactions/index.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/examples/interactions.ts b/examples/interactions.ts index 8d7db2e..a1b5e19 100644 --- a/examples/interactions.ts +++ b/examples/interactions.ts @@ -1,2 +1,17 @@ import { Ix } from "../dist/index.js" import * as Effect from "@effect/io/Effect" + +const hello = Ix.global( + { + name: "hello", + description: "A basic command", + }, + Effect.succeedSome({ + type: 4, + data: { + content: "Hello!", + }, + }), +) + +const program = Ix.builder.add(hello).runGateway((e) => Effect.fail(e)) diff --git a/src/Interactions/gateway.ts b/src/Interactions/gateway.ts index afe809a..d0eb4e6 100644 --- a/src/Interactions/gateway.ts +++ b/src/Interactions/gateway.ts @@ -28,13 +28,15 @@ export const run = ( }, ) - const guildSync = Gateway.handleDispatch("GUILD_CREATE", (a) => - Rest.rest.bulkOverwriteGuildApplicationCommands( - application.id, - a.id, - GuildApplicationCommand.map((a) => a.command) as any, - ), - ) + const guildSync = GuildApplicationCommand.length + ? Gateway.handleDispatch("GUILD_CREATE", (a) => + Rest.rest.bulkOverwriteGuildApplicationCommands( + application.id, + a.id, + GuildApplicationCommand.map((a) => a.command) as any, + ), + ) + : Effect.unit() const handle = handlers(definitions) diff --git a/src/Interactions/index.ts b/src/Interactions/index.ts index d169dc2..d9415b3 100644 --- a/src/Interactions/index.ts +++ b/src/Interactions/index.ts @@ -11,6 +11,7 @@ export { modalSubmit, autocomplete, InteractionDefinition, + InteractionResponse, } from "./definitions.js" export { @@ -42,6 +43,39 @@ class InteractionBuilder { handleWebhook(headers: Webhook.Headers, rawBody: string) { return Webhook.run(this.definitions, headers, rawBody) } + + get syncGlobal() { + const commands = this.definitions + .filter( + (c): c is D.GlobalApplicationCommand => + c._tag === "GlobalApplicationCommand", + ) + .map((c) => c.command) + + return Rest.rest + .getCurrentBotApplicationInformation() + .flatMap((r) => r.json) + .flatMap((app) => + Rest.rest.bulkOverwriteGlobalApplicationCommands(app.id, { + body: JSON.stringify(commands), + }), + ) + } + + syncGuild(appId: Discord.Snowflake, guildId: Discord.Snowflake) { + const commands = this.definitions + .filter( + (c): c is D.GuildApplicationCommand => + c._tag === "GuildApplicationCommand", + ) + .map((c) => c.command) + + return Rest.rest.bulkOverwriteGuildApplicationCommands( + appId, + guildId, + commands as any, + ) + } } export const builder = new InteractionBuilder([])