DiscordGo
repository·master·Indexed 26 days ago
https://github.com/bwmarrin/discordgoA 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.
What's inside discordgo
- 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.
Create a Discord Bot Application
masterTo 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.
- Create a standard Discord user account.
- Visit the My Applications page.
- Click on the New Application box.
- Follow the prompts to complete the creation process.
Initialize a new Discord client
masterImport the
github.com/bwmarrin/discordgopackage and usediscordgo.New()to construct a new client. The client requires an authentication token prefixed withBot.import "github.com/bwmarrin/discordgo" // ... discord, err := discordgo.New("Bot " + "authentication token")Install DiscordGo on Linux/BSD
masterTo install DiscordGo on Linux or BSD, download the package to your
$GOPATH/srcfolder usinggo get, then compile and install it to$GOPATH/pkg. Compiling withgo installis 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 installVerify DiscordGo Requirements
masterBefore 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.
Explore DiscordGo examples and documentation
masterDetailed 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.
- Example Programs: A collection of example programs is located in the
Install DiscordGo
masterTo install DiscordGo, use the
go getcommand. This will pull the latest tagged release from the master branch. Ensure you have a working Go environment installed first.go get github.com/bwmarrin/discordgoHandle Interaction events
masterInteractions are received as an
Interactionstruct. TheTypefield determines how to process theDatafield. Use the provided helper methods to safely assert the data type:- For
InteractionApplicationCommandorInteractionApplicationCommandAutocomplete: UseApplicationCommandData()to getApplicationCommandInteractionData. - For
InteractionMessageComponent: UseMessageComponentData()to getMessageComponentInteractionData. - For
InteractionModalSubmit: UseModalSubmitData()to getModalSubmitInteractionData.
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) }- For
Initialize an empty State
masterUseNewState()to create a newStateinstance. 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.Retrieve Discord Gateway Address
masterUseGatewayorGatewayBotto obtain the WebSocket gateway URL.GatewayBotis recommended for bots as it provides the gateway URL along with the recommended number of shards.Calculate User permissions in a channel
masterUseUserChannelPermissionsto 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.Manage Guild Integrations
masterManage integrations associated with a guild:
GuildIntegrations(guildID): Returns an array of*Integrationobjects.GuildIntegrationCreate(guildID, integrationType, integrationID): Creates a new integration.GuildIntegrationEdit(guildID, integrationID, expireBehavior, expireGracePeriod, enableEmoticons): Edits an existing integration.GuildIntegrationDelete(guildID, integrationID): Removes an integration.