diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-01-22 16:04:19 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-01-22 16:04:19 +0100 |
| commit | f4368074a7cc07ad305ae9fb9b08d6b8dd3025eb (patch) | |
| tree | 4fb677a7e41748d5db2ae57192f03d9443b909e5 | |
| parent | 94a11e552b50a389a4e3a6b5fe7bdea4ce7097a4 (diff) | |
feat(context): create custom with config data in
| -rw-r--r-- | common/config.go (renamed from config.go) | 4 | ||||
| -rw-r--r-- | common/context.go | 51 | ||||
| -rw-r--r-- | main.go | 17 |
3 files changed, 64 insertions, 8 deletions
diff --git a/config.go b/common/config.go index 725074c..07cff8d 100644 --- a/config.go +++ b/common/config.go @@ -1,4 +1,4 @@ -package main +package common import ( "fmt" @@ -54,7 +54,7 @@ func (c *Config) SetDefaultValues() { c.Database.SetDefaultValues() } -func getConfig(path string) (*Config, error) { +func LoadConfig(path string) (*Config, error) { b, err := os.ReadFile(path) if err != nil { return nil, err diff --git a/common/context.go b/common/context.go new file mode 100644 index 0000000..3a79264 --- /dev/null +++ b/common/context.go @@ -0,0 +1,51 @@ +package common + +import ( + "context" + + "gorm.io/gorm" +) + +type key uint8 + +const ( + keyDB key = 0 + keyDebug key = 1 + keyAuthor key = 2 +) + +func SetDB(ctx context.Context, db *gorm.DB) context.Context { + return context.WithValue(ctx, keyDB, db) +} + +func GetDB(ctx context.Context) *gorm.DB { + raw := ctx.Value(keyDB) + if raw == nil { + return nil + } + return raw.(*gorm.DB) +} + +func SetDebug(ctx context.Context, b bool) context.Context { + return context.WithValue(ctx, keyDebug, b) +} + +func IsDebug(ctx context.Context) bool { + raw := ctx.Value(keyDebug) + if raw == nil { + return false + } + return raw.(bool) +} + +func SetAuthor(ctx context.Context, s string) context.Context { + return context.WithValue(ctx, keyAuthor, s) +} + +func GetAuthor(ctx context.Context) string { + raw := ctx.Value(keyAuthor) + if raw == nil { + return "" + } + return raw.(string) +} @@ -18,7 +18,6 @@ import ( "git.anhgelus.world/anhgelus/les-copaings-bot/exp" "git.anhgelus.world/anhgelus/les-copaings-bot/rolereact" "git.anhgelus.world/anhgelus/les-copaings-bot/user" - "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/cmd" _ "github.com/joho/godotenv/autoload" discordgo "github.com/nyttikord/gokord" @@ -72,7 +71,7 @@ func init() { func main() { flag.Parse() - cfg, err := getConfig(cfgPath) + cfg, err := common.LoadConfig(cfgPath) if err != nil { panic(err) } @@ -90,6 +89,9 @@ func main() { adm := int64(discord.PermissionManageGuild) ctx := user.SetState(context.Background(), user.NewState()) + ctx = common.SetDB(ctx, db) + ctx = common.SetDebug(ctx, cfg.Debug) + ctx = common.SetAuthor(ctx, cfg.Author) /*rankCmd := cmd.New("rank", "Affiche le niveau d'un copaing"). AddOption(cmd.NewOption( @@ -162,7 +164,7 @@ func main() { events.AddHandler(func(ctx context.Context, s bot.Session, e *event.Ready) { guildID := "" logger := bot.Logger(ctx) - if gokord.Debug { + if common.IsDebug(ctx) { gs, err := s.GuildAPI().UserGuilds(1, "", "", false).Do(ctx) if err != nil { logger.Error("fetching guilds for debug", "error", err) @@ -273,16 +275,19 @@ func main() { } } -func setupTimers(ctx context.Context, dg bot.Session) { +func setupTimers(ctx context.Context, dg *discordgo.Session) { d := 24 * time.Hour - if gokord.Debug { + debug := common.IsDebug(ctx) + if debug { d = 3 * exp.DebugFactor * time.Second } d2 := 30 * time.Minute - if gokord.Debug { + if debug { d2 = 1 * exp.DebugFactor * time.Second } + // because logger was never set in this context + ctx = bot.SetLogger(ctx, dg.Logger()) user.PeriodicReducer(ctx, dg) stopPeriodicReducer = common.NewTimer(ctx, d, func(ctx context.Context, _ context.CancelFunc) { |
