1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-09 16:35:12 +00:00
arikawa/gateway/commands.go

135 lines
3.5 KiB
Go
Raw Normal View History

2020-01-15 04:43:34 +00:00
package gateway
import (
2020-01-15 07:34:18 +00:00
"context"
2020-01-15 04:43:34 +00:00
"github.com/diamondburned/arikawa/discord"
2020-01-15 07:34:18 +00:00
"github.com/pkg/errors"
2020-01-15 04:43:34 +00:00
)
// Rules: VOICE_STATE_UPDATE -> VoiceStateUpdateEvent
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
2020-01-15 07:34:18 +00:00
Shard *Shard `json:"shard,omitempty"` // [ shard_id, num_shards ]
Presence *UpdateStatusData `json:"presence,omitempty"`
}
2020-01-15 04:43:34 +00:00
2020-01-15 07:34:18 +00:00
func (i *IdentifyData) SetShard(id, num int) {
if i.Shard == nil {
i.Shard = new(Shard)
}
i.Shard[0], i.Shard[1] = id, num
2020-01-15 04:43:34 +00:00
}
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"`
}
func (g *Gateway) Identify() error {
2020-01-15 07:34:18 +00:00
ctx, cancel := context.WithTimeout(context.Background(), g.WSTimeout)
defer cancel()
if err := g.Identifier.Wait(ctx); err != nil {
return errors.Wrap(err, "Can't wait for identify()")
}
return g.Send(IdentifyOP, g.Identifier)
2020-01-15 04:43:34 +00:00
}
type ResumeData struct {
Token string `json:"token"`
SessionID string `json:"session_id"`
Sequence int64 `json:"seq"`
}
2020-01-16 03:28:21 +00:00
// 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 (g *Gateway) Resume() error {
var (
ses = g.SessionID
seq = g.Sequence.Get()
)
if ses == "" || seq == 0 {
return ErrMissingForResume
}
return g.Send(ResumeOP, ResumeData{
Token: g.Identifier.Token,
SessionID: ses,
Sequence: seq,
})
}
2020-01-15 04:43:34 +00:00
// HeartbeatData is the last sequence number to be sent.
type HeartbeatData int
func (g *Gateway) Heartbeat() error {
return g.Send(HeartbeatOP, g.Sequence.Get())
}
type RequestGuildMembersData struct {
GuildID []discord.Snowflake `json:"guild_id"`
UserIDs []discord.Snowflake `json:"user_id,omitempty"`
Query string `json:"query,omitempty"`
Limit uint `json:"limit"`
Presences bool `json:"presences,omitempty"`
}
2020-01-18 22:35:07 +00:00
func (g *Gateway) RequestGuildMembers(data RequestGuildMembersData) error {
return g.Send(RequestGuildMembersOP, data)
}
2020-01-15 04:43:34 +00:00
type UpdateVoiceStateData struct {
GuildID discord.Snowflake `json:"guild_id"`
ChannelID discord.Snowflake `json:"channel_id"`
SelfMute bool `json:"self_mute"`
SelfDeaf bool `json:"self_deaf"`
}
2020-01-18 22:35:07 +00:00
func (g *Gateway) UpdateVoiceState(data UpdateVoiceStateData) error {
return g.Send(VoiceStateUpdateOP, data)
}
2020-01-15 04:43:34 +00:00
type UpdateStatusData struct {
Since discord.Milliseconds `json:"since,omitempty"` // 0 if not idle
2020-01-18 21:40:09 +00:00
Game *discord.Activity `json:"game,omitempty"` // nullable
2020-01-15 04:43:34 +00:00
2020-01-18 21:40:09 +00:00
Status discord.Status `json:"status"`
AFK bool `json:"afk"`
2020-01-15 04:43:34 +00:00
}
2020-01-18 22:35:07 +00:00
func (g *Gateway) UpdateStatus(data UpdateStatusData) error {
return g.Send(StatusUpdateOP, data)
}
// Undocumented
type GuildSubscribeData struct {
GuildID discord.Snowflake `json:"guild_id"`
Typing bool `json:"typing"`
Activities bool `json:"activities"`
}
func (g *Gateway) GuildSubscribe(data GuildSubscribeData) error {
return g.Send(GuildSubscriptionsOP, data)
}