Skip to content

Commit

Permalink
refactor: add sync methods to ix builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Dec 5, 2022
1 parent 9883294 commit cc49139
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
15 changes: 15 additions & 0 deletions examples/interactions.ts
Original file line number Diff line number Diff line change
@@ -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))
16 changes: 9 additions & 7 deletions src/Interactions/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export const run = <R, R2, E, E2>(
},
)

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)

Expand Down
34 changes: 34 additions & 0 deletions src/Interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
modalSubmit,
autocomplete,
InteractionDefinition,
InteractionResponse,
} from "./definitions.js"

export {
Expand Down Expand Up @@ -42,6 +43,39 @@ class InteractionBuilder<R, E> {
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<R, E> =>
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<R, E> =>
c._tag === "GuildApplicationCommand",
)
.map((c) => c.command)

return Rest.rest.bulkOverwriteGuildApplicationCommands(
appId,
guildId,
commands as any,
)
}
}

export const builder = new InteractionBuilder<never, never>([])
Expand Down

0 comments on commit cc49139

Please sign in to comment.