shards

package module
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2022 License: MIT Imports: 4 Imported by: 4

README

Shards Build Status CodeFactor

Configurable, scalable and automatic sharding library for discordgo.

Features

  • Automatic scaling: head-ache free configuration runs out of the box.
  • Zero-downtime restarts: make downtime a thing of the past.
  • Slash commands: integrate the latest in Discord functionality.
Installing

This assumes you already have a working Go environment, if not please install Go first.

go get github.com/servusdei2018/shards
Usage

Import the package into your project, like so:

import (
	"github.com/servusdei2018/shards"
)

Documentation

  • Go Reference

Documentation

Index

Constants

View Source
const (
	// TIMELIMIT specifies how long to pause between connecting shards.
	TIMELIMIT = time.Second * 5
	// VERSION specifies the shards module version. Follows semantic versioning (semver.org).
	VERSION = "1.2.6"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	sync.RWMutex

	// Discord gateway.
	Gateway *discordgo.Session
	// Discord intent.
	Intent discordgo.Intent
	// Shards managed by this Manager.
	Shards []*Shard
	// Total Shard count.
	ShardCount int
	// contains filtered or unexported fields
}

Manager facilitates the management of Shards.

func New

func New(token string) (mgr *Manager, err error)

New creates a new Manager with the recommended number of shards. After calling New, call Start to begin connecting the shards.

Example: mgr := shards.New("Bot TOKEN")

func (*Manager) AddHandler

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

AddHandler registers an event handler for all Shards.

func (*Manager) ApplicationCommandCreate added in v1.2.0

func (m *Manager) ApplicationCommandCreate(guildID string, cmd *discordgo.ApplicationCommand) (errs []error)

ApplicationCommandCreate registers an application command for all Shards.

func (*Manager) GuildCount

func (m *Manager) GuildCount() (count int)

GuildCount returns the amount of guilds that a Manager's Shards are handling.

func (*Manager) RegisterIntent

func (m *Manager) RegisterIntent(intent discordgo.Intent)

RegisterIntent sets the Intent for all Shards' sessions.

func (*Manager) Restart

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

Restart restarts the Manager, and rescales if necessary, all with zero downtime.

func (*Manager) SessionForDM

func (m *Manager) SessionForDM() *discordgo.Session

SessionForDM returns the proper session for sending and receiving DM's.

func (*Manager) SessionForGuild

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

SessionForGuild returns the proper session for the specified guild.

func (*Manager) SetShardCount added in v1.2.2

func (m *Manager) SetShardCount(count int)

SetShardCount sets the shard count. The new shard count won't take effect until the Manager is restarted.

func (*Manager) Shutdown

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

Shutdown gracefully terminates the Manager.

func (*Manager) Start

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

Start starts the Manager.

type Shard

type Shard struct {
	sync.RWMutex

	// The Discord session handling this Shard.
	Session *discordgo.Session
	// This Shard's ID.
	ID int
	// Total Shard count.
	ShardCount int
	// contains filtered or unexported fields
}

A Shard represents a shard.

func (*Shard) AddHandler

func (s *Shard) AddHandler(handler interface{})

AddHandler registers an event handler for a Shard.

Shouldn't be called after Init or results in undefined behavior.

func (*Shard) ApplicationCommandCreate added in v1.2.0

func (s *Shard) ApplicationCommandCreate(guildID string, cmd *discordgo.ApplicationCommand) error

ApplicationCommandCreate registers an application command for a Shard.

Shouldn't be called before Initialization.

func (*Shard) GuildCount

func (s *Shard) GuildCount() (count int)

GuildCount returns the amount of guilds that a Shard is handling.

func (*Shard) Init

func (s *Shard) Init(token string, ID, ShardCount int, intent discordgo.Intent) (err error)

Init initializes a shard with a bot token, its Shard ID, the total amount of shards, and a Discord intent.

func (*Shard) Stop

func (s *Shard) Stop() (err error)

Stop stops a shard.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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