This commit is contained in:
nquidox 2025-03-23 15:35:24 +03:00
commit 7c610d0477
11 changed files with 399 additions and 0 deletions

69
config/config.go Normal file
View file

@ -0,0 +1,69 @@
package config
import (
"github.com/disgoorg/snowflake/v2"
log "github.com/sirupsen/logrus"
"os"
"strconv"
)
type Config struct {
AppConf AppConfig
TgConf TelegramConfig
DsConf DiscordConfig
}
type AppConfig struct {
LogLvl string
}
type TelegramConfig struct {
Token string
ChatID int64
}
type DiscordConfig struct {
Token string
GuildID snowflake.ID
ChannelID snowflake.ID
}
func NewConfig() *Config {
return &Config{
AppConfig{
LogLvl: "info",
},
TelegramConfig{
Token: getEnv("TELEGRAM_TOKEN", ""),
ChatID: convertChatID(getEnv("TELEGRAM_CHANNEL_ID", "")),
},
DiscordConfig{
Token: getEnv("DISCORD_TOKEN", ""),
GuildID: convertID(getEnv("GUILD_ID", "")),
ChannelID: convertID(getEnv("CHANNEL_ID", "")),
},
}
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func convertID(channelIDStr string) snowflake.ID {
channelIDUint, err := strconv.ParseUint(channelIDStr, 10, 64)
if err != nil {
log.Fatal("Cannot convert channel ID to snowflake ID")
}
return snowflake.ID(channelIDUint)
}
func convertChatID(str string) int64 {
id, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Fatal("Cannot convert string to int64")
}
return id
}

35
config/logging.go Normal file
View file

@ -0,0 +1,35 @@
package config
import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"path"
"runtime"
)
func LogSetup(lvl string) {
l, err := log.ParseLevel(lvl)
if err != nil {
log.SetLevel(log.DebugLevel)
}
log.SetFormatter(
&log.TextFormatter{
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(" %s:%d", filename, f.Line)
},
},
)
if l == log.DebugLevel {
log.SetLevel(l)
log.SetReportCaller(true)
} else {
log.SetLevel(l)
}
log.SetOutput(os.Stdout)
}