Files
oslstats/internal/discord/config.go
2026-03-05 18:32:55 +11:00

63 lines
1.8 KiB
Go

// Package discord provides utilities for interacting with the discord API
package discord
import (
"strings"
"git.haelnorr.com/h/golib/env"
"github.com/pkg/errors"
)
type Config struct {
ClientID string `ezconf:"DISCORD_CLIENT_ID,required,description:Discord application client ID"`
ClientSecret string `ezconf:"DISCORD_CLIENT_SECRET,required,description:Discord application client secret"`
RedirectPath string `ezconf:"DISCORD_REDIRECT_PATH,required,description:Path for the OAuth redirect handler"`
BotToken string `ezconf:"DISCORD_BOT_TOKEN,required,description:Token for the discord bot"`
GuildID string `ezconf:"DISCORD_GUILD_ID,required,description:ID for the discord server the bot should connect to"`
OAuthScopes string // Authorisation scopes for OAuth
}
func ConfigFromEnv() (any, error) {
cfg := &Config{
ClientID: env.String("DISCORD_CLIENT_ID", ""),
ClientSecret: env.String("DISCORD_CLIENT_SECRET", ""),
OAuthScopes: getOAuthScopes(),
RedirectPath: env.String("DISCORD_REDIRECT_PATH", ""),
BotToken: env.String("DISCORD_BOT_TOKEN", ""),
GuildID: env.String("DISCORD_GUILD_ID", ""),
}
// Check required fields
if cfg.ClientID == "" {
return nil, errors.New("Envar not set: DISCORD_CLIENT_ID")
}
if cfg.ClientSecret == "" {
return nil, errors.New("Envar not set: DISCORD_CLIENT_SECRET")
}
if cfg.RedirectPath == "" {
return nil, errors.New("Envar not set: DISCORD_REDIRECT_PATH")
}
if cfg.BotToken == "" {
return nil, errors.New("Envar not set: DISCORD_BOT_TOKEN")
}
if cfg.GuildID == "" {
return nil, errors.New("Envar not set: DISCORD_GUILD_ID")
}
return cfg, nil
}
func getOAuthScopes() string {
list := []string{
"connections",
"email",
"guilds",
"gdm.join",
"guilds.members.read",
"identify",
}
scopes := strings.Join(list, "+")
return scopes
}