Skip to content

Commit

Permalink
fix: new command not registering
Browse files Browse the repository at this point in the history
That was quite the problem
  • Loading branch information
TheTipo01 committed Nov 21, 2021
1 parent 5422015 commit ce1fc7d
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,26 +197,38 @@ func ready(s *discordgo.Session, _ *discordgo.Ready) {

// Checks for unused commands and deletes them
if cmds, err := s.ApplicationCommands(s.State.User.ID, ""); err == nil {
for _, c := range cmds {
if commandHandlers[c.Name] == nil {
_ = s.ApplicationCommandDelete(s.State.User.ID, "", c.ID)
lit.Info("Deleted unused command %s", c.Name)
}
found := false

// Compare commands with the ones in commands, if they are different we re-create them
for _, v := range commands {
if c.Name == v.Name {
if !isCommandEqual(c, v) {
lit.Info("Registering command `%s`", v.Name)
for _, l := range commands {
for _, o := range cmds {
// We compare every online command with the ones locally stored, to find if a command with the same name exists
if l.Name == o.Name {
// If the options of the command are not equal, we re-register it
if !isCommandEqual(l, o) {
lit.Info("Registering command `%s`", l.Name)

_, err = s.ApplicationCommandCreate(s.State.User.ID, "", v)
_, err = s.ApplicationCommandCreate(s.State.User.ID, "", l)
if err != nil {
lit.Error("Cannot create '%s' command: %s", v.Name, err)
lit.Error("Cannot create '%s' command: %s", l.Name, err)
}
}

found = true
break
}
}

// If we didn't found a match for the locally stored command, it means the command is new. We register it
if !found {
lit.Info("Registering new command `%s`", l.Name)

_, err = s.ApplicationCommandCreate(s.State.User.ID, "", l)
if err != nil {
lit.Error("Cannot create '%s' command: %s", l.Name, err)
}

found = false
}
}
}
}

0 comments on commit ce1fc7d

Please sign in to comment.