refactor(config): handle disabled channels
This commit is contained in:
parent
0b93ea5daa
commit
b9466f8122
3 changed files with 88 additions and 72 deletions
|
@ -102,73 +102,3 @@ func Config(_ *discordgo.Session, i *discordgo.InteractionCreate, _ cmd.OptionMa
|
|||
logger.Alert("config/guild.go - Sending config", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func ConfigChannel(s *discordgo.Session, i *discordgo.InteractionCreate, optMap cmd.OptionMap, resp *cmd.ResponseBuilder) {
|
||||
resp.IsEphemeral()
|
||||
// verify every args
|
||||
t, ok := optMap["type"]
|
||||
if !ok {
|
||||
err := resp.SetMessage("Le type d'action n'a pas été renseigné.").Send()
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Action type not set", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
ts := t.StringValue()
|
||||
salon, ok := optMap["channel"]
|
||||
if !ok {
|
||||
err := resp.SetMessage("Le salon n'a pas été renseigné.").Send()
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Channel not set (disabled)", 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.SetMessage("Le salon est déjà dans la liste des salons désactivés").Send()
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Channel already disabled", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
cfg.DisabledChannels += channel.ID + ";"
|
||||
case "del":
|
||||
if !strings.Contains(cfg.DisabledChannels, channel.ID) {
|
||||
err := resp.SetMessage("Le salon n'est pas désactivé").Send()
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Channel not disabled", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
cfg.DisabledChannels = strings.ReplaceAll(cfg.DisabledChannels, channel.ID+";", "")
|
||||
default:
|
||||
err := resp.SetMessage("Le type d'action n'est pas valide.").Send()
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Invalid action type", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
// save
|
||||
err := cfg.Save()
|
||||
if err != nil {
|
||||
logger.Alert(
|
||||
"commands/config.go - Saving config",
|
||||
err.Error(),
|
||||
"guild_id",
|
||||
i.GuildID,
|
||||
"type",
|
||||
ts,
|
||||
"channel_id",
|
||||
channel.ID,
|
||||
)
|
||||
err = resp.SetMessage("Il y a eu une erreur lors de la modification de de la base de données.").Send()
|
||||
} else {
|
||||
err = resp.SetMessage("Modification sauvegardé.").Send()
|
||||
}
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Modification saved message", err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,12 +5,18 @@ import (
|
|||
"github.com/anhgelus/gokord/component"
|
||||
"github.com/anhgelus/gokord/logger"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
ModifyDisChannel = "disabled_channel"
|
||||
ModifyFallbackChannel = "fallback_channel"
|
||||
FallbackChannelSet = "fallback_channel_set"
|
||||
|
||||
ModifyDisChannel = "disabled_channel"
|
||||
DisChannelAdd = "disabled_channel_add"
|
||||
DisChannelAddSet = "disabled_channel_add_set"
|
||||
DisChannelDel = "disabled_channel_del"
|
||||
DisChannelDelSet = "disabled_channel_del_set"
|
||||
)
|
||||
|
||||
func HandleModifyFallbackChannel(_ *discordgo.Session, _ *discordgo.InteractionCreate, _ discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
|
||||
|
@ -18,7 +24,7 @@ func HandleModifyFallbackChannel(_ *discordgo.Session, _ *discordgo.InteractionC
|
|||
component.NewChannelSelect(FallbackChannelSet).AddChannelType(discordgo.ChannelTypeGuildText),
|
||||
))).Send()
|
||||
if err != nil {
|
||||
logger.Alert("config/channel.go - Sending channel list", err.Error())
|
||||
logger.Alert("config/channel.go - Sending channel list for fallback", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,3 +47,78 @@ func HandleFallbackChannelSet(_ *discordgo.Session, i *discordgo.InteractionCrea
|
|||
logger.Alert("config/channel.go - Sending channel saved", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func HandleModifyDisChannel(_ *discordgo.Session, _ *discordgo.InteractionCreate, _ discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
|
||||
err := resp.SetMessage("Salon de repli...").SetComponents(component.New().Add(component.NewActionRow().
|
||||
Add(
|
||||
component.NewButton(DisChannelAdd, discordgo.PrimaryButton).
|
||||
SetLabel("Désactiver un salon").
|
||||
SetEmoji(&discordgo.ComponentEmoji{Name: "⬇️"}),
|
||||
).
|
||||
Add(
|
||||
component.NewButton(DisChannelDel, discordgo.DangerButton).
|
||||
SetLabel("Réactiver un salon").
|
||||
SetEmoji(&discordgo.ComponentEmoji{Name: "⬆️"}),
|
||||
),
|
||||
)).Send()
|
||||
if err != nil {
|
||||
logger.Alert("config/channel.go - Sending action type", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func HandleDisChannel(_ *discordgo.Session, _ *discordgo.InteractionCreate, data discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
|
||||
resp.SetMessage("Salon à désactiver...")
|
||||
cID := DisChannelAddSet
|
||||
if data.CustomID == DisChannelDel {
|
||||
resp.SetMessage("Salon à réactiver...")
|
||||
cID = DisChannelDelSet
|
||||
}
|
||||
err := resp.SetComponents(component.New().Add(component.NewActionRow().Add(component.NewChannelSelect(cID)))).Send()
|
||||
if err != nil {
|
||||
logger.Alert("config/channel.go - Sending channel list for disable", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func HandleDisChannelAddSet(_ *discordgo.Session, i *discordgo.InteractionCreate, data discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
|
||||
cfg := GetGuildConfig(i.GuildID)
|
||||
id := data.Values[0]
|
||||
if strings.Contains(cfg.DisabledChannels, id) {
|
||||
err := resp.SetMessage("Le salon est déjà dans la liste des salons désactivés").Send()
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Channel already disabled", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
cfg.DisabledChannels += id + ";"
|
||||
if err := cfg.Save(); err != nil {
|
||||
logger.Alert("commands/config.go - Saving config disable add", err.Error())
|
||||
if err = resp.SetMessage("Il y a eu une erreur lors de la modification de de la base de données.").Send(); err != nil {
|
||||
logger.Alert("config/channel.go - Sending error while saving config", err.Error())
|
||||
}
|
||||
}
|
||||
if err := resp.SetMessage("Modification sauvegardé.").Send(); err != nil {
|
||||
logger.Alert("commands/config.go - Modification saved message disable add", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func HandleDisChannelDelSet(_ *discordgo.Session, i *discordgo.InteractionCreate, data discordgo.MessageComponentInteractionData, resp *cmd.ResponseBuilder) {
|
||||
cfg := GetGuildConfig(i.GuildID)
|
||||
id := data.Values[0]
|
||||
if !strings.Contains(cfg.DisabledChannels, id) {
|
||||
err := resp.SetMessage("Le salon n'est pas désactivé").Send()
|
||||
if err != nil {
|
||||
logger.Alert("commands/config.go - Channel not disabled", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
cfg.DisabledChannels = strings.ReplaceAll(cfg.DisabledChannels, id+";", "")
|
||||
if err := cfg.Save(); err != nil {
|
||||
logger.Alert("commands/config.go - Saving config disable del", err.Error())
|
||||
if err = resp.SetMessage("Il y a eu une erreur lors de la modification de de la base de données.").Send(); err != nil {
|
||||
logger.Alert("config/channel.go - Sending error while saving config", err.Error())
|
||||
}
|
||||
}
|
||||
if err := resp.SetMessage("Modification sauvegardé.").Send(); err != nil {
|
||||
logger.Alert("commands/config.go - Modification saved message disable del", err.Error())
|
||||
}
|
||||
}
|
||||
|
|
5
main.go
5
main.go
|
@ -142,6 +142,11 @@ func main() {
|
|||
// channel related
|
||||
bot.HandleMessageComponent(config.HandleModifyFallbackChannel, config.ModifyFallbackChannel)
|
||||
bot.HandleMessageComponent(config.HandleFallbackChannelSet, config.FallbackChannelSet)
|
||||
bot.HandleMessageComponent(config.HandleModifyDisChannel, config.ModifyDisChannel)
|
||||
bot.HandleMessageComponent(config.HandleDisChannel, config.DisChannelAdd)
|
||||
bot.HandleMessageComponent(config.HandleDisChannel, config.DisChannelDel)
|
||||
bot.HandleMessageComponent(config.HandleDisChannelAddSet, config.DisChannelAddSet)
|
||||
bot.HandleMessageComponent(config.HandleDisChannelDelSet, config.DisChannelDelSet)
|
||||
// reduce related
|
||||
bot.HandleMessageComponent(config.HandleModifyPeriodicReduce, config.ModifyTimeReduce)
|
||||
bot.HandleModal(config.HandleTimeReduceSet, config.TimeReduceSet)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue