disgo

repository·master·Indexed 20 days ago

https://github.com/disgoorg/disgo

A Go library for interacting with the Discord API. It provides a high-level Client for managing REST, Gateway/Sharding, and HTTP servers, as well as specialized modules for OAuth2 and Webhooks. The library includes a voice package supporting End-to-End Encryption (E2EE) via DAVE, with both GoDave (CGO) and Dave-Go (Pure Go) implementations.

Tokens
6.1K
Snippets
21
Records
33
Agent score
70%

What's inside disgo

  1. Use disgo with a gateway and REST proxy

    master

    You can configure disgo to route its communication through external proxies, such as gateway-proxy or http-proxy. This is achieved by setting specific environment variables that redirect the gateway (WebSocket) and REST (HTTP) traffic to your proxy URLs instead of the default Discord endpoints.

    disgo_gateway_url=ws://gateway-proxy:7878
    disgo_rest_url=http://rest-proxy:7979/api/v10
  2. Configure a Webhook client with optional arguments

    master

    The webhook.New function accepts variadic webhook.ConfigOpt arguments to customize the client. You can provide a custom logger or set default allowed mentions for all messages sent by this client.

    client := webhook.New(snowflake.ID("webhookID"), "webhookToken",
    	webhook.WithLogger(logrus.New()),
    	webhook.WithDefaultAllowedMentions(discord.AllowedMentions{
    		RepliedUser: false,
    	}),
    )
  3. Initialize a Webhook client

    master

    To use webhooks in disgo, import the webhook package. You can initialize a WebhookClient using either a specific webhook_id and webhook_token or via a direct webhookURL.

    Note: The WebhookClient should be created once and reused, as it holds important state.

    import "github.com/disgoorg/disgo/webhook"
    
    // Using ID and Token
    client := webhook.New(snowflake.ID("webhookID"), "webhookToken")
    
    // Using Webhook URL
    client, err := webhook.NewWithURL("webhookURL")
  4. Inject a custom signature verification implementation in HTTPServer

    master

    By default, httpserver uses crypto/ed25519 for signing verification. If you require a different implementation (for example, a faster one like github.com/oasisprotocol/curve25519-voi), you can override the global Verify function in the httpserver package.

    To do this, assign a function to httpserver.Verify that matches the signature: func(publicKey PublicKey, message, sig []byte) bool.

    package main
    
    import (
    	"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
    	"github.com/disgoorg/disgo/httpserver"
    )
    
    func main() {
    	// Override the default ed25519 verification with a custom implementation
    	httpserver.Verify = func(publicKey httpserver.PublicKey, message, sig []byte) bool {
    		return ed25519.Verify(publicKey, message, sig)
    	}
    }
  5. Send audio using the bot.Client package

    master

    To send audio in a Discord voice channel using bot.Client, you must:

    1. Initialize the client with the gateway.IntentGuildVoiceStates intent.
    2. Configure the VoiceManager with a DAVE session creation function (either golibdave.NewSession for GoDave or session.NewSession for Dave-Go).
    3. Create a connection via client.VoiceManager().CreateConn(guildID).
    4. Open the connection using conn.Open.
    5. Use conn.UDP().Write(frame) to send Opus frames.
    const (
        guildID = 12345
        channelID = 12345
    )
    
    client, err := disgo.New(token,
    	bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentGuildVoiceStates)),
    	bot.WithVoiceManagerConfigOpts(
    		// for GoDave use this
    		voice.WithDaveSessionCreateFunc(golibdave.NewSession),
    		// for Dave-Go use this
    		// voice.WithDaveSessionCreateFunc(session.NewSession),
    	),
    )
    // handle err
    
    conn := client.VoiceManager().CreateConn(guildID)
    
    err = conn.Open(context.TODO(), channelID, false, false)
    // handle err
    
    // set speaking flag
    err = conn.SetSpeaking(ctx, voice.SpeakingFlagMicrophone)
    
    // send opus frame
    conn.UDP().Write(frame)
    
    // close connection
    conn.Close()
  6. Edit messages via Webhook

    master

    Existing messages can be updated using the following methods. All methods require the message_id as a string:

    1. UpdateContent: Updates only the text content of the message.
    2. UpdateEmbeds: Updates only the embeds of the message.
    3. UpdateMessage: The flexible method. It accepts a discord.NewWebhookMessageUpdate() builder or a webhook.WebhookMessageUpdate struct, along with rest.UpdateWebhookMessageParams.
    client := webhook.New(snowflake.ID("webhookID"), "webhookToken")
    
    // Update text
    message, err := client.UpdateContent("870741249114652722", "hello world!")
    
    // Update embeds
    message, err := client.UpdateEmbeds("870741249114652722", discord.NewEmbed().
    	WithDescription("hello world!"),
    )
    
    // Using the builder pattern
    message, err := client.UpdateMessage("870741249114652722", discord.NewWebhookMessageUpdate().
    	WithContent("hello world!"),
    	rest.UpdateWebhookMessageParams{},
    )
    
    // Using a struct
    message, err := client.UpdateMessage("870741249114652722", webhook.WebhookMessageUpdate{
    	Content: json.Ptr("hello world!"),
    }, rest.UpdateWebhookMessageParams{})
  7. Send messages via Webhook

    master

    You can send messages using several methods depending on the level of control required:

    1. CreateContent: A simple way to send plain text content.
    2. CreateEmbeds: Used to send messages containing only embeds.
    3. CreateMessage: The most flexible method. It accepts a discord.NewWebhookMessageCreate() builder or a webhook.WebhookMessageCreate struct, along with rest.CreateWebhookMessageParams for advanced configuration.
    client := webhook.New(snowflake.ID("webhookID"), "webhookToken")
    
    // Simple text
    message, err := client.CreateContent("hello world!")
    
    // Using embeds
    message, err := client.CreateEmbeds(discord.NewEmbed().
    	WithDescription("hello world!"),
    )
    
    // Using the builder pattern
    message, err := client.CreateMessage(discord.NewWebhookMessageCreate().
    	WithContent("hello world!"),
    	rest.CreateWebhookMessageParams{},
    )
    
    // Using a struct
    message, err := client.CreateMessage(webhook.WebhookMessageCreate{
    	Content: "hello world!",
    }, rest.CreateWebhookMessageParams{})