dshardmanager

package
v2.29.2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: MIT Imports: 8 Imported by: 0

README

dshardmanager

Simple shard manager for discord bots

Status:

  • Core funcitonality, add handlers, log shard events to discord and to custom outputs
  • Fancy status message in discord that gets updated live
  • Use the recommded shard count by discord
  • Warn when getting close to the cap
  • Automatically re-scale the sharding when needed Needed being when a shard with +2500 guilds disconnects and fails to resume, this shard will no longer be able to identify afaik
  • Simple api? maybe in an extras package.

Documentation

Index

Constants

View Source
const (
	VersionMajor = 0
	VersionMinor = 2
	VersionPath  = 0
)

Variables

View Source
var (
	VersionString = strconv.Itoa(VersionMajor) + "." + strconv.Itoa(VersionMinor) + "." + strconv.Itoa(VersionPath)
)

Functions

This section is empty.

Types

type Event

type Event struct {
	Type EventType

	Shard     int
	NumShards int

	Msg string

	// When this event occured
	Time time.Time
}

Event holds data for an event

func (*Event) String

func (c *Event) String() string

type EventType

type EventType int
const (
	// Sent when the connection to the gateway was established
	EventConnected EventType = iota

	// Sent when the connection is lose
	EventDisconnected

	// Sent when the connection was sucessfully resumed
	EventResumed

	// Sent on ready
	EventReady

	// Sent when Open() is called
	EventOpen

	// Sent when Close() is called
	EventClose

	// Sent when an error occurs
	EventError
)

func (EventType) String

func (c EventType) String() string

type Manager

type Manager struct {
	sync.RWMutex

	// Name of the bot, to appear before log messages as a prefix
	// and in the title of the updated status message
	Name string

	// All the shard sessions
	Sessions []*discordgo.Session

	// If set logs connection status events to this channel
	LogChannel int64

	// If set keeps an updated satus message in this channel
	StatusMessageChannel int64

	// The function that provides the guild counts per shard, used fro the updated status message
	// Should return a slice of guild counts, with the index being the shard number
	GuildCountsFunc func() []int

	// Called on events, by default this is set to a function that logs it to log.Printf
	// You can override this if you want another behaviour, or just set it to nil for nothing.
	OnEvent func(e *Event)

	// SessionFunc creates a new session and returns it, override the default one if you have your own
	// session settings to apply
	SessionFunc SessionFunc
	// contains filtered or unexported fields
}

func New

func New(token string) *Manager

New creates a new shard manager with the defaults set, after you have created this you call Manager.Start To start connecting dshardmanager.New("Bot asd", OptLogChannel(someChannel), OptLogEventsToDiscord(true, true))

func (*Manager) AddHandler

func (m *Manager) AddHandler(handler interface{})

Adds an event handler to all shards All event handlers will be added to new sessions automatically.

func (*Manager) GetFullStatus

func (m *Manager) GetFullStatus() *Status

GetFullStatus retrieves the full status at this instant

func (*Manager) GetNumShards

func (m *Manager) GetNumShards() int

GetNumShards returns the current set number of shards

func (*Manager) GetRecommendedCount

func (m *Manager) GetRecommendedCount() (int, error)

GetRecommendedCount gets the recommended sharding count from discord, this will also set the shard count internally if called Should not be called after calling Start(), will have undefined behaviour

func (*Manager) Init

func (m *Manager) Init() error

Init initializesthe manager, retreiving the recommended shard count if needed and initalizes all the shards

func (*Manager) LogConnectionEventStd

func (m *Manager) LogConnectionEventStd(e *Event)

LogConnectionEventStd is the standard connection event logger, it logs it to whatever log.output is set to.

func (*Manager) OnDiscordConnected

func (m *Manager) OnDiscordConnected(s *discordgo.Session, evt *discordgo.Connect)

func (*Manager) OnDiscordDisconnected

func (m *Manager) OnDiscordDisconnected(s *discordgo.Session, evt *discordgo.Disconnect)

func (*Manager) OnDiscordReady

func (m *Manager) OnDiscordReady(s *discordgo.Session, evt *discordgo.Ready)

func (*Manager) OnDiscordResumed

func (m *Manager) OnDiscordResumed(s *discordgo.Session, evt *discordgo.Resumed)

func (*Manager) Session

func (m *Manager) Session(shardID int) *discordgo.Session

Session retrieves a session from the sessions map, rlocking it in the process

func (*Manager) SessionForGuild

func (m *Manager) SessionForGuild(guildID int64) *discordgo.Session

SessionForGuild returns the session for the specified guild

func (*Manager) SessionForGuildS

func (m *Manager) SessionForGuildS(guildID string) *discordgo.Session

SessionForGuildS is the same as SessionForGuild but accepts the guildID as a string for convenience

func (*Manager) SetNumShards

func (m *Manager) SetNumShards(n int)

SetNumShards sets the number of shards to use, if you want to override the recommended count Should not be called after calling Start(), will panic

func (*Manager) Start

func (m *Manager) Start() error

Start starts the shard manager, opening all gateway connections

func (*Manager) StdGuildCountsFunc

func (m *Manager) StdGuildCountsFunc() []int

StdGuildsFunc uses the standard states to return the guilds

func (*Manager) StdSessionFunc

func (m *Manager) StdSessionFunc(token string) (*discordgo.Session, error)

StdSessionFunc is the standard session provider, it does nothing to the actual session

func (*Manager) StopAll

func (m *Manager) StopAll() (err error)

StopAll stops all the shard sessions and returns the last error that occured

type SessionFunc

type SessionFunc func(token string) (*discordgo.Session, error)

type ShardStatus

type ShardStatus struct {
	Shard     int                     `json:"shard"`
	Status    discordgo.GatewayStatus `json:"status"`
	Started   bool                    `json:"started"`
	NumGuilds int                     `json:"num_guilds"`
}

type Status

type Status struct {
	Shards    []*ShardStatus `json:"shards"`
	NumGuilds int            `json:"num_guilds"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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