diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-01-22 13:06:52 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-01-22 13:06:52 +0100 |
| commit | 42c25b54e169aeaf515522ba6103b1fe6db49be7 (patch) | |
| tree | 7703222680d3b7cc9cebfcbbdd1ac7dc57475591 | |
| parent | df72cc3cb9daf5f284bb62caddf77cf1f1ea4154 (diff) | |
refactor(config): replace old gokord config by custom one
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | config.go | 34 | ||||
| -rw-r--r-- | main.go | 54 | ||||
| -rw-r--r-- | rolereact/rolereact.go | 25 |
4 files changed, 52 insertions, 63 deletions
@@ -19,7 +19,7 @@ # vendor/ # Go workspace file -go.work +go.work* .idea .env @@ -2,8 +2,8 @@ package main import ( "fmt" + "os" - "github.com/anhgelus/gokord" "github.com/pelletier/go-toml/v2" "gorm.io/driver/postgres" "gorm.io/gorm" @@ -41,23 +41,12 @@ func (p *PostgresConfig) Connect() (*gorm.DB, error) { // generateDsn for the connection to postgres using the given config.SQLCredentials func (p *PostgresConfig) generateDsn() string { - return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Europe/Paris", + return fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Europe/Paris", p.Host, p.User, p.Password, p.DBName, p.Port, ) } -func (c *Config) IsDebug() bool { - return c.Debug -} - -func (c *Config) GetAuthor() string { - return c.Author -} - -func (c *Config) GetRedisCredentials() *gokord.RedisCredentials { - return nil -} - func (c *Config) SetDefaultValues() { c.Debug = false c.Author = "anhgelus" @@ -65,14 +54,11 @@ func (c *Config) SetDefaultValues() { c.Database.SetDefaultValues() } -func (c *Config) GetSQLCredentials() gokord.SQLCredentials { - return c.Database -} - -func (c *Config) Marshal() ([]byte, error) { - return toml.Marshal(c) -} - -func (c *Config) Unmarshal(bytes []byte) error { - return toml.Unmarshal(bytes, c) +func getConfig(path string) (*Config, error) { + b, err := os.ReadFile(path) + if err != nil { + return nil, err + } + var cfg Config + return &cfg, toml.Unmarshal(b, &cfg) } @@ -3,9 +3,7 @@ package main import ( "context" _ "embed" - "errors" "flag" - "log/slog" "os" "time" @@ -17,7 +15,7 @@ import ( "git.anhgelus.world/anhgelus/les-copaings-bot/user" "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/cmd" - "github.com/joho/godotenv" + _ "github.com/joho/godotenv/autoload" discordgo "github.com/nyttikord/gokord" "github.com/nyttikord/gokord/bot" "github.com/nyttikord/gokord/discord" @@ -30,7 +28,8 @@ import ( ) var ( - token string + token string + cfgPath string = "config.toml" //go:embed updates.json updatesData []byte Version = gokord.Version{ @@ -48,16 +47,13 @@ var ( var interTTF []byte func init() { - err := godotenv.Load() - if err != nil && !errors.Is(err, os.ErrNotExist) { - slog.Error("error while loading .env file", "error", err) - } flag.StringVar(&token, "token", os.Getenv("TOKEN"), "token of the bot") + flag.StringVar(&cfgPath, "config", cfgPath, "config's path") flag.BoolVar(&verbose, "v", verbose, "verbose") // Use a nicer font - fontTTF, parseErr := opentype.Parse(interTTF) - if parseErr != nil { + fontTTF, err := opentype.Parse(interTTF) + if err != nil { panic(err) } inter := font.Font{Typeface: "Inter"} @@ -66,23 +62,27 @@ func init() { Face: fontTTF, }}) plot.DefaultFont = inter - } func main() { flag.Parse() - gokord.UseRedis = false - err := gokord.SetupConfigs(&Config{}, []*gokord.ConfigInfo{}) + + cfg, err := getConfig(cfgPath) + if err != nil { + panic(err) + } + + db, err := cfg.Database.Connect() if err != nil { panic(err) } - err = gokord.DB.AutoMigrate(&user.Copaing{}, &config.GuildConfig{}, &config.XpRole{}, &user.CopaingXP{}, &config.RoleReactMessage{}, &config.RoleReact{}) + err = db.AutoMigrate(&user.Copaing{}, &config.GuildConfig{}, &config.XpRole{}, &user.CopaingXP{}, &config.RoleReactMessage{}, &config.RoleReact{}) if err != nil { panic(err) } - adm := gokord.AdminPermission + adm := int64(discord.PermissionManageGuild) ctx := user.SetState(context.Background(), user.NewState()) @@ -203,12 +203,13 @@ func main() { // related to rolereact // TEMP BECAUSE (OLD) GOKORD DOES NOT SUPPORT COMMAND MESSAGE - b.AddHandler(func(_ context.Context, s bot.Session, e *event.Ready) { + b.AddHandler(func(ctx context.Context, s bot.Session, e *event.Ready) { guildID := "" + logger := bot.Logger(ctx) if gokord.Debug { - gs, err := s.GuildAPI().UserGuilds(1, "", "", false) + gs, err := s.GuildAPI().UserGuilds(1, "", "", false).Do(ctx) if err != nil { - s.Logger().Error("fetching guilds for debug", "error", err) + logger.Error("fetching guilds for debug", "error", err) return } else { guildID = gs[0].ID @@ -220,24 +221,25 @@ func main() { Name: "Modifier", DefaultMemberPermissions: &adm, } - c, err := s.InteractionAPI().CommandCreate(s.SessionState().User().ID, guildID, &handleRolereactionMessageCmd) + c, err := s.InteractionAPI(). + CommandCreate(s.SessionState().User().ID, guildID, &handleRolereactionMessageCmd). + Do(ctx) if err != nil { - s.Logger().Error("unable to push rolereaction message command", "error", err) + logger.Error("unable to push rolereaction message command", "error", err) return } - s.Logger().Debug("pushed rolereaction message command", "CommandID", c.ID) + logger.Debug("pushed rolereaction message command", "CommandID", c.ID) }) b.AddHandler(func(ct context.Context, s bot.Session, _ *event.Disconnect) { user.PeriodicSaver(ctx, s) }) - b.AddHandler(func(_ context.Context, s bot.Session, i *event.InteractionCreate) { + b.AddHandler(func(ctx context.Context, s bot.Session, i *event.InteractionCreate) { if i.Type != types.InteractionApplicationCommand { return } - data := i.CommandData() - if data.Name == "Modifier" && data.CommandType == types.CommandMessage { - resp := cmd.NewResponseBuilder(s, i) - rolereact.HandleModifyCommand(s, i, data, resp) + cmd := i.Command() + if cmd.Data.Name == "Modifier" && cmd.Data.CommandType == types.CommandMessage { + rolereact.HandleModifyCommand(ctx, s, cmd) } }) b.AddHandler(rolereact.HandleReactionAdd) diff --git a/rolereact/rolereact.go b/rolereact/rolereact.go index 3d3ae47..6cb3b5c 100644 --- a/rolereact/rolereact.go +++ b/rolereact/rolereact.go @@ -1,6 +1,7 @@ package rolereact import ( + "context" "fmt" "slices" @@ -124,13 +125,13 @@ func HandleCommand( } func HandleModifyCommand( + ctx context.Context, s bot.Session, - i *event.InteractionCreate, - data *interaction.CommandInteractionData, - resp *cmd.ResponseBuilder, + cmd *interaction.ApplicationCommand, ) { - messageId := data.TargetID - cfg := GetGuildConfigPreloaded(i.GuildID) + logger := bot.Logger(ctx) + messageId := cmd.Data.TargetID + cfg := GetGuildConfigPreloaded(cmd.GuildID) var target *config.RoleReactMessage var targetEditID uint for editID, message := range messageEdits { @@ -146,15 +147,15 @@ func HandleModifyCommand( } } if target == nil { - err := s.InteractionAPI().Respond(i.Interaction, &interaction.Response{ + err := s.InteractionAPI().Respond(cmd.Interaction, &interaction.Response{ Type: types.InteractionResponseChannelMessageWithSource, Data: &interaction.ResponseData{ Flags: channel.MessageFlagsEphemeral, Content: "Le message sélectionné n'est pas un message de rôles de réaction.", }, - }) + }).Do(ctx) if err != nil { - s.Logger().Error("Unable to send rolereact message not found", "error", err) + logger.Error("Unable to send rolereact message not found", "error", err) } return } @@ -162,12 +163,12 @@ func HandleModifyCommand( targetEditID = messageCounter messageCounter++ } - err := s.InteractionAPI().Respond(i.Interaction, &interaction.Response{ + err := s.InteractionAPI().Respond(cmd.Interaction, &interaction.Response{ Type: types.InteractionResponseChannelMessageWithSource, - Data: MessageModifyData(i, &EditID{MessageEditID: targetEditID}), - }) + Data: MessageModifyData(cmd, &EditID{MessageEditID: targetEditID}), + }).Do(ctx) if err != nil { - s.Logger().Error("Unable to send modify rolereact message", "error", err) + logger.Error("Unable to send modify rolereact message", "error", err) } } |
