DiscordGo

repository·master·Indexed 26 days ago

https://github.com/bwmarrin/discordgo

A low-level Go package providing bindings to the Discord chat client API, including support for the websocket and voice interfaces. It provides direct bindings to Discord REST API endpoints and the data websocket interface, featuring built-in state tracking, caching, and support for multi-server voice connections.

Tokens
20.9K
Snippets
18
Records
157
Agent score
89%

What's inside discordgo

  1. Overview of DiscordGo

    master
    DiscordGo is a Go (Golang) interface for the Discord chat service. It provides low-level direct bindings to the Discord REST API endpoints and the data websocket interface, alongside helper functions designed to simplify the creation of custom Discord clients and chat bot applications. Key features include high performance, minimal resource load, support for multi-server voice connections (both sending and receiving), and built-in state tracking and caching.
  2. Create a Discord Bot Application

    master

    To use DiscordGo for automation or services, you must create a Bot account. Bot accounts are distinct from regular user accounts and are managed through the Discord Developer Portal.

    1. Create a standard Discord user account.
    2. Visit the My Applications page.
    3. Click on the New Application box.
    4. Follow the prompts to complete the creation process.
  3. Initialize a new Discord client

    master

    Import the github.com/bwmarrin/discordgo package and use discordgo.New() to construct a new client. The client requires an authentication token prefixed with Bot .

    import "github.com/bwmarrin/discordgo"
    
    // ...
    
    discord, err := discordgo.New("Bot " + "authentication token")
  4. Install DiscordGo on Linux/BSD

    master

    To install DiscordGo on Linux or BSD, download the package to your $GOPATH/src folder using go get, then compile and install it to $GOPATH/pkg. Compiling with go install is highly recommended as it enables autocomplete for DiscordGo functions in most IDEs/editors.

    go get github.com/bwmarrin/discordgo
    
    cd $GOPATH/src/github.com/bwmarrin/discordgo
    go install
  5. Verify DiscordGo Requirements

    master

    Before installing DiscordGo, ensure your environment meets the following requirements:

    • Go Version: 1.4 or higher.
    • Operating Systems: Tested on Debian Linux 8, FreeBSD 10, and Windows 7. It is expected to work on any platform supporting Go 1.4+.
    • Go Environment: You must have a working Go environment already installed and configured.
  6. Explore DiscordGo examples and documentation

    master

    Detailed usage information and code samples can be found in the following locations:

    • Example Programs: A collection of example programs is located in the examples/ directory of the repository.
    • Go Reference: The official Go documentation (pkg.go.dev) provides the most up-to-date technical reference for all exported symbols.
    • Awesome DiscordGo: A curated list of high-quality projects using DiscordGo is available on the project wiki.
  7. Handle Interaction events

    master

    Interactions are received as an Interaction struct. The Type field determines how to process the Data field. Use the provided helper methods to safely assert the data type:

    • For InteractionApplicationCommand or InteractionApplicationCommandAutocomplete: Use ApplicationCommandData() to get ApplicationCommandInteractionData.
    • For InteractionMessageComponent: Use MessageComponentData() to get MessageComponentInteractionData.
    • For InteractionModalSubmit: Use ModalSubmitData() to get ModalSubmitInteractionData.

    Warning: Calling these helpers on the wrong interaction type will cause a panic.

    func (i Interaction) ApplicationCommandData() (data ApplicationCommandInteractionData) {
    	if i.Type != InteractionApplicationCommand && i.Type != InteractionApplicationCommandAutocomplete {
    		panic("ApplicationCommandData called on interaction of type " + i.Type.String())
    	}
    	return i.Data.(ApplicationCommandInteractionData)
    }
  8. Initialize an empty State

    master
    Use NewState() to create a new State instance. By default, it enables tracking for channels, threads, emojis, stickers, members, thread members, roles, voice, and presences. It also initializes internal maps for guilds, channels, and members.
  9. Calculate User permissions in a channel

    master
    Use UserChannelPermissions to determine the bitwise permissions of a specific user within a specific channel. It calculates permissions based on the user's roles and the channel's permission overwrites.
  10. Manage Guild Integrations

    master

    Manage integrations associated with a guild:

    • GuildIntegrations(guildID): Returns an array of *Integration objects.
    • GuildIntegrationCreate(guildID, integrationType, integrationID): Creates a new integration.
    • GuildIntegrationEdit(guildID, integrationID, expireBehavior, expireGracePeriod, enableEmoticons): Edits an existing integration.
    • GuildIntegrationDelete(guildID, integrationID): Removes an integration.