fixed some rare boot looping
This commit is contained in:
61
handlers.go
61
handlers.go
@@ -1,52 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/disgoorg/disgo/bot"
|
||||
"github.com/disgoorg/disgo/discord"
|
||||
"github.com/disgoorg/disgo/events"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/vaporvee/acecore/shared"
|
||||
)
|
||||
|
||||
var commands []shared.Command
|
||||
|
||||
func ready(e *events.Ready) {
|
||||
logrus.Info("Starting up...")
|
||||
removeOldCommandFromAllGuilds(e.Client())
|
||||
var existingCommandNames []string
|
||||
existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false)
|
||||
if err != nil {
|
||||
logrus.Errorf("error fetching existing global commands: %v", err)
|
||||
} else {
|
||||
for _, existingCommand := range existingCommands {
|
||||
existingCommandNames = append(existingCommandNames, existingCommand.Name())
|
||||
}
|
||||
}
|
||||
globalCommands := []discord.ApplicationCommandCreate{}
|
||||
for _, command := range commands {
|
||||
if !slices.Contains(existingCommandNames, command.Definition.CommandName()) || slices.Contains(os.Args, "--update-all") || slices.Contains(os.Args, "--clean") {
|
||||
globalCommands = append(globalCommands, command.Definition)
|
||||
logrus.Infof("Appending command \"%s\"", command.Definition.CommandName())
|
||||
}
|
||||
}
|
||||
if len(globalCommands) > 0 {
|
||||
logrus.Infof("Attempting to add global commands %s", fmt.Sprint(globalCommands))
|
||||
_, err = e.Client().Rest().SetGlobalCommands(e.Client().ApplicationID(), globalCommands)
|
||||
if err != nil {
|
||||
logrus.Errorf("error creating global commands '%s'", err)
|
||||
} else {
|
||||
logrus.Infof("Added global commands sucessfully!")
|
||||
}
|
||||
}
|
||||
logrus.Info("Successfully started the Bot!")
|
||||
}
|
||||
|
||||
func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) {
|
||||
for _, command := range commands {
|
||||
if command.Interact != nil && e.Data.CommandName() == command.Definition.CommandName() {
|
||||
@@ -106,28 +70,3 @@ func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeOldCommandFromAllGuilds(c bot.Client) {
|
||||
app, err := c.Rest().GetCurrentApplication()
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
globalCommands, err := c.Rest().GetGlobalCommands(app.Bot.ID, false)
|
||||
if err != nil {
|
||||
logrus.Errorf("error fetching existing global commands: %v", err)
|
||||
return
|
||||
}
|
||||
var commandNames []string
|
||||
for _, command := range commands {
|
||||
commandNames = append(commandNames, command.Definition.CommandName())
|
||||
}
|
||||
for _, existingCommand := range globalCommands {
|
||||
if !slices.Contains(commandNames, existingCommand.Name()) {
|
||||
logrus.Infof("Deleting command '%s'", existingCommand.Name())
|
||||
err := c.Rest().DeleteGlobalCommand(c.ApplicationID(), existingCommand.ID())
|
||||
if err != nil {
|
||||
logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user