Skip to content

tim-smart/dfx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Mar 13, 2025
8545bba · Mar 13, 2025
Apr 29, 2024
Mar 13, 2025
Feb 7, 2025
Mar 13, 2025
Jul 10, 2023
Jul 10, 2023
Jul 10, 2023
May 7, 2024
May 7, 2024
Mar 14, 2022
Aug 29, 2023
Feb 7, 2023
Mar 13, 2024
Mar 13, 2025
Aug 4, 2023
May 7, 2024
Sep 10, 2024
May 7, 2024
May 7, 2024
Mar 13, 2024
Mar 13, 2025
Mar 13, 2025
Jul 10, 2023
Jul 10, 2023
Jul 10, 2023
Jul 10, 2023
Jul 10, 2023
Jul 10, 2023
Jul 10, 2023

Repository files navigation

dfx

Discord

A Discord library built on top of effect

  • Supports both the gateway and webhooks
  • Simple yet powerful abstractions to build Discord bots

Example

import { NodeHttpClient, NodeRuntime, NodeSocket } from "@effect/platform-node"
import { DiscordConfig, Ix } from "dfx"
import { DiscordIxLive, InteractionsRegistry } from "dfx/gateway"
import * as Dotenv from "dotenv"
import { Config, Effect, Layer } from "effect"

Dotenv.config()

// Create a config layer
const DiscordConfigLive = DiscordConfig.layerConfig({
  token: Config.secret("DISCORD_BOT_TOKEN"),
})

// Create hello service
const HelloLive = Layer.effectDiscard(
  Effect.gen(function* (_) {
    const registry = yield* _(InteractionsRegistry)

    // Create hello command that responds with "Hello!"
    const hello = Ix.global(
      {
        name: "hello",
        description: "A basic command",
      },
      Effect.succeed({
        type: 4,
        data: {
          content: "Hello!",
        },
      }),
    )

    // register the command(s) and handle errors
    yield* _(
      registry.register(Ix.builder.add(hello).catchAllCause(Effect.logError)),
    )
  }),
).pipe(
  // provide discord ix layer
  Layer.provide(DiscordIxLive),
)

// Construct the main layer
const MainLive = HelloLive.pipe(
  Layer.provide(NodeHttpClient.layerUndici),
  Layer.provide(NodeSocket.layerWebSocketConstructor),
  Layer.provide(DiscordConfigLive),
)

// run it
NodeRuntime.runMain(Layer.launch(MainLive))