slashRouter.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package discordbot
  2. import (
  3. "github.com/bwmarrin/discordgo"
  4. )
  5. type SlashRouter struct {
  6. registeredCommands []*discordgo.ApplicationCommand
  7. commands []*discordgo.ApplicationCommand
  8. commandHandlers map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)
  9. guildIDs []string
  10. existingCommands []Command
  11. }
  12. type SlashRouteOptions struct {
  13. Name string // match name that should trigger this route handler
  14. Description string // short description of this route
  15. Callback SlashHandler
  16. Options []*discordgo.ApplicationCommandOption // Array of application command options for the command
  17. }
  18. // Handler is the function signature required for a message route handler.
  19. type SlashHandler func(s *discordgo.Session, i *discordgo.InteractionCreate)
  20. func NewSlashRouter() *SlashRouter {
  21. r := &SlashRouter{}
  22. r.commandHandlers = make(map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate))
  23. return r
  24. }
  25. func (r *SlashRouter) RegisterGuild(guildID string) {
  26. r.guildIDs = append(r.guildIDs, guildID)
  27. }
  28. type Command struct {
  29. applicationID string
  30. guildID string
  31. commandID string
  32. name string
  33. }
  34. func (r *SlashRouter) GetExistingCommand(appID, guildID, name string) (string, bool) {
  35. for _, cmd := range r.existingCommands {
  36. if cmd.applicationID == appID && cmd.guildID == guildID && cmd.name == name {
  37. return cmd.commandID, true
  38. }
  39. }
  40. return "", false
  41. }
  42. func (r *SlashRouter) LoadExistingCommands(s *discordgo.Session) error {
  43. logHandler.Info("Loading Existing Commands")
  44. for _, guildID := range r.guildIDs {
  45. logHandler.Infof("Getting applications for %s", guildID)
  46. cmds, err := s.ApplicationCommands(s.State.User.ID, guildID)
  47. if err != nil {
  48. r.existingCommands = nil
  49. return err
  50. }
  51. for _, cmd := range cmds {
  52. newCommand := Command{
  53. applicationID: cmd.ApplicationID,
  54. guildID: cmd.GuildID,
  55. commandID: cmd.ID,
  56. name: cmd.Name,
  57. }
  58. r.existingCommands = append(r.existingCommands, newCommand)
  59. }
  60. }
  61. logHandler.Infof("Loaded %d commands", len(r.existingCommands))
  62. return nil
  63. }
  64. func (r *SlashRouter) Register(name string, options SlashRouteOptions) error {
  65. if name == "" {
  66. return ErrRegisterRouteNameRequired
  67. }
  68. if options.Callback == nil {
  69. return ErrRegisterCallbackRequired
  70. }
  71. command := discordgo.ApplicationCommand{
  72. Name: name,
  73. Description: options.Description,
  74. Options: options.Options,
  75. }
  76. r.commands = append(r.commands, &command)
  77. r.commandHandlers[name] = options.Callback
  78. return nil
  79. }
  80. func (r *SlashRouter) GenerateCommands(s *discordgo.Session) error {
  81. r.registeredCommands = make([]*discordgo.ApplicationCommand, len(r.commands))
  82. s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
  83. if h, ok := r.commandHandlers[i.ApplicationCommandData().Name]; ok {
  84. h(s, i)
  85. }
  86. })
  87. for _, guildID := range r.guildIDs {
  88. for i, v := range r.commands {
  89. var cmd *discordgo.ApplicationCommand
  90. var err error
  91. if cmdID, exists := r.GetExistingCommand(s.State.User.ID, guildID, v.Name); exists {
  92. logHandler.Infof("Updating the existing command - %s", v.Name)
  93. cmd, err = s.ApplicationCommandEdit(s.State.User.ID, guildID, cmdID, v)
  94. } else {
  95. logHandler.Infof("Creating a new command - %s", v.Name)
  96. cmd, err = s.ApplicationCommandCreate(s.State.User.ID, guildID, v)
  97. }
  98. if err != nil {
  99. logHandler.Errorf("Cannot create '%v' command: %v", v.Name, err)
  100. return err
  101. }
  102. r.registeredCommands[i] = cmd
  103. }
  104. }
  105. return nil
  106. }
  107. func (r *SlashRouter) CleanCommands(s *discordgo.Session) error {
  108. for _, guildID := range r.guildIDs {
  109. for _, v := range r.registeredCommands {
  110. err := s.ApplicationCommandDelete(s.State.User.ID, guildID, v.ID)
  111. if err != nil {
  112. logHandler.Errorf("Cannot delete '%v' command: %v", v.Name, err)
  113. return err
  114. }
  115. }
  116. }
  117. return nil
  118. }
  119. // Create a Router
  120. // Added commands the bot will support
  121. func (bot *Bot) AddSlashCommands(guildID string, commands *[]discordgo.ApplicationCommand) error {
  122. err := bot.session.Open()
  123. if err != nil {
  124. return err
  125. }
  126. defer bot.session.Close()
  127. existingCommands, err := bot.session.ApplicationCommands(bot.session.State.User.ID, guildID)
  128. if err != nil {
  129. logHandler.WithError(err).Errorf("Failed to retrieve application commands from the server %s for user %s", guildID, bot.session.State.User.ID)
  130. return err
  131. }
  132. var find = func(name string, commands []*discordgo.ApplicationCommand) bool {
  133. for _, command := range commands {
  134. if command.Name == name {
  135. return true
  136. }
  137. }
  138. return false
  139. }
  140. for _, newCommand := range *commands {
  141. if exists := find(newCommand.Name, existingCommands); !exists {
  142. cmd, err := bot.session.ApplicationCommandCreate(bot.session.State.User.ID, guildID, &newCommand)
  143. if err != nil {
  144. logHandler.WithError(err).Errorf("Failed to create command %s", newCommand.Name)
  145. return err
  146. }
  147. logHandler.Infof("New Command for %s added, new command id is %s", newCommand.Name, cmd.ID)
  148. }
  149. }
  150. return nil
  151. }
  152. func (bot *Bot) RemoveSlashCommands(guildID string) error {
  153. err := bot.session.Open()
  154. if err != nil {
  155. return err
  156. }
  157. defer bot.session.Close()
  158. existingCommands, err := bot.session.ApplicationCommands(bot.session.State.User.ID, guildID)
  159. if err != nil {
  160. logHandler.WithError(err).Errorf("Failed to retrieve application commands from the server %s for user %s", guildID, bot.session.State.User.ID)
  161. return err
  162. }
  163. for _, existingCommand := range existingCommands {
  164. if err := bot.session.ApplicationCommandDelete(bot.session.State.User.ID, guildID, existingCommand.ID); err != nil {
  165. logHandler.WithError(err).Errorf("Failed to delete command %s", existingCommand.Name)
  166. return err
  167. }
  168. }
  169. return nil
  170. }