etfapi

package
v18.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBadData = errors.New("bad data")

ErrBadData is the error returned when upserting State elements if data is an incorrect type or invalid value

View Source
var ErrBadElementData = errors.New("bad element data")

ErrBadElementData is the error returned when attempting to create a new element but the data provided does not match the ETFCode

View Source
var ErrBadFieldType = errors.New("bad field type")

ErrBadFieldType is the error returned when attempting to unmarshal an etf payload and a field is an incorrect type (like non-string-like map keys)

View Source
var ErrBadMarshalData = errors.New("bad marshal data")

ErrBadMarshalData is the error returned when attempting to marshal an etf payload to []byte and the data in an Element doesn't match the ETFCode

View Source
var ErrBadParity = errors.New("non-even list parity")

ErrBadParity is the error returned when a list that should be even parity is not (usually when trying to deal with Map Elements)

View Source
var ErrBadPayload = errors.New("bad payload format")

ErrBadPayload is the error returned when attempting to unmarshal an etf payload fails due to bad formatting

View Source
var ErrBadTarget = errors.New("bad element unmarshal target")

ErrBadTarget is the error returned when trying to convert an Element to an incorrect type

View Source
var ErrMissingData = errors.New("missing data")

ErrMissingData is the error returned when upserting State elements is missing required fields

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is the error returned when upserting State elements and the element to update is not found

View Source
var ErrOutOfBounds = errors.New("int value out of bounds")

ErrOutOfBounds is the error returned when integer values are out of the bounds of the type code

Functions

func MapAndIDFromElement

func MapAndIDFromElement(e Element) (map[string]Element, snowflake.Snowflake, error)

MapAndIDFromElement converts a Map element into a string->Element map and attempts to extract an id Snowflake from the "id" field

func SnowflakeFromElement

func SnowflakeFromElement(e Element) (snowflake.Snowflake, error)

SnowflakeFromElement converts a number-like Element into a Snowflake

Types

type Channel

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

Channel represents known information about a discord channel

func ChannelFromElement

func ChannelFromElement(e Element) (Channel, error)

ChannelFromElement creates a new Channel object from the given etf Element. The element should be a Map-type Element

func ChannelFromElementMap

func ChannelFromElementMap(eMap map[string]Element) (Channel, error)

ChannelFromElementMap creates a new Channel object from the given data map.

func (*Channel) ID

func (c *Channel) ID() snowflake.Snowflake

ID returns the channel's ID

func (*Channel) UpdateFromElementMap

func (c *Channel) UpdateFromElementMap(eMap map[string]Element) error

UpdateFromElementMap updates information about the channel This will not remove known data, only replace it

type ChannelType

type ChannelType int

ChannelType represents the type of a discord channel

const (
	GuildTextChannel     ChannelType = 0
	DMChannel            ChannelType = 1
	GuildVoiceChannel    ChannelType = 2
	GroupDMChannel       ChannelType = 3
	GuildCategoryChannel ChannelType = 4
)

These are the known discord channel types

func ChannelTypeFromElement

func ChannelTypeFromElement(e Element) (ChannelType, error)

ChannelTypeFromElement extracts the channel type from a etf Element

func (ChannelType) String

func (t ChannelType) String() string

type ETFCode

type ETFCode int

ETFCode is a type alias for representing ETF type codes

const (
	Map       ETFCode = 116
	Atom      ETFCode = 100
	List      ETFCode = 108
	Binary    ETFCode = 109
	Int8      ETFCode = 97
	Int32     ETFCode = 98
	Float     ETFCode = 70
	String    ETFCode = 107
	EmptyList ETFCode = 106
	SmallBig  ETFCode = 110
	LargeBig  ETFCode = 111
)

These are the ETF type codes that this library knows about

func (ETFCode) IsCollection

func (c ETFCode) IsCollection() bool

IsCollection determines if an ETFCode is a collection of other elements

func (ETFCode) IsList

func (c ETFCode) IsList() bool

IsList determines if an ETFCode is a list

func (ETFCode) IsNumeric

func (c ETFCode) IsNumeric() bool

IsNumeric determines if an ETFCode is number-like

func (ETFCode) IsStringish

func (c ETFCode) IsStringish() bool

IsStringish determines if an ETFCode is string-like

func (ETFCode) String

func (c ETFCode) String() string

type Element

type Element struct {
	Code ETFCode
	Val  []byte
	Vals []Element
}

Element is a container for arbitrary etf-formatted data

func ElementMapToElementSlice

func ElementMapToElementSlice(m map[string]Element) ([]Element, error)

ElementMapToElementSlice converts an string->Element map into a slice of Elements (kv pairs)

func NewAtomElement

func NewAtomElement(val []byte) (Element, error)

NewAtomElement generates a new Element representing an Atom value

func NewBasicElement

func NewBasicElement(code ETFCode, val []byte) (Element, error)

NewBasicElement generates a new Element to hold data for non-collection types.

func NewBinaryElement

func NewBinaryElement(val []byte) (Element, error)

NewBinaryElement generates a new Element representing Binary data

func NewBoolElement

func NewBoolElement(val bool) (Element, error)

NewBoolElement generates a new Element representing a boolean value

func NewCollectionElement

func NewCollectionElement(code ETFCode, val []Element) (Element, error)

NewCollectionElement generates a new Element to hold data for collection types.

func NewInt32Element

func NewInt32Element(val int) (Element, error)

NewInt32Element generates a new Element representing a 32-bit unsigned integer value; Bounds checking will happen inside the function

func NewInt8Element

func NewInt8Element(val int) (Element, error)

NewInt8Element generates a new Element representing an 8-bit unsigned integer value; Bounds checking will happen inside the function.

func NewListElement

func NewListElement(val []Element) (Element, error)

NewListElement generates a new Element representing a List

NOTE: empty lists are likely not handled well

func NewMapElement

func NewMapElement(val map[string]Element) (Element, error)

NewMapElement generates a new Element representing a Map

Keys are encoded as Binary type elements

func NewNilElement

func NewNilElement() (Element, error)

NewNilElement generates a new Element representing "nil"

func NewSmallBigElement

func NewSmallBigElement(val int64) (Element, error)

NewSmallBigElement generates a new Element representing a 64-bit unsigned integer value

func NewStringElement

func NewStringElement(val string) (Element, error)

NewStringElement generates a new Element representing a String value

func (*Element) IsCollection

func (e *Element) IsCollection() bool

IsCollection determines if an element is a collection (map or list)

func (*Element) IsFalse

func (e *Element) IsFalse() bool

IsFalse determines if an element represents a "false" value

func (*Element) IsList

func (e *Element) IsList() bool

IsList determines if an element is a list (with or without members)

func (*Element) IsNil

func (e *Element) IsNil() bool

IsNil determines if an element represents a "nil" value

func (*Element) IsNumeric

func (e *Element) IsNumeric() bool

IsNumeric determines if an element is number-like

func (*Element) IsStringish

func (e *Element) IsStringish() bool

IsStringish determines if an element is string-like

func (*Element) IsTrue

func (e *Element) IsTrue() bool

IsTrue determines if an element represents a "true" value

func (*Element) Marshal

func (e *Element) Marshal() ([]byte, error)

Marshal formats the data in the given element in etf binary format

func (*Element) MarshalTo

func (e *Element) MarshalTo(b io.Writer) error

MarshalTo formats the data in the given element in etf binary format and writes it to the provided writer

func (Element) String

func (e Element) String() string

func (*Element) ToBytes

func (e *Element) ToBytes() ([]byte, error)

ToBytes converts a string-like Element to a []byte, if possible

func (*Element) ToInt

func (e *Element) ToInt() (int, error)

ToInt converts an int-like Element to an int, if possible

func (*Element) ToInt64

func (e *Element) ToInt64() (int64, error)

ToInt64 converts an int-like Element to an int64, if possible

func (*Element) ToList

func (e *Element) ToList() ([]Element, error)

ToList converts a list Element to a list

func (*Element) ToMap

func (e *Element) ToMap() (map[string]Element, error)

ToMap converts a map Element to a map

func (*Element) ToString

func (e *Element) ToString() (string, error)

ToString converts a string-like element to a real string, if possible

type Guild

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

Guild represents the known data about a discord guild

func GuildFromElement

func GuildFromElement(e Element) (Guild, error)

GuildFromElement creates a new Guild object from the given Element

func GuildFromElementMap

func GuildFromElementMap(eMap map[string]Element) (Guild, error)

GuildFromElementMap creates a new Guild object from the given data

func (*Guild) ChannelWithName

func (g *Guild) ChannelWithName(name string) (snowflake.Snowflake, bool)

ChannelWithName finds the channel id for the channel with the provided name

func (*Guild) HasRole

func (g *Guild) HasRole(uid, rid snowflake.Snowflake) bool

HasRole determines if the user with the provided ID has the role with the provided id

func (*Guild) ID

func (g *Guild) ID() snowflake.Snowflake

ID returns the guild ID

func (*Guild) IsAdmin

func (g *Guild) IsAdmin(uid snowflake.Snowflake) bool

IsAdmin determines if the user with the provided ID has administrator powers in the guild

func (*Guild) OwnsChannel

func (g *Guild) OwnsChannel(cid snowflake.Snowflake) bool

OwnsChannel determines if this guild owns a channel with the provided id

func (*Guild) RoleIsAdministrator added in v18.2.0

func (g *Guild) RoleIsAdministrator(rid snowflake.Snowflake) bool

RoleIsAdministrator determines if a role has admin powers in the guild

func (*Guild) RoleWithName

func (g *Guild) RoleWithName(name string) (snowflake.Snowflake, bool)

RoleWithName finds the role id for the role with the provided name

func (*Guild) UpdateFromElementMap

func (g *Guild) UpdateFromElementMap(eMap map[string]Element) error

UpdateFromElementMap updates information about the guild from the provided data

This will not delete data; it will only add and change data

func (*Guild) UpsertMemberFromElementMap

func (g *Guild) UpsertMemberFromElementMap(eMap map[string]Element) (GuildMember, error)

UpsertMemberFromElementMap upserts a GuildMemeber in the guild from the given data

func (*Guild) UpsertRoleFromElementMap

func (g *Guild) UpsertRoleFromElementMap(eMap map[string]Element) (Role, error)

UpsertRoleFromElementMap upserts a Role in the guild from the given data

type GuildMember

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

GuildMember represents the information about a known guild membership

func GuildMemberFromElement

func GuildMemberFromElement(e Element) (GuildMember, error)

GuildMemberFromElement generates a new GuildMember object from the given Element

func (*GuildMember) UpdateFromElementMap

func (m *GuildMember) UpdateFromElementMap(eMap map[string]Element) error

UpdateFromElementMap updates the information from the given data

This will not remove data; it will only add and change data

type Message

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

Message represents the data about a message in a discord channel

func MessageFromElement

func MessageFromElement(e Element) (Message, error)

MessageFromElement generates a new Message object from the given Element

func MessageFromElementMap

func MessageFromElementMap(eMap map[string]Element) (Message, error)

MessageFromElementMap generates a new Message object from the given data

func (*Message) AuthorID

func (m *Message) AuthorID() snowflake.Snowflake

AuthorID returns the ID of the author of the message

func (*Message) ChannelID

func (m *Message) ChannelID() snowflake.Snowflake

ChannelID returns the ID of the channel the message was sent to

func (*Message) ContentString

func (m *Message) ContentString() string

ContentString returns the content of the message

func (*Message) ID

func (m *Message) ID() snowflake.Snowflake

ID returns the ID of the message

func (*Message) MessageType

func (m *Message) MessageType() MessageType

MessageType returns the MessageType of the message

type MessageType

type MessageType int

MessageType represents the type of message received in a discord channel

const (
	DefaultMessage              MessageType = 0
	RecipientAddMessage         MessageType = 1
	RecipientRemoveMessage      MessageType = 2
	CallMessage                 MessageType = 3
	ChannelNameChangeMessage    MessageType = 4
	ChannelIconChangeMessage    MessageType = 5
	ChannelPinnedMessageMessage MessageType = 6
	GuildMemberJoinMessage      MessageType = 7
)

These are the known message types

func MessageTypeFromElement

func MessageTypeFromElement(e Element) (MessageType, error)

MessageTypeFromElement generates a MessageType representation from the given message-type Element

func (MessageType) String

func (t MessageType) String() string

type Payload

type Payload struct {
	OpCode    discordapi.OpCode
	SeqNum    *int
	EventName string

	Data     map[string]Element
	DataList []Element
}

Payload represents the data in a etf api payload (both for sending and receiving)

func Unmarshal

func Unmarshal(raw []byte) (*Payload, error)

Unmarshal creates a new Payload from the raw etf data in the []byte

func (*Payload) Marshal

func (p *Payload) Marshal() ([]byte, error)

Marshal converts a payload into a properly formatted []byte that can be sent over a websocket connection

func (Payload) String

func (p Payload) String() string

type Reaction

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

Message represents the data about a message in a discord channel

func ReactionFromElement

func ReactionFromElement(e Element) (Reaction, error)

ReactionFromElement generates a new Reaction object from the given Element

func ReactionFromElementMap

func ReactionFromElementMap(eMap map[string]Element) (Reaction, error)

ReactionFromElementMap generates a new Reaction object from the given data

func (*Reaction) ChannelID

func (r *Reaction) ChannelID() snowflake.Snowflake

ChannelID returns the ID of the channel the reaction was to

func (*Reaction) Emoji

func (r *Reaction) Emoji() string

Emoji returns the emoji of the reaction ChannelID returns the ID of the channel the reaction was to

func (*Reaction) GuildID

func (r *Reaction) GuildID() snowflake.Snowflake

GuildID returns the ID of the guild the reaction was to

func (*Reaction) MessageID

func (r *Reaction) MessageID() snowflake.Snowflake

MessageID returns the ID of the message the reaction was to

func (*Reaction) UserID

func (r *Reaction) UserID() snowflake.Snowflake

UserID returns the ID of the reactor

type Role

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

Role represents a discord guild role

func RoleFromElement

func RoleFromElement(e Element) (Role, error)

RoleFromElement generates a new Role object from the given Element

func (*Role) IsAdmin

func (r *Role) IsAdmin() bool

IsAdmin determines if a role is a server admin

func (*Role) UpdateFromElementMap

func (r *Role) UpdateFromElementMap(eMap map[string]Element) error

UpdateFromElementMap updates the data in a role from the given information

This will not remove data, only change and add data

type Session

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

Session represents a discord bot's session with an api gateway

The primary purpose of this wrapper is to wrap and lock access to a State field for safe access from multiple goroutines

func NewSession

func NewSession() *Session

NewSession creates a new session object in an unlocked state and with empty session id

func (*Session) ChannelName

func (s *Session) ChannelName(cid snowflake.Snowflake) (string, bool)

ChannelName returns the name of the channel with the provided id, if one is known

The second return value will be alse if not such channel was found

func (*Session) Guild

func (s *Session) Guild(gid snowflake.Snowflake) (Guild, bool)

Guild finds a guild with the given ID in the current session state, if it exists

The second return value will be false if no such guild was found

func (*Session) GuildIDs added in v18.0.7

func (s *Session) GuildIDs() []snowflake.Snowflake

GuildIDs finds all the currently stored guild ids in the session state

func (*Session) GuildOfChannel

func (s *Session) GuildOfChannel(cid snowflake.Snowflake) (snowflake.Snowflake, bool)

GuildOfChannel returns the id of the guild that owns the channel with the provided id, if one is known

The second return value will be false if no such guild was found

func (*Session) ID

func (s *Session) ID() string

ID returns the session id of the current session (or an empty string if an id has not been set)

func (*Session) IsGuildAdmin

func (s *Session) IsGuildAdmin(gid, uid snowflake.Snowflake) bool

IsGuildAdmin returns true if the user with the given uid has Admin powers in the guild with the given gid. If the guild is not found, this will return false

func (*Session) UpdateFromReady

func (s *Session) UpdateFromReady(p *Payload) error

UpdateFromReady updates data in the session state from a session ready message, and updates the session id

func (*Session) UpsertChannelFromElement

func (s *Session) UpsertChannelFromElement(e Element) (snowflake.Snowflake, error)

UpsertChannelFromElement updates data in the session state for a channel based on the given Element

func (*Session) UpsertChannelFromElementMap

func (s *Session) UpsertChannelFromElementMap(eMap map[string]Element) (snowflake.Snowflake, error)

UpsertChannelFromElementMap updates data in the session state for a channel based on the given data

func (*Session) UpsertGuildFromElement

func (s *Session) UpsertGuildFromElement(e Element) (snowflake.Snowflake, error)

UpsertGuildFromElement updates data in the session state for a guild based on the given Element

func (*Session) UpsertGuildFromElementMap

func (s *Session) UpsertGuildFromElementMap(eMap map[string]Element) (snowflake.Snowflake, error)

UpsertGuildFromElementMap updates data in the session state for a guild based on the given data

func (*Session) UpsertGuildMemberFromElementMap

func (s *Session) UpsertGuildMemberFromElementMap(eMap map[string]Element) (snowflake.Snowflake, error)

UpsertGuildMemberFromElementMap updates data in the session state for a guild member based on the given data

func (*Session) UpsertGuildRoleFromElementMap

func (s *Session) UpsertGuildRoleFromElementMap(eMap map[string]Element) (snowflake.Snowflake, error)

UpsertGuildRoleFromElementMap updates data in the session state for a guild role based on the given data

type User

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

User represents the data about a discord user

func UserFromElement

func UserFromElement(e Element) (User, error)

UserFromElement generates a new User object from the given Element

func (*User) UpdateFromElementMap

func (u *User) UpdateFromElementMap(eMap map[string]Element) error

UpdateFromElementMap updates the information about a user from the given data

This will not remove information, only change and add information

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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