Skip to content

Commit

Permalink
Removal procedure for slash commands example (#1103)
Browse files Browse the repository at this point in the history
Remove commands on exit.
  • Loading branch information
Fahim-Ferdous committed Feb 23, 2022
1 parent 0b48d46 commit 4cc53b7
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions examples/slash_commands/main.go
Expand Up @@ -363,24 +363,48 @@ func init() {

func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
err := s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}

for _, v := range commands {
_, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v)
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}

defer s.Close()

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Press Ctrl+C to exit")
<-stop

if *RemoveCommands {
log.Println("Removing commands...")
// // We need to fetch the commands, since deleting requires the command ID.
// // We are doing this from the returned commands on line 375, because using
// // this will delete all the commands, which might not be desirable, so we
// // are deleting only the commands that we added.
// registeredCommands, err := s.ApplicationCommands(s.State.User.ID, *GuildID)
// if err != nil {
// log.Fatalf("Could not fetch registered commands: %v", err)
// }

for _, v := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}

log.Println("Gracefully shutdowning")
}

0 comments on commit 4cc53b7

Please sign in to comment.