feat(commands,config): level changing command

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-08-26 11:51:13 -03:00
parent 739ba7062a
commit 1ea4309224

View File

@@ -1,8 +1,11 @@
package commands
import (
"dislate/internals/discord/bot/gconf"
e "errors"
"fmt"
"log/slog"
"dislate/internals/discord/bot/gconf"
dgo "github.com/bwmarrin/discordgo"
)
@@ -32,6 +35,7 @@ func (c ManageConfig) Components() []Component {
func (c ManageConfig) Subcommands() []Command {
return []Command{
loggerConfigChannel(c),
loggerConfigLevel(c),
}
}
@@ -99,3 +103,81 @@ func (c loggerConfigChannel) Components() []Component {
func (c loggerConfigChannel) Subcommands() []Command {
return []Command{}
}
type loggerConfigLevel struct {
db gconf.DB
}
func (c loggerConfigLevel) Info() *dgo.ApplicationCommand {
var permissions int64 = dgo.PermissionAdministrator
return &dgo.ApplicationCommand{
Name: "log-level",
Description: "Change logging channel",
DefaultMemberPermissions: &permissions,
Options: []*dgo.ApplicationCommandOption{{
Type: dgo.ApplicationCommandOptionString,
Required: true,
Name: "log-level",
Description: "The logging level of messages and errors",
Choices: []*dgo.ApplicationCommandOptionChoice{
{Name: "Debug", Value: "debug"},
{Name: "Info", Value: "info"},
{Name: "Warn", Value: "warn"},
{Name: "Error", Value: "error"},
},
}},
}
}
func (c loggerConfigLevel) Handle(s *dgo.Session, ic *dgo.InteractionCreate) error {
opts := getOptions(ic.ApplicationCommandData().Options)
var err error
opt, ok := opts["log-level"]
if !ok {
return e.New("Parameter log-level is required")
}
var l slog.Level
switch opt.Value {
case "debug":
l = slog.LevelDebug
case "info":
l = slog.LevelInfo
case "warn":
l = slog.LevelWarn
case "error":
l = slog.LevelError
default:
return e.New("Parameter log-level is not a valid value")
}
guild, err := c.db.Guild(ic.GuildID)
if err != nil {
return err
}
conf := guild.Config
conf.LoggingLevel = &l
guild.Config = conf
err = c.db.GuildUpdate(guild)
if err != nil {
return err
}
err = s.InteractionRespond(ic.Interaction, &dgo.InteractionResponse{
Type: dgo.InteractionResponseChannelMessageWithSource,
Data: &dgo.InteractionResponseData{
Content: fmt.Sprintf("Logging level changed to %s", l),
Flags: dgo.MessageFlagsEphemeral,
},
})
return err
}
func (c loggerConfigLevel) Components() []Component {
return []Component{}
}
func (c loggerConfigLevel) Subcommands() []Command {
return []Command{}
}