Discord Module

Discord API integration for duso. Provides webhooks and Gateway client for real-time event handling.

Usage

discord = require("discord")

Functions

post_webhook(url, payload)

Post a message to a Discord webhook.

Parameters:

Returns: Boolean (true if successful)

Example:

discord.post_webhook("https://discordapp.com/api/webhooks/...", {
  content = "Hello from Duso!"
})

session(config)

Create a Gateway client connection for real-time events.

Parameters:

Returns: Session object

Example:

bot = discord.session({
  token = env("DISCORD_TOKEN")
})

Gateway Intents

Intents control what events your bot receives. Combine intent constants with +:

discord = require("discord")

// Use individual intents
intents = discord.intents.guilds + discord.intents.guild_messages + discord.intents.message_content

bot = discord.session({
  token = env("DISCORD_TOKEN"),
  intents = intents
})

Available intents:

Session Object

Returned by session(). Handles Gateway connection and event reading.

Methods

Event Structure

Events returned by read() have this structure:

// Example event structure
event = {
  type = "MESSAGE_CREATE",
  data = {content = "hello", author = {id = 123}},
  seq = 1234
}

Common event types:

Example: Echo Bot

discord = require("discord")

bot = discord.session({
  token = env("DISCORD_TOKEN"),
  intents = discord.intents.guilds + discord.intents.guild_messages + discord.intents.message_content
})

print("Bot connected")

while bot.is_connected() do
  event = bot.read(timeout=30)
  
  if event == nil then
    continue
  end

  if event.type == "MESSAGE_CREATE" then
    msg = event.data
    
    // Don't respond to bot messages
    if msg.author.bot then
      continue
    end

    // Don't respond to messages in DMs
    if msg.guild_id == nil then
      continue
    end

    // Send reply
    bot.send_message(msg.channel_id, {
      content = "You said: " + msg.content
    })
  end
end

bot.close()

Example: Webhook Alert

discord = require("discord")

webhook_url = "https://discordapp.com/api/webhooks/..."

discord.post_webhook(webhook_url, {
  content = "Alert: Server CPU at 90%",
  embeds = [
    {
      title = "System Alert",
      color = 16711680,  // Red
      fields = [
        {
          name = "Status",
          value = "Critical",
          inline = true
        },
        {
          name = "Timestamp",
          value = format_time(now()),
          inline = true
        }
      ]
    }
  ]
})

Notes

See Also