Documentation
¶
Overview ¶
Package discord provides a Discord API client.
Example ¶
A simple program that sends "Hello, World!" to a user.
package main import ( "context" "zombiezen.com/go/discord" ) func main() { ctx := context.TODO() // Get authorization token. Details at // https://discord.com/developers/docs/reference#authentication auth := discord.BotAuthorization("xyzzy") // Create a client. client := discord.NewClient(auth, nil) // Get or create a DM channel for a particular user. // https://discord.com/developers/docs/resources/user#create-dm // // To find out your user ID, see // https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID- userID := "123" dmChannel, err := client.CreateDM(ctx, userID) if err != nil { // ... handle error ... } // Send a message on a channel. // https://discord.com/developers/docs/resources/channel#create-message _, err = client.CreateMessage(ctx, &discord.CreateMessageParams{ ChannelID: dmChannel.ID, Content: "Hello, World!", }) if err != nil { // ... handle error ... } }
Output:
Index ¶
- Constants
- func OAuth2AuthorizationURL(clientID string, opts *OAuth2Options) *url.URL
- type ApplicationCommand
- type ApplicationCommandOption
- type ApplicationCommandOptionChoice
- type ApplicationCommandOptionType
- type AuthHeader
- type AuthorizationInfo
- type ButtonStyle
- type Channel
- type ChannelType
- type Client
- func (c *Client) CreateDM(ctx context.Context, recipientID string) (*Channel, error)
- func (c *Client) CreateGlobalApplicationCommand(ctx context.Context, cmd *ApplicationCommand) (id string, err error)
- func (c *Client) CreateInteractionResponse(ctx context.Context, resp *InteractionResponse) error
- func (c *Client) CreateMessage(ctx context.Context, params *CreateMessageParams) (*Message, error)
- func (c *Client) CreateReaction(ctx context.Context, channelID, messageID, emoji string) error
- func (c *Client) CurrentAuthorization(ctx context.Context) (*AuthorizationInfo, error)
- func (c *Client) EditMessage(ctx context.Context, params *EditMessageParams) (*Message, error)
- func (c *Client) EditWebhookMessage(ctx context.Context, resp *EditWebhookMessageParams) (*Message, error)
- func (c *Client) GetChannel(ctx context.Context, id string) (*Channel, error)
- func (c *Client) GetMessage(ctx context.Context, channelID, messageID string) (*Message, error)
- func (c *Client) GetUser(ctx context.Context, id string) (*User, error)
- func (c *Client) ListGlobalApplicationCommands(ctx context.Context, appID string) ([]*ApplicationCommand, error)
- func (c *Client) OpenGateway(ctx context.Context) (*Gateway, error)
- type ClientOptions
- type Component
- type ComponentType
- type CreateMessageParams
- type EditMessageParams
- type EditWebhookMessageParams
- type Emoji
- type Gateway
- func (g *Gateway) Close() error
- func (g *Gateway) Listen(ctx context.Context) (*GatewayPayload, error)
- func (g *Gateway) SetConnectFuncs(start, success func(ctx context.Context, isResume bool))
- func (g *Gateway) SetErrorFunc(f func(ctx context.Context, err error))
- func (g *Gateway) URL() *url.URL
- func (g *Gateway) WaitForReady(ctx context.Context) error
- type GatewayPayload
- type GuildMember
- type Interaction
- type InteractionCallbackFlags
- type InteractionCallbackType
- type InteractionData
- type InteractionDataOption
- type InteractionRequestType
- type InteractionResponse
- type InteractionResponseData
- type Message
- type MessageReference
- type MessageType
- type OAuth2Options
- type Permissions
- type Reaction
- type SelectOption
- type User
Examples ¶
Constants ¶
const InteractionResponseID = "@original"
InteractionResponseID is the message ID to use when accessing or modifying the initial response to an interaction. https://discord.com/developers/docs/interactions/receiving-and-responding#followup-messages
Variables ¶
This section is empty.
Functions ¶
func OAuth2AuthorizationURL ¶
func OAuth2AuthorizationURL(clientID string, opts *OAuth2Options) *url.URL
Types ¶
type ApplicationCommand ¶
type ApplicationCommand struct { ID string `json:"id,omitempty"` ApplicationID string `json:"application_id"` GuildID string `json:"guild_id,omitempty"` Name string `json:"name"` Description string `json:"description"` Options []*ApplicationCommandOption `json:"options"` }
An ApplicationCommand is the base "command" model that belongs to an application. https://discord.com/developers/docs/interactions/slash-commands#application-command-object-application-command-option-structure
type ApplicationCommandOption ¶
type ApplicationCommandOption struct { Type ApplicationCommandOptionType `json:"type"` Name string `json:"name"` Description string `json:"string"` Required bool `json:"required"` Choices []*ApplicationCommandOptionChoice `json:"choices"` Options []*ApplicationCommandOption `json:"options,omitempty"` }
An ApplicationCommandOption describes an option given to an application command. https://discord.com/developers/docs/interactions/slash-commands#application-command-object-application-command-option-structure
type ApplicationCommandOptionChoice ¶
type ApplicationCommandOptionChoice struct { Name string `json:"name"` Value interface{} `json:"value"` // a string or a json.Number }
An ApplicationCommandOptionChoice describes a valid value for a "choices" application command option. https://discord.com/developers/docs/interactions/slash-commands#application-command-object-application-command-option-choice-structure
type ApplicationCommandOptionType ¶
type ApplicationCommandOptionType int
ApplicationCommandOptionType is an enumeration of the types of application command options. https://discord.com/developers/docs/interactions/slash-commands#application-command-object-application-command-option-type
const ( OptionTypeSubCommand ApplicationCommandOptionType = 1 OptionTypeSubCommandGroup ApplicationCommandOptionType = 2 OptionTypeString ApplicationCommandOptionType = 3 OptionTypeInteger ApplicationCommandOptionType = 4 OptionTypeBoolean ApplicationCommandOptionType = 5 OptionTypeUser ApplicationCommandOptionType = 6 OptionTypeChannel ApplicationCommandOptionType = 7 OptionTypeRole ApplicationCommandOptionType = 8 OptionTypeMentionable ApplicationCommandOptionType = 9 OptionTypeNumber ApplicationCommandOptionType = 10 )
Valid application command option types. https://discord.com/developers/docs/interactions/slash-commands#application-command-object-application-command-option-type
type AuthHeader ¶
type AuthHeader string
AuthHeader is authentication passed as an Authorization HTTP header value. https://discord.com/developers/docs/reference#authentication
func BearerAuthorization ¶
func BearerAuthorization(token string) AuthHeader
BearerAuthorization returns the authentication for a bearer token.
func BotAuthorization ¶
func BotAuthorization(token string) AuthHeader
BotAuthorization returns the authentication for a bot token.
func (AuthHeader) IsValid ¶
func (auth AuthHeader) IsValid() bool
IsValid reports whether the authentication is in a format that Discord will accept.
type AuthorizationInfo ¶
type AuthorizationInfo struct { Scopes []string `json:"scopes"` Expires time.Time `json:"expires"` User *User `json:"user,omitempty"` }
AuthorizationInfo holds the response to a CurrentAuthorization call. https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information-response-structure
type ButtonStyle ¶
type ButtonStyle int
ButtonStyle is an enumeration of component button styles. https://discord.com/developers/docs/interactions/message-components#button-object-button-styles
const ( ButtonStylePrimary ButtonStyle = 1 ButtonStyleSecondary ButtonStyle = 2 ButtonStyleSuccess ButtonStyle = 3 ButtonStyleDanger ButtonStyle = 4 ButtonStyleLink ButtonStyle = 5 )
Valid component button styles. https://discord.com/developers/docs/interactions/message-components#button-object-button-styles
type Channel ¶
type Channel struct { ID string `json:"id"` Type ChannelType `json:"type"` GuildID string `json:"guild_id,omitempty"` Position int `json:"position,omitempty"` Name string `json:"name,omitempty"` Topic string `json:"topic,omitempty"` NSFW bool `json:"nsfw,omitempty"` LastMessageID string `json:"last_message_id,omitempty"` Bitrate int `json:"bitrate,omitempty"` UserLimit int `json:"user_limit,omitempty"` RateLimitPerUserSeconds int `json:"rate_limit_per_user,omitempty"` Recipients []*User `json:"recipients,omitempty"` Icon string `json:"icon,omitempty"` OwnerID string `json:"owner_id,omitempty"` ApplicationID string `json:"application_id,omitempty"` ParentID string `json:"parent_id,omitempty"` }
A Channel represents a guild or DM channel within Discord. https://discord.com/developers/docs/resources/channel#channel-object
type ChannelType ¶
type ChannelType int
ChannelType is an enumeration of channel types. https://discord.com/developers/docs/resources/channel#channel-object-channel-types
const ( ChannelTypeGuildText ChannelType = 0 ChannelTypeDM ChannelType = 1 ChannelTypeGuildVoice ChannelType = 2 ChannelTypeGroupDM ChannelType = 3 ChannelTypeGuildCategory ChannelType = 4 ChannelTypeGuildNews ChannelType = 5 ChannelTypeGuildStore ChannelType = 6 ChannelTypeGuildNewsThread ChannelType = 10 ChannelTypeGuildPublicThread ChannelType = 11 ChannelTypeGuildPrivateThread ChannelType = 12 ChannelTypeGuildStageVoice ChannelType = 12 )
Valid channel types. https://discord.com/developers/docs/resources/channel#channel-object-channel-types
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an authenticated client of the Discord API.
func NewClient ¶
func NewClient(auth AuthHeader, opts *ClientOptions) *Client
NewClient returns a new client. opts may be nil. NewClient panics if auth.IsValid() reports false.
func (*Client) CreateDM ¶
CreateDM creates a new DM channel with a user. https://discord.com/developers/docs/resources/user#create-dm
func (*Client) CreateGlobalApplicationCommand ¶
func (c *Client) CreateGlobalApplicationCommand(ctx context.Context, cmd *ApplicationCommand) (id string, err error)
CreateGlobalApplicationCommand creates a new global command. https://discord.com/developers/docs/interactions/slash-commands#create-global-application-command
func (*Client) CreateInteractionResponse ¶
func (c *Client) CreateInteractionResponse(ctx context.Context, resp *InteractionResponse) error
func (*Client) CreateMessage ¶
CreateMessage posts a message to a guild text or DM channel. https://discord.com/developers/docs/resources/channel#create-message
func (*Client) CreateReaction ¶
CreateReaction creates a reaction for a message to a guild text or DM channel. https://discord.com/developers/docs/resources/channel#create-reaction
func (*Client) CurrentAuthorization ¶
func (c *Client) CurrentAuthorization(ctx context.Context) (*AuthorizationInfo, error)
CurrentAuthorization returns info about the current authorization. https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information
func (*Client) EditMessage ¶
EditMessage posts a message to a guild text or DM channel. https://discord.com/developers/docs/resources/channel#edit-message
func (*Client) EditWebhookMessage ¶
func (c *Client) EditWebhookMessage(ctx context.Context, resp *EditWebhookMessageParams) (*Message, error)
EditWebhookMessage updates a message previously sent by a webhook or in response to an interaction. https://discord.com/developers/docs/resources/webhook#edit-webhook-message
func (*Client) GetChannel ¶
GetChannel gets a channel by ID. https://discord.com/developers/docs/resources/user#create-dm
func (*Client) GetMessage ¶
GetMessage returns a specific message in the channel. https://discord.com/developers/docs/resources/channel#get-channel-message
func (*Client) GetUser ¶
GetUser returns a user object for the given ID. https://discord.com/developers/docs/resources/user#get-user
func (*Client) ListGlobalApplicationCommands ¶
func (c *Client) ListGlobalApplicationCommands(ctx context.Context, appID string) ([]*ApplicationCommand, error)
ListGlobalApplicationCommands fetches all of the global commands for your application. https://discord.com/developers/docs/interactions/slash-commands#get-global-application-commands
type ClientOptions ¶
type ClientOptions struct { // BaseURL specifies the client's base URL. If nil, the Discord API URL // documented in https://discord.com/developers/docs/reference#api-reference-base-url // is used. BaseURL *url.URL // HTTPClient is used to make HTTP requests. If nil, http.DefaultClient is used. HTTPClient *http.Client // UserAgent is the name of the application accessing Discord. UserAgent string }
ClientOptions holds optional parameters for NewClient.
type Component ¶
type Component struct { Type ComponentType `json:"type"` CustomID string `json:"custom_id,omitempty"` Disabled bool `json:"disabled,omitempty"` ButtonStyle ButtonStyle `json:"style,omitempty"` ButtonLabel string `json:"label,omitempty"` ButtonEmoji *Emoji `json:"emoji,omitempty"` ButtonURL string `json:"url,omitempty"` SelectOptions []*SelectOption `json:"options,omitempty"` SelectPlaceholder string `json:"placeholder,omitempty"` SelectMinValues json.Number `json:"min_values,omitempty"` SelectMaxValues json.Number `json:"max_values,omitempty"` Components []*Component `json:"components,omitempty"` }
A Component is an interactive element added to a Message. https://discord.com/developers/docs/interactions/message-components#component-object-component-structure
type ComponentType ¶
type ComponentType int
ComponentType is an enumeration of component types. https://discord.com/developers/docs/interactions/message-components#component-object-component-types
const ( ComponentTypeActionRow ComponentType = 1 ComponentTypeButton ComponentType = 2 ComponentTypeSelectMenu ComponentType = 3 )
Valid component types. https://discord.com/developers/docs/interactions/message-components#component-object-component-types
type CreateMessageParams ¶
type CreateMessageParams struct { ChannelID string `json:"-"` Content string `json:"content"` TextToSpeech bool `json:"tts"` MessageReference *MessageReference `json:"message_reference,omitempty"` Components []*Component `json:"components,omitempty"` }
https://discord.com/developers/docs/resources/channel#create-message-jsonform-params
type EditMessageParams ¶
type EditMessageParams struct { ChannelID string `json:"-"` MessageID string `json:"-"` Content string `json:"content,omitempty"` Components []*Component `json:"components"` }
https://discord.com/developers/docs/resources/channel#edit-message-jsonform-params
type EditWebhookMessageParams ¶
type EditWebhookMessageParams struct { WebhookID string `json:"-"` WebhookToken string `json:"-"` MessageID string `json:"-"` Content string `json:"content,omitempty"` Components []*Component `json:"components"` }
https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params
type Emoji ¶
type Emoji struct { ID string `json:"id,omitempty"` Name string `json:"name"` User *User `json:"user,omitempty"` RequiresColons bool `json:"requires_colons,omitempty"` Managed bool `json:"managed,omitempty"` Animated bool `json:"animated"` Available bool `json:"available,omitempty"` }
https://discord.com/developers/docs/resources/emoji#emoji-object-emoji-structure
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
Gateway represents an ongoing stream of events from the Discord Gateway. https://discord.com/developers/docs/topics/gateway
Example ¶
Discord's gateway enables receiving real-time events. This example shows how to set up a basic event loop to process incoming messages.
package main import ( "context" "encoding/json" "fmt" "zombiezen.com/go/discord" ) func main() { ctx := context.TODO() // Get authorization token. Details at // https://discord.com/developers/docs/reference#authentication auth := discord.BotAuthorization("xyzzy") // Create a client. client := discord.NewClient(auth, nil) // Open a connection to the Discord gateway. gateway, err := client.OpenGateway(ctx) if err != nil { // ... handle error ... } defer func() { if err := gateway.Close(); err != nil { // ... handle error ... } }() // Start listening for events. for { event, err := gateway.Listen(ctx) if err != nil { // ... handle error ... break } switch event.EventName { case "MESSAGE_CREATE": // The event data is a discord.Message JSON object. // https://discord.com/developers/docs/topics/gateway#message-create msg := new(discord.Message) if err := json.Unmarshal(event.Data, msg); err != nil { // ... handle error ... continue } fmt.Printf("%v: %s\n", msg.User(), msg.Content) default: // ... others ... } } }
Output:
func (*Gateway) Listen ¶
func (g *Gateway) Listen(ctx context.Context) (*GatewayPayload, error)
Listen listens for the next event from the gateway.
If there is a long delay (>30 seconds) between calls to Listen, the gateway may drop the connection. Listen will automatically handle any reconnects, so Listen will only return an error if the Context is Done.
func (*Gateway) SetConnectFuncs ¶
SetConnectFuncs sets callbacks that are called when a Websocket connection is about to be opened or after it is has successfully established.
func (*Gateway) SetErrorFunc ¶
SetErrorFunc sets a callback that is called when a non-critical error occurs during listening.
type GatewayPayload ¶
type GatewayPayload struct { OpCode int `json:"op"` Data json.RawMessage `json:"d"` SequenceNumber int64 `json:"s,omitempty"` EventName string `json:"t,omitempty"` }
GatewayPayload represents a single message sent over the Discord gateway websocket. https://discord.com/developers/docs/topics/gateway#payloads-gateway-payload-structure
type GuildMember ¶
type GuildMember struct { User *User `json:"user"` Nickname string `json:"nick,omitempty"` Roles []string `json:"roles"` JoinedAt time.Time `json:"joined_at"` Deaf bool `json:"deaf"` Mute bool `json:"mute"` Pending bool `json:"pending"` Permissions string `json:"permissions,omitempty"` }
https://discord.com/developers/docs/resources/guild#guild-member-object
type Interaction ¶
type Interaction struct { ID string `json:"id"` ApplicationID string `json:"application_id"` Type InteractionRequestType `json:"type"` Data *InteractionData `json:"data,omitempty"` GuildID string `json:"guild_id,omitempty"` ChannelID string `json:"channel_id,omitempty"` Member *GuildMember `json:"member,omitempty"` DMUser *User `json:"user,omitempty"` Token string `json:"token"` Message *Message `json:"message,omitempty"` }
An Interaction is the base "thing" that is sent when a user invokes a command, and is the same for Slash Commands and other future interaction types (such as Message Components). https://discord.com/developers/docs/interactions/slash-commands#interaction-object-interaction-structure
func (*Interaction) User ¶
func (interaction *Interaction) User() *User
type InteractionCallbackFlags ¶
type InteractionCallbackFlags uint
const (
InteractionCallbackFlagEphemeral InteractionCallbackFlags = 1 << 6
)
Valid interaction callback https://discord.com/developers/docs/interactions/slash-commands#interaction-response-object-interaction-application-command-callback-data-flags
type InteractionCallbackType ¶
type InteractionCallbackType int
InteractionCallbackType is an enumeration of interaction response types. https://discord.com/developers/docs/interactions/slash-commands#interaction-response-object-interaction-callback-type
const ( InteractionCallbackTypePong InteractionCallbackType = 1 InteractionCallbackTypeChannelMessageWithSource InteractionCallbackType = 4 InteractionCallbackTypeDeferredChannelMessageWithSource InteractionCallbackType = 5 InteractionCallbackTypeDeferredUpdateMessage InteractionCallbackType = 6 InteractionCallbackTypeUpdateMessage InteractionCallbackType = 7 )
Valid interaction callback types. https://discord.com/developers/docs/interactions/slash-commands#interaction-response-object-interaction-callback-type
type InteractionData ¶
type InteractionData struct { ID string `json:"id"` CommandName string `json:"name"` Options []*ApplicationCommandOption `json:"options,omitempty"` CustomID string `json:"custom_id,omitempty"` ComponentType ComponentType `json:"component_type,omitempty"` Values []string `json:"values"` }
InteractionData holds data about an interaction. https://discord.com/developers/docs/interactions/slash-commands#interaction-object-application-command-interaction-data-structure
type InteractionDataOption ¶
type InteractionDataOption struct { Name string `json:"name"` Type ApplicationCommandOptionType `json:"type"` Value interface{} `json:"value,omitempty"` Options []*InteractionDataOption `json:"options,omitempty"` }
InteractionDataOption holds data about an interaction option chosen. https://discord.com/developers/docs/interactions/slash-commands#interaction-object-application-command-interaction-data-option-structure
type InteractionRequestType ¶
type InteractionRequestType int
InteractionRequestType is an enumeration of interaction request types. https://discord.com/developers/docs/interactions/slash-commands#interaction-object-interaction-request-type
const ( InteractionRequestTypePing InteractionRequestType = 1 InteractionRequestTypeApplicationCommand InteractionRequestType = 2 InteractionRequestTypeMessageComponent InteractionRequestType = 3 )
Valid interaction request types. https://discord.com/developers/docs/interactions/slash-commands#interaction-object-interaction-request-type
type InteractionResponse ¶
type InteractionResponse struct { InteractionID string `json:"-"` InteractionToken string `json:"-"` Type InteractionCallbackType `json:"type"` Data *InteractionResponseData `json:"data,omitempty"` }
type InteractionResponseData ¶
type InteractionResponseData struct { TextToSpeech bool `json:"tts"` Content string `json:"content,omitempty"` Flags InteractionCallbackFlags `json:"flags"` }
type Message ¶
type Message struct { ID string `json:"id"` ChannelID string `json:"channel_id"` GuildID string `json:"guild_id"` Author *User `json:"author,omitempty"` Member *GuildMember `json:"member,omitempty"` Content string `json:"content"` Timestamp time.Time `json:"timestamp"` TextToSpeech bool `json:"tts"` MentionEveryone bool `json:"mention_everyone"` Reactions []*Reaction `json:"reactions"` Nonce string `json:"nonce,omitempty"` Pinned bool `json:"pinned"` WebhookID string `json:"webhook_id"` Type MessageType `json:"type"` ApplicationID string `json:"application_id,omitempty"` ReferencedMessage *MessageReference `json:"referenced_message"` Thread *Channel `json:"thread,omitempty"` Components []*Component `json:"components,omitempty"` }
A Message represents a message sent in a channel within Discord. https://discord.com/developers/docs/resources/channel#message-object
type MessageReference ¶
type MessageReference struct { MessageID string `json:"message_id,omitempty"` ResponseMessageID string `json:"id,omitempty"` // undocumented, but on response we only get "id", not "message_id" ChannelID string `json:"channel_id,omitempty"` GuildID string `json:"guild_id,omitempty"` }
A MessageReference is an attribution of a previous message. https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-structure
func (*MessageReference) ID ¶
func (ref *MessageReference) ID() string
ID returns the first non-empty of ref.MessageID or ref.ResponseMessageID.
type MessageType ¶
type MessageType int
MessageType is an enumeration of message types. https://discord.com/developers/docs/resources/channel#message-object-message-types
const (
MessageTypeDefault MessageType = 0
)
Valid message types. https://discord.com/developers/docs/resources/channel#message-object-message-types
type OAuth2Options ¶
type OAuth2Options struct { // BaseURL specifies the client's base URL. If nil, the Discord API URL // documented in https://discord.com/developers/docs/reference#api-reference-base-url // is used. BaseURL *url.URL // Scopes is the set of scopes to request. Scopes []string // State is the unique string described in // https://discord.com/developers/docs/topics/oauth2#state-and-security State string Permissions Permissions RedirectURI string }
type Reaction ¶
Reaction is an emoji reaction to a https://discord.com/developers/docs/resources/channel#reaction-object
type SelectOption ¶
type SelectOption struct { Label string `json:"label"` Value string `json:"value"` Description string `json:"description,omitempty"` Emoji *Emoji `json:"emoji,omitempty"` Default bool `json:"default,omitempty"` }
SelectOption is a single choice in a select menu component. https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure
type User ¶
type User struct { ID string `json:"id"` Username string `json:"username"` Discriminator string `json:"discriminator"` Avatar string `json:"avatar,omitempty"` Bot bool `json:"bot"` System bool `json:"system"` MFAEnabled bool `json:"mfa_enabled"` Locale string `json:"locale,omitempty"` Verified bool `json:"verified"` Email string `json:"email,omitempty"` }
https://discord.com/developers/docs/resources/user#user-object
Directories
¶
Path | Synopsis |
---|---|
Package fakediscord provides an in-memory implementation of the Discord API.
|
Package fakediscord provides an in-memory implementation of the Discord API. |
Package snowflake provides utilities for operating on Snowflake IDs.
|
Package snowflake provides utilities for operating on Snowflake IDs. |