Documentation ¶
Overview ¶
Package gateway handles the Discord gateway (or Websocket) connection, its events, and everything related to it. This includes logging into the Websocket.
This package does not abstract events and function handlers; instead, it leaves that to the session package. This package exposes only a single Events channel.
Index ¶
- Constants
- Variables
- func GatewayURL() (string, error)
- func HandleEvent(g *Gateway, data []byte) error
- func HandleOP(g *Gateway, op *OP) error
- type ChannelCreateEvent
- type ChannelDeleteEvent
- type ChannelPinsUpdateEvent
- type ChannelUpdateEvent
- type Event
- type Gateway
- func (g *Gateway) Close() error
- func (g *Gateway) GuildSubscribe(data GuildSubscribeData) error
- func (g *Gateway) Heartbeat() error
- func (g *Gateway) Identify() error
- func (g *Gateway) Open() error
- func (g *Gateway) Reconnect() error
- func (g *Gateway) RequestGuildMembers(data RequestGuildMembersData) error
- func (g *Gateway) Resume() error
- func (g *Gateway) Send(code OPCode, v interface{}) error
- func (g *Gateway) Start() error
- func (g *Gateway) UpdateStatus(data UpdateStatusData) error
- func (g *Gateway) UpdateVoiceState(data UpdateVoiceStateData) error
- type GuildBanAddEvent
- type GuildBanRemoveEvent
- type GuildCreateEvent
- type GuildDeleteEvent
- type GuildEmojisUpdateEvent
- type GuildFolder
- type GuildIntegrationsUpdateEvent
- type GuildMemberAddEvent
- type GuildMemberRemoveEvent
- type GuildMemberUpdateEvent
- type GuildMembersChunkEvent
- type GuildRoleCreateEvent
- type GuildRoleDeleteEvent
- type GuildRoleUpdateEvent
- type GuildSubscribeData
- type GuildUpdateEvent
- type HeartbeatData
- type HelloEvent
- type Identifier
- type IdentifyData
- type IdentifyProperties
- type InvalidSessionEvent
- type MessageCreateEvent
- type MessageDeleteBulkEvent
- type MessageDeleteEvent
- type MessageReactionAddEvent
- type MessageReactionRemoveAllEvent
- type MessageReactionRemoveEvent
- type MessageUpdateEvent
- type OP
- type OPCode
- type Pacemaker
- type PresenceUpdateEvent
- type ReadyEvent
- type Relationship
- type RelationshipType
- type RequestGuildMembersData
- type ResumeData
- type ResumedEvent
- type Sequence
- type SettingsChannelOverride
- type Shard
- type TypingStartEvent
- type UpdateStatusData
- type UpdateVoiceStateData
- type UserGuildSettings
- type UserGuildSettingsUpdateEvent
- type UserSettings
- type UserSettingsUpdateEvent
- type UserUpdateEvent
- type VoiceServerUpdateEvent
- type VoiceStateUpdateEvent
- type WebhooksUpdateEvent
Constants ¶
const ( EndpointGateway = api.Endpoint + "gateway" EndpointGatewayBot = api.EndpointGateway + "/bot" Version = "6" Encoding = "json" )
Variables ¶
var ( // WSTimeout is the timeout for connecting and writing to the Websocket, // before Gateway cancels and fails. WSTimeout = wsutil.DefaultTimeout // WSBuffer is the size of the Event channel. This has to be at least 1 to // make space for the first Event: Ready or Resumed. WSBuffer = 10 // WSRetries is the times Gateway would try and connect or reconnect to the // gateway. WSRetries = uint(5) // WSError is the default error handler WSError = func(err error) {} // WSFatal is the default fatal handler, which is called when the Gateway // can't recover. WSFatal = func(err error) { log.Fatalln("Gateway failed:", err) } // WSExtraReadTimeout is the duration to be added to Hello, as a read // timeout for the websocket. WSExtraReadTimeout = time.Second )
var ( ErrMissingForResume = errors.New( "missing session ID or sequence for resuming") ErrWSMaxTries = errors.New("max tries reached") )
var ErrDead = errors.New("no heartbeat replied")
var ErrInvalidSession = errors.New("Invalid session")
var EventCreator = map[string]func() Event{ "HELLO": func() Event { return new(HelloEvent) }, "READY": func() Event { return new(ReadyEvent) }, "RESUMED": func() Event { return new(ResumedEvent) }, "INVALID_SESSION": func() Event { return new(InvalidSessionEvent) }, "CHANNEL_CREATE": func() Event { return new(ChannelCreateEvent) }, "CHANNEL_UPDATE": func() Event { return new(ChannelUpdateEvent) }, "CHANNEL_DELETE": func() Event { return new(ChannelDeleteEvent) }, "CHANNEL_PINS_UPDATE": func() Event { return new(ChannelPinsUpdateEvent) }, "GUILD_CREATE": func() Event { return new(GuildCreateEvent) }, "GUILD_UPDATE": func() Event { return new(GuildUpdateEvent) }, "GUILD_DELETE": func() Event { return new(GuildDeleteEvent) }, "GUILD_BAN_ADD": func() Event { return new(GuildBanAddEvent) }, "GUILD_BAN_REMOVE": func() Event { return new(GuildBanRemoveEvent) }, "GUILD_EMOJIS_UPDATE": func() Event { return new(GuildEmojisUpdateEvent) }, "GUILD_INTEGRATIONS_UPDATE": func() Event { return new(GuildIntegrationsUpdateEvent) }, "GUILD_MEMBER_ADD": func() Event { return new(GuildMemberAddEvent) }, "GUILD_MEMBER_REMOVE": func() Event { return new(GuildMemberRemoveEvent) }, "GUILD_MEMBER_UPDATE": func() Event { return new(GuildMemberUpdateEvent) }, "GUILD_MEMBERS_CHUNK": func() Event { return new(GuildMembersChunkEvent) }, "GUILD_ROLE_CREATE": func() Event { return new(GuildRoleCreateEvent) }, "GUILD_ROLE_UPDATE": func() Event { return new(GuildRoleUpdateEvent) }, "GUILD_ROLE_DELETE": func() Event { return new(GuildRoleDeleteEvent) }, "MESSAGE_CREATE": func() Event { return new(MessageCreateEvent) }, "MESSAGE_UPDATE": func() Event { return new(MessageUpdateEvent) }, "MESSAGE_DELETE": func() Event { return new(MessageDeleteEvent) }, "MESSAGE_DELETE_BULK": func() Event { return new(MessageDeleteBulkEvent) }, "MESSAGE_REACTION_ADD": func() Event { return new(MessageReactionAddEvent) }, "MESSAGE_REACTION_REMOVE": func() Event { return new(MessageReactionRemoveEvent) }, "MESSAGE_REACTION_REMOVE_ALL": func() Event { return new(MessageReactionRemoveAllEvent) }, "PRESENCE_UPDATE": func() Event { return new(PresenceUpdateEvent) }, "TYPING_START": func() Event { return new(TypingStartEvent) }, "USER_UPDATE": func() Event { return new(UserUpdateEvent) }, "VOICE_STATE_UPDATE": func() Event { return new(VoiceStateUpdateEvent) }, "VOICE_SERVER_UPDATE": func() Event { return new(VoiceServerUpdateEvent) }, "WEBHOOKS_UPDATE": func() Event { return new(WebhooksUpdateEvent) }, }
var Identity = IdentifyProperties{ OS: runtime.GOOS, Browser: "Arikawa", Device: "Arikawa", }
Identity is used as the default identity when initializing a new Gateway.
Functions ¶
func GatewayURL ¶
func HandleEvent ¶
Types ¶
type ChannelPinsUpdateEvent ¶
type ChannelPinsUpdateEvent struct { GuildID discord.Snowflake `json:"guild_id,omitempty"` ChannelID discord.Snowflake `json:"channel_id,omitempty"` LastPin discord.Timestamp `json:"timestamp,omitempty"` }
https://discordapp.com/developers/docs/topics/gateway#channels
type Event ¶
type Event = interface{}
Event is any event struct. They have an "Event" suffixed to them.
type Gateway ¶
type Gateway struct { WS *wsutil.Websocket json.Driver // Timeout for connecting and writing to the Websocket, uses default // WSTimeout (global). WSTimeout time.Duration // Retries on connect and reconnect. WSRetries uint // 3 // All events sent over are pointers to Event structs (structs suffixed with // "Event"). This shouldn't be accessed if the Gateway is created with a // Session. Events chan Event SessionID string Identifier *Identifier Pacemaker *Pacemaker Sequence *Sequence ErrorLog func(err error) // default to log.Println FatalLog func(err error) // called when the WS can't reconnect and resume // If this channel is non-nil, all incoming OP packets will also be sent // here. This should be buffered, so to not block the main loop. OP chan Event // contains filtered or unexported fields }
func NewGateway ¶
NewGateway starts a new Gateway with the default stdlib JSON driver. For more information, refer to NewGatewayWithDriver.
func NewGatewayWithDriver ¶
NewGatewayWithDriver connects to the Gateway and authenticates automatically.
func (*Gateway) GuildSubscribe ¶
func (g *Gateway) GuildSubscribe(data GuildSubscribeData) error
func (*Gateway) RequestGuildMembers ¶
func (g *Gateway) RequestGuildMembers(data RequestGuildMembersData) error
func (*Gateway) Resume ¶
Resume sends to the Websocket a Resume OP, but it doesn't actually resume from a dead connection. Start() resumes from a dead connection.
func (*Gateway) Start ¶
Start authenticates with the websocket, or resume from a dead Websocket connection. This function doesn't block.
func (*Gateway) UpdateStatus ¶
func (g *Gateway) UpdateStatus(data UpdateStatusData) error
func (*Gateway) UpdateVoiceState ¶
func (g *Gateway) UpdateVoiceState(data UpdateVoiceStateData) error
type GuildBanAddEvent ¶
type GuildBanAddEvent struct { GuildID discord.Snowflake `json:"guild_id"` User discord.User `json:"user"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildBanRemoveEvent ¶
type GuildBanRemoveEvent struct { GuildID discord.Snowflake `json:"guild_id"` User discord.User `json:"user"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildCreateEvent ¶
type GuildCreateEvent struct { discord.Guild Joined discord.Timestamp `json:"timestamp,omitempty"` Large bool `json:"large,omitempty"` MemberCount uint64 `json:"member_count,omitempty"` VoiceStates []discord.VoiceState `json:"voice_state,omitempty"` Members []discord.Member `json:"members,omitempty"` Channels []discord.Channel `json:"channel,omitempty"` Presences []discord.Presence `json:"presences,omitempty"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildDeleteEvent ¶
type GuildDeleteEvent struct { ID discord.Snowflake `json:"id"` Unavailable bool `json:"unavailable"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildEmojisUpdateEvent ¶
type GuildEmojisUpdateEvent struct { GuildID discord.Snowflake `json:"guild_id"` Emojis []discord.Emoji `json:"emoji"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildFolder ¶ added in v0.0.2
type GuildFolder struct { Name string `json:"name"` ID int64 `json:"id"` GuildIDs []string `json:"guild_ids"` Color int64 `json:"color"` }
GuildFolder holds a single folder that you see in the left guild panel.
type GuildMemberRemoveEvent ¶
type GuildMemberRemoveEvent struct { GuildID discord.Snowflake `json:"guild_id"` User discord.User `json:"user"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildMemberUpdateEvent ¶
type GuildMemberUpdateEvent struct { GuildID discord.Snowflake `json:"guild_id"` RoleIDs []discord.Snowflake `json:"roles"` User discord.User `json:"user"` Nick string `json:"nick"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
func (GuildMemberUpdateEvent) Update ¶
func (u GuildMemberUpdateEvent) Update(m *discord.Member)
type GuildMembersChunkEvent ¶
type GuildMembersChunkEvent struct { GuildID discord.Snowflake `json:"guild_id"` Members []discord.Member `json:"members"` // Whatever's not found goes here NotFound []string `json:"not_found,omitempty"` // Only filled if requested Presences []discord.Presence `json:"presences,omitempty"` }
GuildMembersChunkEvent is sent when Guild Request Members is called.
type GuildRoleCreateEvent ¶
type GuildRoleCreateEvent struct { GuildID discord.Snowflake `json:"guild_id"` Role discord.Role `json:"role"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildRoleDeleteEvent ¶
type GuildRoleDeleteEvent struct { GuildID discord.Snowflake `json:"guild_id"` RoleID discord.Snowflake `json:"role_id"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildRoleUpdateEvent ¶
type GuildRoleUpdateEvent struct { GuildID discord.Snowflake `json:"guild_id"` Role discord.Role `json:"role"` }
https://discordapp.com/developers/docs/topics/gateway#guilds
type GuildSubscribeData ¶
type GuildSubscribeData struct { GuildID discord.Snowflake `json:"guild_id"` Typing bool `json:"typing"` Activities bool `json:"activities"` }
Undocumented
type HelloEvent ¶
type HelloEvent struct {
HeartbeatInterval discord.Milliseconds `json:"heartbeat_interval"`
}
https://discordapp.com/developers/docs/topics/gateway#connecting-and-resuming
type Identifier ¶
type Identifier struct { IdentifyData IdentifyShortLimit *rate.Limiter `json:"-"` IdentifyGlobalLimit *rate.Limiter `json:"-"` }
func DefaultIdentifier ¶
func DefaultIdentifier(token string) *Identifier
func NewIdentifier ¶
func NewIdentifier(data IdentifyData) *Identifier
type IdentifyData ¶
type IdentifyData struct { Token string `json:"token"` Properties IdentifyProperties `json:"properties"` Compress bool `json:"compress,omitempty"` // true LargeThreshold uint `json:"large_threshold,omitempty"` // 50 GuildSubscription bool `json:"guild_subscriptions"` // true Shard *Shard `json:"shard,omitempty"` // [ shard_id, num_shards ] Presence *UpdateStatusData `json:"presence,omitempty"` }
func (*IdentifyData) SetShard ¶
func (i *IdentifyData) SetShard(id, num int)
type IdentifyProperties ¶
type IdentifyProperties struct { // Required OS string `json:"os"` // GOOS Browser string `json:"browser"` // Arikawa Device string `json:"device"` // Arikawa // Optional BrowserUserAgent string `json:"browser_user_agent,omitempty"` BrowserVersion string `json:"browser_version,omitempty"` OsVersion string `json:"os_version,omitempty"` Referrer string `json:"referrer,omitempty"` ReferringDomain string `json:"referring_domain,omitempty"` }
type InvalidSessionEvent ¶
type InvalidSessionEvent bool
InvalidSessionEvent indicates if the event is resumable.
type MessageDeleteBulkEvent ¶
type MessageDeleteBulkEvent struct { IDs []discord.Snowflake `json:"ids"` ChannelID discord.Snowflake `json:"channel_id"` GuildID discord.Snowflake `json:"guild_id,omitempty"` }
https://discordapp.com/developers/docs/topics/gateway#messages
type MessageDeleteEvent ¶
type MessageDeleteEvent struct { ID discord.Snowflake `json:"id"` ChannelID discord.Snowflake `json:"channel_id"` GuildID discord.Snowflake `json:"guild_id,omitempty"` }
https://discordapp.com/developers/docs/topics/gateway#messages
type MessageReactionAddEvent ¶
type MessageReactionAddEvent struct { UserID discord.Snowflake `json:"user_id"` ChannelID discord.Snowflake `json:"channel_id"` MessageID discord.Snowflake `json:"message_id"` Emoji discord.Emoji `json:"emoji,omitempty"` GuildID discord.Snowflake `json:"guild_id,omitempty"` Member *discord.Member `json:"member,omitempty"` }
https://discordapp.com/developers/docs/topics/gateway#messages
type MessageReactionRemoveEvent ¶
type MessageReactionRemoveEvent struct { UserID discord.Snowflake `json:"user_id"` ChannelID discord.Snowflake `json:"channel_id"` MessageID discord.Snowflake `json:"message_id"` Emoji discord.Emoji `json:"emoji"` GuildID discord.Snowflake `json:"guild_id,omitempty"` }
https://discordapp.com/developers/docs/topics/gateway#messages
type OP ¶
type OP struct { Code OPCode `json:"op"` Data json.Raw `json:"d,omitempty"` // Only for Dispatch (op 0) Sequence int64 `json:"s,omitempty"` EventName string `json:"t,omitempty"` }
func AssertEvent ¶
type Pacemaker ¶
type Pacemaker struct { // Heartrate is the received duration between heartbeats. Heartrate time.Duration SentBeat time.Time EchoBeat time.Time // Any callback that returns an error will stop the pacer. Pace func() error // Event OnDead func() error // contains filtered or unexported fields }
func (*Pacemaker) StartAsync ¶
type PresenceUpdateEvent ¶
Clients may only update their game status 5 times per 20 seconds.
type ReadyEvent ¶
type ReadyEvent struct { Version int `json:"version"` User discord.User `json:"user"` SessionID string `json:"session_id"` PrivateChannels []discord.Channel `json:"private_channels"` Guilds []discord.Guild `json:"guilds"` Shard *Shard `json:"shard"` // Undocumented fields Settings *UserSettings `json:"user_settings"` UserGuildSettings []UserGuildSettings `json:"user_guild_settings"` Relationships []Relationship `json:"relationships"` Presences []discord.Presence `json:"presences,omitempty"` Notes map[discord.Snowflake]string `json:"notes,omitempty"` }
type Relationship ¶ added in v0.0.2
type Relationship struct { ID string `json:"id"` User discord.User `json:"user"` Type RelationshipType `json:"type"` }
A Relationship between the logged in user and Relationship.User
type RelationshipType ¶ added in v0.0.2
type RelationshipType uint8
const ( FriendRelationship RelationshipType BlockedRelationship IncomingFriendRequest SentFriendRequest )
type RequestGuildMembersData ¶
type ResumeData ¶
type ResumedEvent ¶
type ResumedEvent struct{}
https://discordapp.com/developers/docs/topics/gateway#connecting-and-resuming
type Sequence ¶
type Sequence struct {
// contains filtered or unexported fields
}
func NewSequence ¶
func NewSequence() *Sequence
type SettingsChannelOverride ¶ added in v0.0.2
type SettingsChannelOverride struct { Muted bool `json:"muted"` MessageNotifications int `json:"message_notifications"` // TODO: document ChannelID discord.Snowflake `json:"channel_id"` }
A UserGuildSettingsChannelOverride stores data for a channel override for a users guild settings.
type TypingStartEvent ¶
type TypingStartEvent struct { ChannelID discord.Snowflake `json:"channel_id"` UserID discord.Snowflake `json:"user_id"` Timestamp discord.UnixTimestamp `json:"timestamp"` GuildID discord.Snowflake `json:"guild_id,omitempty"` Member *discord.Member `json:"member,omitempty"` }
https://discordapp.com/developers/docs/topics/gateway#presence
type UpdateStatusData ¶
type UpdateVoiceStateData ¶
type UserGuildSettings ¶ added in v0.0.2
type UserGuildSettings struct { SupressEveryone bool `json:"suppress_everyone"` Muted bool `json:"muted"` MobilePush bool `json:"mobile_push"` MessageNotifications int `json:"message_notifications"` GuildID discord.Snowflake `json:"guild_id"` ChannelOverrides []SettingsChannelOverride `json:"channel_overrides"` }
A UserGuildSettings stores data for a users guild settings.
type UserGuildSettingsUpdateEvent ¶ added in v0.0.2
type UserGuildSettingsUpdateEvent UserGuildSettings
Undocumented
type UserSettings ¶ added in v0.0.2
type UserSettings struct { ShowCurrentGame bool `json:"show_current_game"` DefaultGuildsRestricted bool `json:"default_guilds_restricted"` InlineAttachmentMedia bool `json:"inline_attachment_media"` InlineEmbedMedia bool `json:"inline_embed_media"` GIFAutoPlay bool `json:"gif_auto_play"` RenderEmbeds bool `json:"render_embeds"` RenderReactions bool `json:"render_reactions"` AnimateEmoji bool `json:"animate_emoji"` EnableTTSCommand bool `json:"enable_tts_command"` MessageDisplayCompact bool `json:"message_display_compact"` ConvertEmoticons bool `json:"convert_emoticons"` ExplicitContentFilter uint8 `json:"explicit_content_filter"` // ??? DisableGamesTab bool `json:"disable_games_tab"` DeveloperMode bool `json:"developer_mode"` DetectPlatformAccounts bool `json:"detect_platform_accounts"` StreamNotification bool `json:"stream_notification_enabled"` AccessibilityDetection bool `json:"allow_accessbility_detection"` ContactSync bool `json:"contact_sync_enabled"` NativePhoneIntegration bool `json:"native_phone_integration_enabled"` Locale string `json:"locale"` Theme string `json:"theme"` GuildPositions []discord.Snowflake `json:"guild_positions"` GuildFolders []GuildFolder `json:"guild_folders"` RestrictedGuilds []discord.Snowflake `json:"restricted_guilds"` FriendSourceFlags struct { All bool `json:"all"` MutualGuilds bool `json:"mutual_guilds"` MutualFriends bool `json:"mutual_friends"` } `json:"friend_source_flags"` Status discord.Status `json:"status"` CustomStatus struct { Text string `json:"text"` ExpiresAt discord.Timestamp `json:"expires_at,omitempty"` EmojiID discord.Snowflake `json:"emoji_id,string"` EmojiName string `json:"emoji_name"` } `json:"custom_status"` }
type UserSettingsUpdateEvent ¶ added in v0.0.2
type UserSettingsUpdateEvent UserSettings
Undocumented
type VoiceServerUpdateEvent ¶
type VoiceStateUpdateEvent ¶
type VoiceStateUpdateEvent discord.VoiceState
type WebhooksUpdateEvent ¶
type WebhooksUpdateEvent struct { GuildID discord.Snowflake `json:"guild_id"` ChannelID discord.Snowflake `json:"channel_id"` }
https://discordapp.com/developers/docs/topics/gateway#webhooks