aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--commands/config.go14
-rw-r--r--commands/credits.go2
-rw-r--r--commands/rank.go2
-rw-r--r--commands/reset.go6
-rw-r--r--commands/top.go5
-rw-r--r--config.go80
-rw-r--r--go.mod5
-rw-r--r--go.sum16
-rw-r--r--main.go13
-rw-r--r--restart.sh3
-rw-r--r--updates.json10
11 files changed, 125 insertions, 31 deletions
diff --git a/commands/config.go b/commands/config.go
index a3857c6..8df8664 100644
--- a/commands/config.go
+++ b/commands/config.go
@@ -12,7 +12,7 @@ import (
func ConfigShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
cfg := config.GetGuildConfig(i.GuildID)
- resp := utils.ResponseBuilder{C: s, I: i}
+ resp := utils.NewResponseBuilder(s, i)
roles := ""
l := len(cfg.XpRoles) - 1
for i, r := range cfg.XpRoles {
@@ -80,8 +80,7 @@ func ConfigShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) {
optMap := utils.GenerateOptionMapForSubcommand(i)
- resp := utils.ResponseBuilder{C: s, I: i}
- resp.IsEphemeral()
+ resp := utils.NewResponseBuilder(s, i).IsEphemeral()
// verify every args
t, ok := optMap["type"]
if !ok {
@@ -214,8 +213,7 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) {
func ConfigChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
optMap := utils.GenerateOptionMapForSubcommand(i)
- resp := utils.ResponseBuilder{C: s, I: i}
- resp.IsEphemeral()
+ resp := utils.NewResponseBuilder(s, i).IsEphemeral()
// verify every args
t, ok := optMap["type"]
if !ok {
@@ -286,8 +284,7 @@ func ConfigChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
func ConfigFallbackChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
optMap := utils.GenerateOptionMapForSubcommand(i)
- resp := utils.ResponseBuilder{C: s, I: i}
- resp.IsEphemeral()
+ resp := utils.NewResponseBuilder(s, i).IsEphemeral()
// verify every args
salon, ok := optMap["channel"]
if !ok {
@@ -329,8 +326,7 @@ func ConfigFallbackChannel(s *discordgo.Session, i *discordgo.InteractionCreate)
func ConfigPeriodBeforeReduce(s *discordgo.Session, i *discordgo.InteractionCreate) {
optMap := utils.GenerateOptionMapForSubcommand(i)
- resp := utils.ResponseBuilder{C: s, I: i}
- resp.IsEphemeral()
+ resp := utils.NewResponseBuilder(s, i).IsEphemeral()
// verify every args
days, ok := optMap["days"]
if !ok {
diff --git a/commands/credits.go b/commands/credits.go
index d5aa42e..62bbcac 100644
--- a/commands/credits.go
+++ b/commands/credits.go
@@ -6,7 +6,7 @@ import (
)
func Credits(s *discordgo.Session, i *discordgo.InteractionCreate) {
- resp := utils.ResponseBuilder{C: s, I: i}
+ resp := utils.NewResponseBuilder(s, i)
err := resp.Embeds([]*discordgo.MessageEmbed{
{
Type: discordgo.EmbedTypeRich,
diff --git a/commands/rank.go b/commands/rank.go
index 70c0222..d577299 100644
--- a/commands/rank.go
+++ b/commands/rank.go
@@ -14,7 +14,7 @@ func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
msg := "Votre niveau"
m := i.Member
var err error
- resp := utils.ResponseBuilder{C: s, I: i}
+ resp := utils.NewResponseBuilder(s, i)
if v, ok := optMap["copaing"]; ok {
u := v.UserValue(s)
if u.Bot {
diff --git a/commands/reset.go b/commands/reset.go
index 3f8af03..20b15ea 100644
--- a/commands/reset.go
+++ b/commands/reset.go
@@ -10,15 +10,13 @@ import (
func Reset(s *discordgo.Session, i *discordgo.InteractionCreate) {
var copaings []*user.Copaing
gokord.DB.Where("guild_id = ?", i.GuildID).Delete(&copaings)
- resp := utils.ResponseBuilder{C: s, I: i}
- if err := resp.IsEphemeral().Message("L'XP a été reset.").Send(); err != nil {
+ if err := utils.NewResponseBuilder(s, i).IsEphemeral().Message("L'XP a été reset.").Send(); err != nil {
utils.SendAlert("commands/reset.go - Sending success (all)", err.Error())
}
}
func ResetUser(s *discordgo.Session, i *discordgo.InteractionCreate) {
- resp := utils.ResponseBuilder{C: s, I: i}
- resp.IsEphemeral()
+ resp := utils.NewResponseBuilder(s, i).IsEphemeral()
optMap := utils.GenerateOptionMap(i)
v, ok := optMap["user"]
if !ok {
diff --git a/commands/top.go b/commands/top.go
index 53ccef9..5a20091 100644
--- a/commands/top.go
+++ b/commands/top.go
@@ -11,13 +11,12 @@ import (
)
func Top(s *discordgo.Session, i *discordgo.InteractionCreate) {
- resp := utils.ResponseBuilder{C: s, I: i}
- err := resp.IsDeferred().Send()
+ resp := utils.NewResponseBuilder(s, i).IsDeferred()
+ err := resp.Send()
if err != nil {
utils.SendAlert("commands/top.go - Sending deferred", err.Error())
return
}
- resp.NotDeferred().IsEdit()
embeds := make([]*discordgo.MessageEmbed, 3)
wg := sync.WaitGroup{}
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..421f1b1
--- /dev/null
+++ b/config.go
@@ -0,0 +1,80 @@
+package main
+
+import (
+ "fmt"
+ "github.com/anhgelus/gokord"
+ "github.com/pelletier/go-toml/v2"
+ "gorm.io/driver/postgres"
+ "gorm.io/gorm"
+)
+
+type Config struct {
+ Debug bool `toml:"debug"`
+ Author string `toml:"author"`
+ Redis *gokord.RedisCredentials `toml:"redis"`
+ Database *PostgresConfig `toml:"database"`
+}
+
+type PostgresConfig struct {
+ Host string `toml:"host"`
+ User string `toml:"user"`
+ Password string `toml:"password"`
+ DBName string `toml:"db_name"`
+ Port int `toml:"port"`
+}
+
+func (p *PostgresConfig) SetDefaultValues() {
+ p.Host = "localhost"
+ p.User = ""
+ p.Password = ""
+ p.DBName = ""
+ p.Port = 5432
+}
+
+func (p *PostgresConfig) Connect() (*gorm.DB, error) {
+ db, err := gorm.Open(postgres.Open(p.generateDsn()), &gorm.Config{})
+ if err != nil {
+ return nil, err
+ }
+ return db, nil
+}
+
+// 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",
+ 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 c.Redis
+}
+
+func (c *Config) SetDefaultValues() {
+ c.Debug = false
+ c.Author = "anhgelus"
+ c.Redis = &gokord.RedisCredentials{}
+ c.Redis.SetDefaultValues()
+ c.Database = &PostgresConfig{}
+ 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)
+}
diff --git a/go.mod b/go.mod
index c80b3b4..d09fe7a 100644
--- a/go.mod
+++ b/go.mod
@@ -3,9 +3,10 @@ module github.com/anhgelus/les-copaings-bot
go 1.24
require (
- github.com/anhgelus/gokord v0.6.3
+ github.com/anhgelus/gokord v0.7.0
github.com/bwmarrin/discordgo v0.28.1
github.com/redis/go-redis/v9 v9.8.0
+ gorm.io/driver/postgres v1.5.11
)
require (
@@ -23,5 +24,5 @@ require (
golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
- gorm.io/driver/postgres v1.5.11 // indirect
+ gorm.io/gorm v1.26.1 // indirect
)
diff --git a/go.sum b/go.sum
index a188c46..a0274a5 100644
--- a/go.sum
+++ b/go.sum
@@ -16,6 +16,22 @@ github.com/anhgelus/gokord v0.6.2 h1:jR5l6NVGio+yChg8kxeNJSm6mbBfWxSLkvR6FPoh5E4
github.com/anhgelus/gokord v0.6.2/go.mod h1:R1SMf1+C8FshZ1/fDyBBt1Y9ob097UShHON21btlw2s=
github.com/anhgelus/gokord v0.6.3 h1:4Z57e64YcecI6gokGVpIq8YOfvybJofX2Kx1HYRTuwo=
github.com/anhgelus/gokord v0.6.3/go.mod h1:UIpun+/+pgtvMQZdXvsy3qBhNSPG+q18shwDShDEI3Y=
+github.com/anhgelus/gokord v0.6.4-0.20250521131643-7da533f7f3bf h1:7dspKr7pByg/NK5y+/FTtSUixV6JFOL75EU8OKGTjbY=
+github.com/anhgelus/gokord v0.6.4-0.20250521131643-7da533f7f3bf/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
+github.com/anhgelus/gokord v0.6.4-0.20250521133345-2e168a16e7c4 h1:mRp+EVtIVST0wG+cJD31NPF6eePAGon/25gSh5VCqIg=
+github.com/anhgelus/gokord v0.6.4-0.20250521133345-2e168a16e7c4/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
+github.com/anhgelus/gokord v0.6.4-0.20250521141937-ca8f3d5a9972 h1:yvqA7TeG0xa7IOSYoY1p1gAXjz9XvKe99bQdZoTxACU=
+github.com/anhgelus/gokord v0.6.4-0.20250521141937-ca8f3d5a9972/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
+github.com/anhgelus/gokord v0.6.4-0.20250521142735-c6a77bac401e h1:G8bPf9snE+C5UeBtvIQC+9iG3QIT/+z6p+Fa+TdMiEw=
+github.com/anhgelus/gokord v0.6.4-0.20250521142735-c6a77bac401e/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
+github.com/anhgelus/gokord v0.6.4-0.20250521143137-6aab0f8d9c26 h1:h0HMHZXE+pEpPscPPyLBU2h6eEzXvcQQTfmIe+4NJzk=
+github.com/anhgelus/gokord v0.6.4-0.20250521143137-6aab0f8d9c26/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
+github.com/anhgelus/gokord v0.6.4-0.20250521150912-98e83fc9532b h1:TeK9/A1lqyqtfKgjVK/uBOmGppKrqItdxvQm2NpnLDk=
+github.com/anhgelus/gokord v0.6.4-0.20250521150912-98e83fc9532b/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
+github.com/anhgelus/gokord v0.6.4-0.20250521152340-0569047e55ef h1:FZ7ga9B56jxxDJMuaT5DRlFmzlRiFw+NV5WDNVSaySc=
+github.com/anhgelus/gokord v0.6.4-0.20250521152340-0569047e55ef/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
+github.com/anhgelus/gokord v0.7.0 h1:G9GrxD3/xEreXsiz3etKxbeHsNHrwT5I/VEKSWpyrj4=
+github.com/anhgelus/gokord v0.7.0/go.mod h1:SfGKyMMGjNS9F9ehiEb5Cc58P+uoDdLDGGYqXSiMCus=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
diff --git a/main.go b/main.go
index 64db607..46bcb61 100644
--- a/main.go
+++ b/main.go
@@ -31,7 +31,7 @@ func init() {
}
func main() {
- err := gokord.SetupConfigs(nil, []*gokord.ConfigInfo{})
+ err := gokord.SetupConfigs(&Config{}, []*gokord.ConfigInfo{})
if err != nil {
panic(err)
}
@@ -44,7 +44,6 @@ func main() {
adm := gokord.AdminPermission
rankCmd := gokord.NewCommand("rank", "Affiche le niveau d'un copaing").
- HasOption().
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionUser,
"copaing",
@@ -59,7 +58,6 @@ func main() {
).
AddSub(
gokord.NewCommand("xp", "Modifie l'xp").
- HasOption().
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionString,
"type",
@@ -83,7 +81,6 @@ func main() {
).
AddSub(
gokord.NewCommand("disabled-channels", "Modifie les salons désactivés").
- HasOption().
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionString,
"type",
@@ -101,7 +98,6 @@ func main() {
).
AddSub(
gokord.NewCommand("period-before-reduce", "Temps avant la perte d'xp (affecte aussi le /top)").
- HasOption().
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionInteger,
"days",
@@ -111,7 +107,6 @@ func main() {
).
AddSub(
gokord.NewCommand("fallback-channel", "Modifie le salon textuel par défaut").
- HasOption().
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionChannel,
"channel",
@@ -121,16 +116,13 @@ func main() {
).SetPermission(&adm)
topCmd := gokord.NewCommand("top", "Copaings les plus actifs").
- HasOption().
SetHandler(commands.Top)
resetCmd := gokord.NewCommand("reset", "Reset l'xp").
- HasOption().
SetHandler(commands.Reset).
SetPermission(&adm)
resetUserCmd := gokord.NewCommand("reset-user", "Reset l'xp d'un utilisation").
- HasOption().
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionUser,
"user",
@@ -140,7 +132,6 @@ func main() {
SetPermission(&adm)
creditsCmd := gokord.NewCommand("credits", "Crédits").
- HasOption().
SetHandler(commands.Credits)
innovations, err := gokord.LoadInnovationFromJson(updatesData)
@@ -168,7 +159,7 @@ func main() {
Content: "Les Copaings Bot " + Version.String(),
},
},
- Commands: []*gokord.GeneralCommand{
+ Commands: []gokord.CommandBuilder{
rankCmd,
configCmd,
topCmd,
diff --git a/restart.sh b/restart.sh
new file mode 100644
index 0000000..ddb9128
--- /dev/null
+++ b/restart.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/bash
+
+podman compose down && podman compose build && podman compose up -d \ No newline at end of file
diff --git a/updates.json b/updates.json
index fd73d8f..f93e4b8 100644
--- a/updates.json
+++ b/updates.json
@@ -18,5 +18,15 @@
"config"
]
}
+ },
+ {
+ "version": "3.0.1",
+ "commands": {
+ "added": [],
+ "removed": [],
+ "updated": [
+ "config", "ping"
+ ]
+ }
}
]