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/xp"
"github.com/bwmarrin/discordgo"
"strings"
)
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)
}
}
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{
{
Type: discordgo.EmbedTypeRich,
@ -33,6 +48,11 @@ func ConfigShow(s *discordgo.Session, i *discordgo.InteractionCreate) {
Value: roles,
Inline: false,
},
{
Name: "Salons désactivés",
Value: chans,
Inline: false,
},
},
},
}).Send()
@ -133,3 +153,61 @@ func ConfigXP(s *discordgo.Session, i *discordgo.InteractionCreate) {
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 {
gorm.Model
GuildID string `gorm:"not null"`
XpRoles []XpRole
GuildID string `gorm:"not null"`
XpRoles []XpRole
DisabledChannels string
}
type XpRole struct {

18
main.go
View file

@ -64,6 +64,24 @@ func main() {
"Rôle",
).IsRequired()).
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{

View file

@ -15,7 +15,7 @@ type Copaing struct {
GuildID string `gorm:"not null"`
}
var r *redis.Client
var redisClient *redis.Client
func GetCopaing(discordID string, guildID string) *Copaing {
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) {
if r == nil {
if redisClient == nil {
var err error
r, err = gokord.BaseCfg.Redis.Get()
return r, err
redisClient, err = gokord.BaseCfg.Redis.Get()
return redisClient, err
}
return r, nil
return redisClient, nil
}
func CloseRedisClient() {
if r == nil {
if redisClient == nil {
return
}
err := r.Close()
err := redisClient.Close()
if err != nil {
utils.SendAlert("xp/member.go - Closing redis client", err.Error())
}