forked from carrydela/mygoTgChanBot
first
This commit is contained in:
67
internal/config/config.go
Normal file
67
internal/config/config.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Bot BotConfig `yaml:"bot"`
|
||||
Admins []int64 `yaml:"admins"`
|
||||
Channel ChannelConfig `yaml:"channel"`
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
TOC TOCConfig `yaml:"toc"`
|
||||
}
|
||||
|
||||
type BotConfig struct {
|
||||
Token string `yaml:"token"`
|
||||
}
|
||||
|
||||
type ChannelConfig struct {
|
||||
ID int64 `yaml:"id"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Path string `yaml:"path"`
|
||||
}
|
||||
|
||||
type TOCConfig struct {
|
||||
DebounceSeconds int `yaml:"debounce_seconds"`
|
||||
}
|
||||
|
||||
func Load(path string) (*Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config: %w", err)
|
||||
}
|
||||
|
||||
if cfg.Bot.Token == "" {
|
||||
return nil, fmt.Errorf("bot token is required")
|
||||
}
|
||||
|
||||
if cfg.Database.Path == "" {
|
||||
cfg.Database.Path = "./data/bot.db"
|
||||
}
|
||||
|
||||
if cfg.TOC.DebounceSeconds <= 0 {
|
||||
cfg.TOC.DebounceSeconds = 3
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) IsAdmin(userID int64) bool {
|
||||
for _, id := range c.Admins {
|
||||
if id == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user