gateway

package
v0.1.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package gateway allows you to communicate with Discord.

We use pre-allocated buffers to prevent future allocations. You can adjust them using options.

Each gateway is limited to ONE data handler

WARNING: Payload pointer that is passed as first data handler argument is SHARED. This means that the gateway uses it for every message that comes through the websocket, and DO NOT SHARE this pointer to another thread (goroutine). Payload processing from A to Z must be synchronous for gateway to wait when payload is free to be reused.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PayloadTo

func PayloadTo[V any](p *Payload) (x V, err error)

Types

type ChangeVoiceStateOptions

type ChangeVoiceStateOptions struct {
	GuildID   snowflake.ID `json:"guild_id"`
	ChannelID snowflake.ID `json:"channel_id"`
	SelfMute  bool         `json:"self_mute"`
	SelfDeaf  bool         `json:"self_deaf"`
}

type ChannelPinsUpdateEvent

type ChannelPinsUpdateEvent struct {
	LastPinTimestamp timeconv.Timestamp `json:"last_pin_timestamp"`
	GuildID          snowflake.ID       `json:"guild_id"`
	ChannelID        snowflake.ID       `json:"channel_id"`
}

type Gateway

type Gateway struct {
	Logger golog.Logger
	// contains filtered or unexported fields
}

func New

func New(token string, shard uint16, totalShards uint16, options ...Option) *Gateway

func (*Gateway) ChangeVoiceState

func (g *Gateway) ChangeVoiceState(opts ChangeVoiceStateOptions) error

ChangeVoiceState method will send OP4 Voice State Update to the gateway.

You have to listen for VoiceStateUpdate and VoiceServerUpdate events by yourself or use a wrapper built-in to client package (client.ChangeVoiceState)

func (*Gateway) Connect

func (g *Gateway) Connect(ctx context.Context) (err error)

func (*Gateway) Disconnect

func (g *Gateway) Disconnect(code websocket.StatusCode)

func (*Gateway) EventChannel

func (g *Gateway) EventChannel() *broadcaster.Group[events.Event]

func (*Gateway) OnData

func (g *Gateway) OnData(fn func(data *Payload))

func (*Gateway) Presence

func (g *Gateway) Presence() PresenceSet

func (*Gateway) Reconnect

func (g *Gateway) Reconnect(ctx context.Context, resume bool) (err error)

func (*Gateway) RequestMembers

func (g *Gateway) RequestMembers(ctx context.Context, opts RequestMembers) (members []discord.MemberWithUser, presences []discord.BasePresence, err error)

type GuildBanEvent

type GuildBanEvent struct {
	User    discord.User `json:"user"`
	GuildID snowflake.ID `json:"guild_id"`
}

type GuildEmojisUpdateEvent

type GuildEmojisUpdateEvent struct {
	Emojis  []discord.Emoji `json:"emojis"`
	GuildID snowflake.ID    `json:"guild_id"`
}

type GuildRoleDeleteEvent

type GuildRoleDeleteEvent struct {
	GuildID snowflake.ID `json:"guild_id"`
	RoleID  snowflake.ID `json:"role_id"`
}

type GuildRoleEvent

type GuildRoleEvent struct {
	Role    discord.Role `json:"role"`
	GuildID snowflake.ID `json:"guild_id"`
}

type GuildScheduledUserEvent

type GuildScheduledUserEvent struct {
	EventID snowflake.ID `json:"guild_scheduled_event_id"`
	UserID  snowflake.ID `json:"user_id"`
	GuildID snowflake.ID `json:"guild_id"`
}

type Identify

type Identify struct {
	Properties     IdentifyProperties `json:"properties"`
	Token          string             `json:"token"`
	Shard          []uint16           `json:"shard"`
	Intents        intents.Intent     `json:"intents"`
	LargeThreshold uint16             `json:"large_threshold,omitempty"`
	Compress       bool               `json:"compress,omitempty"`
}

type IdentifyProperties

type IdentifyProperties struct {
	OS      string `json:"$os"`
	Browser string `json:"$browser"`
	Device  string `json:"$device"`
}

type IntegrationDeleteEvent

type IntegrationDeleteEvent struct {
	ID            snowflake.ID `json:"id"`
	GuildID       snowflake.ID `json:"guild_id"`
	ApplicationID snowflake.ID `json:"application_id"`
}

type InviteCreateEvent

type InviteCreateEvent struct {
	CreatedAt  timeconv.Timestamp `json:"created_at"`
	Code       string             `json:"code"`
	Inviter    discord.User       `json:"inviter"`
	TargetUser discord.User       `json:"target_user"`
	ChannelID  snowflake.ID       `json:"channel_id"`
	GuildID    snowflake.ID       `json:"guild_id"`
	MaxAge     int                `json:"max_age"`
	MaxUses    int                `json:"max_uses"`
	TargetType int                `json:"target_type"`
	Temporary  bool               `json:"temporary"`
}

type InviteDeleteEvent

type InviteDeleteEvent struct {
	Code      string       `json:"code"`
	ChannelID snowflake.ID `json:"channel_id"`
	GuildID   snowflake.ID `json:"guild_id"`
}

type MessageDeleteEvent

type MessageDeleteEvent struct {
	ChannelID snowflake.ID `json:"channel_id"`
	GuildID   snowflake.ID `json:"guild_id"`
	ID        snowflake.ID `json:"id"`
}

func (MessageDeleteEvent) Message

type MessageReactionAddEvent

type MessageReactionAddEvent struct {
	Member *discord.MemberWithUser `json:"member"`
	MessageReactionRemoveEvent
}

type MessageReactionRemoveAllEmojiEvent

type MessageReactionRemoveAllEmojiEvent struct {
	Emoji     discord.Emoji `json:"emoji"`
	ChannelID snowflake.ID  `json:"channel_id"`
	GuildID   snowflake.ID  `json:"guild_id"`
	MessageID snowflake.ID  `json:"message_id"`
}

type MessageReactionRemoveAllEvent

type MessageReactionRemoveAllEvent struct {
	ChannelID snowflake.ID `json:"channel_id"`
	GuildID   snowflake.ID `json:"guild_id"`
	MessageID snowflake.ID `json:"message_id"`
}

type MessageReactionRemoveEvent

type MessageReactionRemoveEvent struct {
	Emoji     discord.Emoji `json:"emoji"`
	UserID    snowflake.ID  `json:"user_id"`
	ChannelID snowflake.ID  `json:"channel_id"`
	GuildID   snowflake.ID  `json:"guild_id"`
	MessageID snowflake.ID  `json:"message_id"`
}

func (MessageReactionRemoveEvent) InGuild

func (v MessageReactionRemoveEvent) InGuild() bool

func (MessageReactionRemoveEvent) User

type OpCode

type OpCode uint8
const (
	DispatchOp OpCode = iota
	HeartbeatOp
	IdentifyOp
	PresenceUpdateOp
	VoiceStateUpdateOp
	ResumeOp OpCode = iota + 1
	ReconnectOp
	RequestGuildMembersOp
	InvalidSessionOp
	HelloOp
	HeartbeatAckOp
)

type Option

type Option func(v *Options)

func WithApiClient

func WithApiClient(client *api.Client) Option

func WithBufferSize

func WithBufferSize(size int) Option

func WithIntents

func WithIntents(intents intents.Intent) Option

func WithLogger

func WithLogger(log golog.Logger) Option

func WithProperties

func WithProperties(props IdentifyProperties) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

type Payload

type Payload struct {
	Event events.Event    `json:"t"`
	Data  json.RawMessage `json:"d"`
	Seq   uint64          `json:"s"`
	Shard uint16          `json:"-"`
	OP    OpCode          `json:"op"`
}

type PresenceSet

type PresenceSet interface {
	Set(status discord.PresenceStatus, ac discord.Activity)
	SetCustom(data PresenceUpdate)
}

type PresenceUpdate

type PresenceUpdate struct {
	Since      *uint64                `json:"since"`
	Status     discord.PresenceStatus `json:"status"`
	Activities []discord.Activity     `json:"activities"`
	AFK        bool                   `json:"afk"`
}

type ReadyEvent

type ReadyEvent struct {
	SessionID        string `json:"session_id"`
	ResumeGatewayURL string `json:"resume_gateway_url"`
	Guilds           []struct {
		ID snowflake.ID `json:"id"`
	} `json:"guilds"`
	Shard []uint16     `json:"shard"`
	User  discord.User `json:"user"`
}

type RequestMembers

type RequestMembers struct {
	Query     string         `json:"query"`
	Nonce     string         `json:"nonce,omitempty"`
	UserIDs   []snowflake.ID `json:"user_ids,omitempty"`
	GuildID   snowflake.ID   `json:"guild_id"`
	Limit     int            `json:"limit"`
	Presences bool           `json:"presences,omitempty"`
}

type RequestMembersResponse

type RequestMembersResponse struct {
	Nonce      string                   `json:"nonce"`
	Members    []discord.MemberWithUser `json:"members"`
	NotFound   []snowflake.ID           `json:"not_found"`
	Presences  []discord.Presence       `json:"presences"`
	ChunkIndex int                      `json:"chunk_index"`
	ChunkCount int                      `json:"chunk_count"`
	GuildID    snowflake.ID             `json:"guild_id"`
}

type Shard

type Shard struct {
	// contains filtered or unexported fields
}

func NewShard

func NewShard(gateway *Gateway) *Shard

func (*Shard) Gateway

func (v *Shard) Gateway() *Gateway

func (*Shard) ID

func (v *Shard) ID() uint16

func (*Shard) Latency

func (v *Shard) Latency() uint16

func (*Shard) LatencyHistory

func (v *Shard) LatencyHistory() []uint16

func (*Shard) SetStatus

func (v *Shard) SetStatus(status ShardStatus)

func (*Shard) Status

func (v *Shard) Status() ShardStatus

type ShardStatus

type ShardStatus uint8
const (
	ShardStatusDisconnected ShardStatus = iota + 1
	ShardStatusConnected
)

type ThreadListSyncEvent

type ThreadListSyncEvent struct {
	ChannelIDs []snowflake.ID         `json:"channel_ids"`
	Threads    []discord.Channel      `json:"threads"`
	Members    []discord.ThreadMember `json:"members"`
	GuildID    snowflake.ID           `json:"guild_id"`
}

type ThreadMembersUpdateEvent

type ThreadMembersUpdateEvent struct {
	AddedMembers     []discord.ThreadMember `json:"added_members"`
	RemovedMemberIDs []snowflake.ID         `json:"removed_member_ids"`
	ID               snowflake.ID           `json:"id"`
	GuildID          snowflake.ID           `json:"guild_id"`
	MemberCount      int                    `json:"member_count"`
}

type TypingStartEvent

type TypingStartEvent struct {
	Timestamp timeconv.Timestamp `json:"timestamp"`
	Member    discord.Member     `json:"member"`
	ChannelID snowflake.ID       `json:"channel_id"`
	GuildID   snowflake.ID       `json:"guild_id"`
	UserID    snowflake.ID       `json:"user_id"`
}

type UnavailableGuild

type UnavailableGuild struct {
	ID          snowflake.ID `json:"id"`
	Unavailable bool         `json:"unavailable"`
}

type VoiceServerUpdateEvent

type VoiceServerUpdateEvent struct {
	Token    string       `json:"token"`
	Endpoint string       `json:"endpoint"`
	GuildID  snowflake.ID `json:"guild_id"`
}

type VoiceStateUpdateEvent

type VoiceStateUpdateEvent = discord.VoiceState

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL