Files
mygoTgChanBot/internal/config/config.go

78 lines
1.4 KiB
Go

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
}
// IsSuperAdmin 检查是否为超级管理员(配置文件中的管理员)
func (c *Config) IsSuperAdmin(userID int64) bool {
for _, id := range c.Admins {
if id == userID {
return true
}
}
return false
}