feat(config): disable channel for xp

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-15 16:44:46 +02:00
parent 5451dd0e81
commit f97df035c3
No known key found for this signature in database
GPG key ID: CF4550297832A29F
4 changed files with 106 additions and 9 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/anhgelus/les-copaings-bot/config" "github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/xp" "github.com/anhgelus/les-copaings-bot/xp"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"strings"
) )
func ConfigShow(s *discordgo.Session, i *discordgo.InteractionCreate) { func ConfigShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
@ -21,6 +22,20 @@ func ConfigShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
roles += fmt.Sprintf("> Niveau %d - <@&%s>\n", xp.Level(r.XP), r.RoleID) roles += fmt.Sprintf("> Niveau %d - <@&%s>\n", xp.Level(r.XP), r.RoleID)
} }
} }
if len(roles) == 0 {
roles = "Aucun rôle configuré :("
}
disChans := strings.Split(cfg.DisabledChannels, ";")
l = len(disChans) - 1
chans := ""
for i, c := range disChans {
if i != l {
chans += fmt.Sprintf("> <#%s>", c)
}
}
if len(chans) == 0 {
chans = "Aucun salon désactivé :)"
}
err := resp.Embeds([]*discordgo.MessageEmbed{ err := resp.Embeds([]*discordgo.MessageEmbed{
{ {
Type: discordgo.EmbedTypeRich, Type: discordgo.EmbedTypeRich,
@ -33,6 +48,11 @@ func ConfigShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
Value: roles, Value: roles,
Inline: false, Inline: false,
}, },
{
Name: "Salons désactivés",
Value: chans,
Inline: false,
},
}, },
}, },
}).Send() }).Send()
@ -133,3 +153,61 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) {
utils.SendAlert("commands/config.go - Config updated", err.Error()) utils.SendAlert("commands/config.go - Config updated", err.Error())
} }
} }
func ConfigChannel(s *discordgo.Session, i *discordgo.InteractionCreate) {
optMap := utils.GenerateOptionMapForSubcommand(i)
resp := utils.ResponseBuilder{C: s, I: i}
resp.IsEphemeral()
// verify every args
t, ok := optMap["type"]
if !ok {
err := resp.Message("Le type d'action n'a pas été renseigné.").Send()
if err != nil {
utils.SendAlert("commands/config.go - Action type not set", err.Error())
}
return
}
ts := t.StringValue()
salon, ok := optMap["channel"]
if !ok {
err := resp.Message("Le salon n'a pas été renseigné.").Send()
if err != nil {
utils.SendAlert("commands/config.go - Channel not set", err.Error())
}
return
}
channel := salon.ChannelValue(s)
cfg := config.GetGuildConfig(i.GuildID)
switch ts {
case "add":
if strings.Contains(cfg.DisabledChannels, channel.ID) {
err := resp.Message("Le salon est déjà dans la liste des salons désactivés").Send()
if err != nil {
utils.SendAlert("commands/config.go - Channel already disabled", err.Error())
}
return
}
cfg.DisabledChannels += channel.ID + ";"
case "del":
if !strings.Contains(cfg.DisabledChannels, channel.ID) {
err := resp.Message("Le salon n'est pas désactivé").Send()
if err != nil {
utils.SendAlert("commands/config.go - Channel not disabled", err.Error())
}
return
}
cfg.DisabledChannels = strings.ReplaceAll(cfg.DisabledChannels, channel.ID+";", "")
default:
err := resp.Message("Le type d'action n'est pas valide.").Send()
if err != nil {
utils.SendAlert("commands/config.go - Invalid action type", err.Error())
}
return
}
// save
cfg.Save()
err := resp.Message("Modification sauvegardé.").Send()
if err != nil {
utils.SendAlert("commands/config.go - Modification saved message", err.Error())
}
}

View file

@ -7,8 +7,9 @@ import (
type GuildConfig struct { type GuildConfig struct {
gorm.Model gorm.Model
GuildID string `gorm:"not null"` GuildID string `gorm:"not null"`
XpRoles []XpRole XpRoles []XpRole
DisabledChannels string
} }
type XpRole struct { type XpRole struct {

18
main.go
View file

@ -64,6 +64,24 @@ func main() {
"Rôle", "Rôle",
).IsRequired()). ).IsRequired()).
SetHandler(commands.ConfigXP), SetHandler(commands.ConfigXP),
).
AddSub(
gokord.NewCommand("disabled-channels", "Modifie les salons désactivés").
HasOption().
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionString,
"type",
"Type d'action à effectuer",
).
AddChoice(gokord.NewChoice("Désactiver", "add")).
AddChoice(gokord.NewChoice("Activer", "del")).IsRequired(),
).
AddOption(gokord.NewOption(
discordgo.ApplicationCommandOptionChannel,
"channel",
"Salon à modifier",
).IsRequired()).
SetHandler(commands.ConfigChannel),
) )
bot := gokord.Bot{ bot := gokord.Bot{

View file

@ -15,7 +15,7 @@ type Copaing struct {
GuildID string `gorm:"not null"` GuildID string `gorm:"not null"`
} }
var r *redis.Client var redisClient *redis.Client
func GetCopaing(discordID string, guildID string) *Copaing { func GetCopaing(discordID string, guildID string) *Copaing {
c := Copaing{DiscordID: discordID, GuildID: guildID} c := Copaing{DiscordID: discordID, GuildID: guildID}
@ -43,19 +43,19 @@ func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn f
} }
func getRedisClient() (*redis.Client, error) { func getRedisClient() (*redis.Client, error) {
if r == nil { if redisClient == nil {
var err error var err error
r, err = gokord.BaseCfg.Redis.Get() redisClient, err = gokord.BaseCfg.Redis.Get()
return r, err return redisClient, err
} }
return r, nil return redisClient, nil
} }
func CloseRedisClient() { func CloseRedisClient() {
if r == nil { if redisClient == nil {
return return
} }
err := r.Close() err := redisClient.Close()
if err != nil { if err != nil {
utils.SendAlert("xp/member.go - Closing redis client", err.Error()) utils.SendAlert("xp/member.go - Closing redis client", err.Error())
} }