common

package module
v0.0.0-...-eafa341 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MPL-2.0 Imports: 16 Imported by: 3

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.21
  • 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
)

Variables

View Source
var (
	Debug bool

	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 (
	ErrGuildIDDiscordIDNotPresent = errors.New("guild_id or discord_id not informed")
	ErrNilClient                  = errors.New("redis.NewClient is nil")
)

Ctx background

View Source
var DB *gorm.DB

DB used

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 {
	Main     Main
	Redis    RedisCredentials
	Database DatabaseCredentials
}

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 []Cmd         // Commands of the Bot
	Handlers []interface{} // Handlers of the Bot
}

Bot is the representation of a discord bot

func (*Bot) Start

func (b *Bot) Start()

Start the Bot

type Cmd

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

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 DatabaseCredentials

type DatabaseCredentials struct {
	Host     string
	User     string
	Password string
	DBName   string
	Port     int
}

func (*DatabaseCredentials) Connect

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

Connect to the postgres database using the given config.DatabaseCredentials

type Main

type Main struct {
	Debug bool
}

type RedisCredentials

type RedisCredentials struct {
	Address  string
	Password string
	DB       int
}
var Credentials RedisCredentials

Credentials of redis

func (*RedisCredentials) Get

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

Get the redis.Client with the given RedisCredentials

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 UserBase

type UserBase struct {
	DiscordID string
	GuildID   string
}

UserBase is the minimum struct required to store a used in redis

func (*UserBase) GenKey

func (p *UserBase) GenKey() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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