gokord

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MPL-2.0 Imports: 22 Imported by: 0

README

Common

Common code shared between my bots.

Features

  • Creation of the bot (commands, status, handlers)
  • Basic config
  • Redis connection
  • Postgres connection
  • Various useful things (logger, timers)

Technologies

  • Go 1.22
  • go-redis
  • pelletier/go-toml
  • GORM

Documentation

Index

Constants

View Source
const (
	GameStatus      = 0
	WatchStatus     = 1
	StreamingStatus = 2
	ListeningStatus = 3

	AdminPermission int64 = discordgo.PermissionManageServer // AdminPermission of the command
)
View Source
const (
	ConfigFolder = "config"
)

Variables

View Source
var (
	Debug = true

	ErrBadStatusType     = errors.New("bad status type, please use the constant")
	ErrStatusUrlNotFound = errors.New("status url not found")
)
View Source
var (
	//go:embed resources/config.toml
	DefaultBaseConfig string

	// BaseCfg is the BaseConfig used by the bot
	BaseCfg BaseConfig

	ErrImpossibleToConnectDB    = errors.New("impossible to connect to the database")
	ErrImpossibleToConnectRedis = errors.New("impossible to connect to redis")
)
View Source
var DB *gorm.DB

DB used

View Source
var (
	ErrNilClient = errors.New("redis.NewClient is nil")
)
View Source
var (
	ErrSubsAreNil = errors.New("subs are nil in general handler")
)

Functions

func Get

func Get(cfg any, defaultConfig string, name string) error

Get a config (already called on start)

func SetupConfigs

func SetupConfigs(cfgInfo []*ConfigInfo) error

SetupConfigs with the given configs (+ base config which is available at BaseCfg)

Types

type BaseConfig

type BaseConfig struct {
	Debug    bool                `toml:"debug"`
	Author   string              `toml:"author"`
	Redis    RedisCredentials    `toml:"redis"`
	Database DatabaseCredentials `toml:"database"`
}

BaseConfig is all basic configuration (debug, redis connection and database connection)

type Bot

type Bot struct {
	Token    string            // Token of the Bot
	Status   []*Status         // Status of the Bot
	Commands []*GeneralCommand // Commands of the Bot, use NewCommand to create easily a new command
	//Handlers []interface{} // Handlers of the Bot
	AfterInit   func(s *discordgo.Session) // AfterInit is called after the initialization process of the Bot
	Version     *Version
	Innovations []*Innovation
	Name        string
}

Bot is the representation of a discord bot

func (*Bot) Start

func (b *Bot) Start()

Start the Bot (blocking instruction)

type BotData added in v0.5.0

type BotData struct {
	gorm.Model
	Version string `gorm:"version"`
	Name    string `gorm:"name"`
}

func (*BotData) Load added in v0.5.0

func (b *BotData) Load() error

func (*BotData) Save added in v0.5.0

func (b *BotData) Save() error

type Cmd

type Cmd struct {
	*discordgo.ApplicationCommand
	Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) // Handler called
	Subs    []*SimpleSubCmd
}

Cmd is a discordgo.ApplicationCommand + its handler

Use AdminPermission to set the admin permission

type ConfigInfo

type ConfigInfo struct {
	Cfg     any    // pointer to the struct
	Name    string // name of the config
	Default string // default content of the config
}

ConfigInfo has all required information to get a config

type DataBase added in v0.3.0

type DataBase interface {
	Load() error // Load data from the database
	Save() error // Save data into the database
}

DataBase is an interface with basic methods to load and save data

type DatabaseCredentials

type DatabaseCredentials struct {
	Host     string `toml:"host"`
	User     string `toml:"user"`
	Password string `toml:"password"`
	DBName   string `toml:"db_name"`
	Port     int    `toml:"port"`
}

func (*DatabaseCredentials) Connect

func (dc *DatabaseCredentials) Connect() (*gorm.DB, error)

Connect to the postgres database using the given config.DatabaseCredentials

type GCommandChoice

type GCommandChoice struct {
	Name  string
	Value interface{}
}

GCommandChoice represents a generic choice of GCommandOption

func NewChoice

func NewChoice(name string, value interface{}) *GCommandChoice

NewChoice creates a new choice for GCommandOption

func (*GCommandChoice) ToDiscordChoice

ToDiscordChoice turns GCommandChoice into a discordgo.ApplicationCommandOptionChoice (internal use of the API only)

type GCommandOption

type GCommandOption struct {
	Type        discordgo.ApplicationCommandOptionType
	Name        string
	Description string
	Required    bool
	Choices     []*GCommandChoice
}

GCommandOption represents a generic option of GeneralCommand

func NewOption

func NewOption(t discordgo.ApplicationCommandOptionType, name string, description string) *GCommandOption

NewOption creates a new GCommandOption

func (*GCommandOption) AddChoice

func (o *GCommandOption) AddChoice(c *GCommandChoice) *GCommandOption

AddChoice to the GCommandOption

func (*GCommandOption) IsRequired

func (o *GCommandOption) IsRequired() *GCommandOption

IsRequired informs that the GCommandOption is required

func (*GCommandOption) ToDiscordOption

func (o *GCommandOption) ToDiscordOption() *discordgo.ApplicationCommandOption

ToDiscordOption turns GCommandOption into a discordgo.ApplicationCommandOption (internal use of the API only)

type GeneralCommand

type GeneralCommand struct {
	HasSub      bool
	IsSub       bool
	Name        string
	Permission  *int64
	CanDM       bool
	Description string
	Options     []*GCommandOption
	Subs        []*GeneralCommand
	Handler     func(s *discordgo.Session, i *discordgo.InteractionCreate) // Handler called
}

GeneralCommand represents a generic command

func NewCommand

func NewCommand(name string, description string) *GeneralCommand

NewCommand creates a GeneralCommand

func (*GeneralCommand) AddOption

func (c *GeneralCommand) AddOption(s *GCommandOption) *GeneralCommand

AddOption to the GeneralCommand (also call HasOption)

func (*GeneralCommand) AddSub

AddSub to the GeneralCommand (also call ContainsSub)

func (*GeneralCommand) ContainsSub

func (c *GeneralCommand) ContainsSub() *GeneralCommand

ContainsSub makes the GeneralCommand able to contain subcommands

func (*GeneralCommand) DM

func (c *GeneralCommand) DM() *GeneralCommand

DM makes the GeneralCommand used in DM

func (*GeneralCommand) HasOption

func (c *GeneralCommand) HasOption() *GeneralCommand

HasOption makes the GeneralCommand able to contains GCommandOption

func (*GeneralCommand) Is added in v0.4.0

Is returns true if the GeneralCommand is approximately the same as *discordgo.ApplicationCommand

func (*GeneralCommand) SetHandler

func (c *GeneralCommand) SetHandler(handler func(s *discordgo.Session, i *discordgo.InteractionCreate)) *GeneralCommand

SetHandler of the GeneralCommand (if GeneralCommand contains subcommand, it will never be called)

func (*GeneralCommand) SetPermission

func (c *GeneralCommand) SetPermission(p *int64) *GeneralCommand

SetPermission of the GeneralCommand

func (*GeneralCommand) ToCmd

func (c *GeneralCommand) ToCmd() *Cmd

ToCmd turns GeneralCommand into a Cmd (internal use of the API only)

func (*GeneralCommand) ToSubCmd

func (c *GeneralCommand) ToSubCmd() *SubCmd

ToSubCmd turns GeneralCommand into a SubCmd (internal use of the API only)

type Innovation added in v0.5.0

type Innovation struct {
	Version  *Version            `json:"version"`
	Commands *InnovationCommands `json:"commands"`
}

func LoadInnovationFromJson added in v0.5.0

func LoadInnovationFromJson(b []byte) ([]*Innovation, error)

LoadInnovationFromJson provided (could be embedded with go/embed)

type InnovationCommands added in v0.5.0

type InnovationCommands struct {
	Added   []string `json:"added"`
	Removed []string `json:"removed"`
	Updated []string `json:"updated"`
}

func GetCommandsUpdate added in v0.5.0

func GetCommandsUpdate(bot *Bot) *InnovationCommands

type RedisBase added in v0.3.0

type RedisBase interface {
	GenKey(key string) string // GenKey generates the key to use
}

RedisBase is an interface helping use of redis to store/cache data

type RedisCredentials

type RedisCredentials struct {
	Address  string `toml:"address"`
	Password string `toml:"password"`
	DB       int    `toml:"db"`
}
var (
	// Credentials of redis
	Credentials RedisCredentials
	// Ctx background
	Ctx = context.Background()
)

func (*RedisCredentials) Get

func (rc *RedisCredentials) Get() (*redis.Client, error)

Get the redis.Client with the given RedisCredentials

type RedisUser added in v0.3.0

type RedisUser struct {
	RedisBase
	DiscordID string
	GuildID   string
}

RedisUser is the default implementation of RedisBase for a Discord User

func (*RedisUser) GenKey added in v0.3.0

func (p *RedisUser) GenKey(key string) string

type SimpleSubCmd

type SimpleSubCmd struct {
	Name    string
	Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) // Handler called
}

SimpleSubCmd is for the internal use of the API

type Status

type Status struct {
	Type    int    // Type of the Status (use GameStatus or WatchStatus or StreamingStatus or ListeningStatus)
	Content string // Content of the Status
	Url     string // Url of the StreamingStatus
}

Status contains all required information for updating the status

type SubCmd

type SubCmd struct {
	*discordgo.ApplicationCommandOption
	Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) // Handler called
}

SubCmd is for the internal use of the API

func (*SubCmd) ToSimple

func (s *SubCmd) ToSimple() *SimpleSubCmd

ToSimple turns SubCmd into a SimpleSubCmd

type Version added in v0.5.0

type Version struct {
	Major      uint
	Minor      uint
	Patch      uint
	PreRelease string
}

func ParseVersion added in v0.5.0

func ParseVersion(s string) (*Version, error)

func (*Version) ForSort added in v0.5.0

func (v *Version) ForSort(o *Version) int

ForSort returns:

  • 0 if o and v are the same version
  • 1 if v is newer than o
  • -1 if o is newer than v

func (*Version) Is added in v0.5.0

func (v *Version) Is(o *Version) bool

Is check if this is the same version

func (*Version) NewerThan added in v0.5.0

func (v *Version) NewerThan(o *Version) bool

NewerThan check if the version is newer than version o

Does not support pre-release checks

func (*Version) SetMajor added in v0.5.0

func (v *Version) SetMajor(m uint) *Version

func (*Version) SetMinor added in v0.5.0

func (v *Version) SetMinor(m uint) *Version

func (*Version) SetPatch added in v0.5.0

func (v *Version) SetPatch(p uint) *Version

func (*Version) SetPreRelease added in v0.5.0

func (v *Version) SetPreRelease(p string) *Version

func (*Version) String added in v0.5.0

func (v *Version) String() string

func (*Version) UpdateBotVersion added in v0.5.0

func (v *Version) UpdateBotVersion(bot *Bot)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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