update exec slash command
@@ -37,7 +37,7 @@ func (c *Exec) Description() string {
}
func (c *Exec) Version() string {
- return "1.0.0"
+ return "1.1.0"
}
func (c *Exec) Type() discordgo.ApplicationCommandType {
@@ -56,6 +56,19 @@ func (c *Exec) Options() []*discordgo.ApplicationCommandOption {
Name: "reset",
Description: "Disable code execution and remove stored credentials.",
},
+ {
+ Type: discordgo.ApplicationCommandOptionSubCommand,
+ Name: "enable",
+ Description: "Set the enabled state true or false.",
+ Options: []*discordgo.ApplicationCommandOption{
+ {
+ Type: discordgo.ApplicationCommandOptionBoolean,
+ Name: "enabled",
+ Description: "The enabled state of the code execution.",
+ Required: true,
+ },
+ },
+ },
{
Type: discordgo.ApplicationCommandOptionSubCommand,
Name: "check",
@@ -77,6 +90,18 @@ func (c *Exec) Run(ctx *ken.Ctx) (err error) {
return
}
+ var ranEnable bool
+ err = ctx.HandleSubCommands(ken.SubCommandHandler{"enable", func(ctx *ken.SubCommandCtx) error {
+ ranEnable = true
+ return c.enable(ctx)
+ }})
+ if err != nil {
+ return
+ }
+ if ranEnable {
+ return
+ }
+
execFact := ctx.Get(static.DiCodeExecFactory).(codeexec.Factory)
if execFact.Name() == "ranna" {
return ctx.FollowUpEmbed(&discordgo.MessageEmbed{
@@ -169,6 +194,14 @@ func (c *Exec) setup(ctx *ken.SubCommandCtx) (err error) {
if err != nil {
util.SendEmbedError(ctx.Session, dmChan.ID,
"An unexpected error occured while saving the key. Please contact the host of this bot about this: ```\n"+err.Error()+"\n```")
+ return
+ }
+
+ err = db.SetGuildCodeExecEnabled(ctx.Event.GuildID, true)
+ if err != nil {
+ util.SendEmbedError(ctx.Session, dmChan.ID,
+ "An unexpected error occured while saving enabled state. Please contact the host of this bot about this: ```\n"+err.Error()+"\n```")
+ return
}
util.SendEmbed(s, dmChan.ID, "API key set and system is enabled. :ok_hand:", "", static.ColorEmbedGreen)
@@ -189,11 +222,35 @@ func (c *Exec) reset(ctx *ken.SubCommandCtx) (err error) {
return err
}
+ err = db.SetGuildCodeExecEnabled(ctx.Event.GuildID, false)
+ if err != nil {
+ return err
+ }
+
return ctx.FollowUpEmbed(&discordgo.MessageEmbed{
Description: "API key was deleted from database and system was disabled.",
}).Error
}
+func (c *Exec) enable(ctx *ken.SubCommandCtx) (err error) {
+ enabled := ctx.Options().GetByName("enabled").BoolValue()
+
+ db, _ := ctx.Get(static.DiDatabase).(database.Database)
+ err = db.SetGuildCodeExecEnabled(ctx.Event.GuildID, enabled)
+ if err != nil {
+ return err
+ }
+
+ stateStr := "disabled"
+ if enabled {
+ stateStr = "enabled"
+ }
+
+ return ctx.FollowUpEmbed(&discordgo.MessageEmbed{
+ Description: fmt.Sprintf("Code execution has been **%s** on this guild.", stateStr),
+ }).Error
+}
+
func (c *Exec) check(ctx *ken.SubCommandCtx) (err error) {
db, _ := ctx.Get(static.DiDatabase).(database.Database)
key, err := db.GetGuildJdoodleKey(ctx.Event.GuildID)
