This commit is contained in:
dela
2026-02-04 22:33:45 +08:00
commit d82badc6e3
16 changed files with 2261 additions and 0 deletions

67
internal/config/config.go Normal file
View 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
}