krisppurg / dimscord

A Discord Bot & REST Library for Nim.
https://krisppurg.github.io/dimscord/
MIT License
222 stars 22 forks source link
api client discord discord-api discord-library nim

Dimscord

A Discord Bot & REST Library for Nim. Discord API Channel and Dimscord Server for help

Why Dimscord?

Getting Started:

  1. Install Nim using choosenim or Nim's website

  2. Install Dimscord via Nimble using nimble install dimscord or GitHub git clone https://github.com/krisppurg/dimscord

    • You will need at least Nim 1.2.0 to install dimscord
  3. Read the Wiki or Examples for referencing. Maybe even rewrite your bot if you want to switch.

  4. Start coding! Stay up-to-date with the latest Dimscord release and stuff.

Quick Example:

import dimscord, asyncdispatch, times, options

let discord = newDiscordClient("<your bot token goes here>")

# Handle event for on_ready.
proc onReady(s: Shard, r: Ready) {.event(discord).} =
    echo "Ready as " & $r.user

# Handle event for message_create.
proc messageCreate(s: Shard, m: Message) {.event(discord).} =
    if m.author.bot: return
    if m.content == "!ping": # If message content is "!ping".
        let
            before = epochTime() * 1000
            msg = await discord.api.sendMessage(m.channel_id, "ping?")
            after = epochTime() * 1000
        # Now edit the message.
        # Use 'discard' because editMessage returns a new message.
        discard await discord.api.editMessage(
            m.channel_id,
            msg.id, 
            "Pong! took " & $int(after - before) & "ms | " & $s.latency() & "ms."
        )
    elif m.content == "!embed": # Otherwise if message content is "!embed".
        # Sends a message with embed.
        discard await discord.api.sendMessage(
            m.channel_id,
            embeds = @[Embed(
                title: some "Hello there!", 
                description: some "This is description",
                color: some 0x7789ec
            )]
        )

# Connect to Discord and run the bot.
waitFor discord.startSession()

Please note that you need to define -d:ssl if you are importing httpclient before importing dimscord. You can use -d:dimscordDebug, if you want to debug.

If you want to use voice then you can use -d:dimscordVoice, this requires libsodium, libopus, ffmpeg and optionally yt-dlp (by default)

Contributing