main.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/signal"
  8. "github.com/bwmarrin/discordgo"
  9. )
  10. // Bot parameters
  11. var (
  12. GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
  13. BotToken = flag.String("token", "", "Bot access token")
  14. RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
  15. )
  16. var s *discordgo.Session
  17. func init() { flag.Parse() }
  18. func init() {
  19. var err error
  20. s, err = discordgo.New("Bot " + *BotToken)
  21. if err != nil {
  22. log.Fatalf("Invalid bot parameters: %v", err)
  23. }
  24. }
  25. var (
  26. commands = []*discordgo.ApplicationCommand{
  27. {
  28. Name: "single-autocomplete",
  29. Description: "Showcase of single autocomplete option",
  30. Type: discordgo.ChatApplicationCommand,
  31. Options: []*discordgo.ApplicationCommandOption{
  32. {
  33. Name: "autocomplete-option",
  34. Description: "Autocomplete option",
  35. Type: discordgo.ApplicationCommandOptionString,
  36. Required: true,
  37. Autocomplete: true,
  38. },
  39. },
  40. },
  41. {
  42. Name: "multi-autocomplete",
  43. Description: "Showcase of multiple autocomplete option",
  44. Type: discordgo.ChatApplicationCommand,
  45. Options: []*discordgo.ApplicationCommandOption{
  46. {
  47. Name: "autocomplete-option-1",
  48. Description: "Autocomplete option 1",
  49. Type: discordgo.ApplicationCommandOptionString,
  50. Required: true,
  51. Autocomplete: true,
  52. },
  53. {
  54. Name: "autocomplete-option-2",
  55. Description: "Autocomplete option 2",
  56. Type: discordgo.ApplicationCommandOptionString,
  57. Required: true,
  58. Autocomplete: true,
  59. },
  60. },
  61. },
  62. }
  63. commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
  64. "single-autocomplete": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
  65. switch i.Type {
  66. case discordgo.InteractionApplicationCommand:
  67. data := i.ApplicationCommandData()
  68. err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  69. Type: discordgo.InteractionResponseChannelMessageWithSource,
  70. Data: &discordgo.InteractionResponseData{
  71. Content: fmt.Sprintf(
  72. "You picked %q autocompletion",
  73. // Autocompleted options do not affect usual flow of handling application command. They are ordinary options at this stage
  74. data.Options[0].StringValue(),
  75. ),
  76. },
  77. })
  78. if err != nil {
  79. panic(err)
  80. }
  81. // Autocomplete options introduce a new interaction type (8) for returning custom autocomplete results.
  82. case discordgo.InteractionApplicationCommandAutocomplete:
  83. data := i.ApplicationCommandData()
  84. choices := []*discordgo.ApplicationCommandOptionChoice{
  85. {
  86. Name: "Autocomplete",
  87. Value: "autocomplete",
  88. },
  89. {
  90. Name: "Autocomplete is best!",
  91. Value: "autocomplete_is_best",
  92. },
  93. {
  94. Name: "Choice 3",
  95. Value: "choice3",
  96. },
  97. {
  98. Name: "Choice 4",
  99. Value: "choice4",
  100. },
  101. {
  102. Name: "Choice 5",
  103. Value: "choice5",
  104. },
  105. // And so on, up to 25 choices
  106. }
  107. if data.Options[0].StringValue() != "" {
  108. choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
  109. Name: data.Options[0].StringValue(), // To get user input you just get value of the autocomplete option.
  110. Value: "choice_custom",
  111. })
  112. }
  113. err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  114. Type: discordgo.InteractionApplicationCommandAutocompleteResult,
  115. Data: &discordgo.InteractionResponseData{
  116. Choices: choices, // This is basically the whole purpose of autocomplete interaction - return custom options to the user.
  117. },
  118. })
  119. if err != nil {
  120. panic(err)
  121. }
  122. }
  123. },
  124. "multi-autocomplete": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
  125. switch i.Type {
  126. case discordgo.InteractionApplicationCommand:
  127. data := i.ApplicationCommandData()
  128. err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  129. Type: discordgo.InteractionResponseChannelMessageWithSource,
  130. Data: &discordgo.InteractionResponseData{
  131. Content: fmt.Sprintf(
  132. "Option 1: %s\nOption 2: %s",
  133. data.Options[0].StringValue(),
  134. data.Options[1].StringValue(),
  135. ),
  136. },
  137. })
  138. if err != nil {
  139. panic(err)
  140. }
  141. case discordgo.InteractionApplicationCommandAutocomplete:
  142. data := i.ApplicationCommandData()
  143. var choices []*discordgo.ApplicationCommandOptionChoice
  144. switch {
  145. // In this case there are multiple autocomplete options. The Focused field shows which option user is focused on.
  146. case data.Options[0].Focused:
  147. choices = []*discordgo.ApplicationCommandOptionChoice{
  148. {
  149. Name: "Autocomplete 4 first option",
  150. Value: "autocomplete_default",
  151. },
  152. {
  153. Name: "Choice 3",
  154. Value: "choice3",
  155. },
  156. {
  157. Name: "Choice 4",
  158. Value: "choice4",
  159. },
  160. {
  161. Name: "Choice 5",
  162. Value: "choice5",
  163. },
  164. }
  165. if data.Options[0].StringValue() != "" {
  166. choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
  167. Name: data.Options[0].StringValue(),
  168. Value: "choice_custom",
  169. })
  170. }
  171. case data.Options[1].Focused:
  172. choices = []*discordgo.ApplicationCommandOptionChoice{
  173. {
  174. Name: "Autocomplete 4 second option",
  175. Value: "autocomplete_1_default",
  176. },
  177. {
  178. Name: "Choice 3.1",
  179. Value: "choice3_1",
  180. },
  181. {
  182. Name: "Choice 4.1",
  183. Value: "choice4_1",
  184. },
  185. {
  186. Name: "Choice 5.1",
  187. Value: "choice5_1",
  188. },
  189. }
  190. if data.Options[1].StringValue() != "" {
  191. choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
  192. Name: data.Options[1].StringValue(),
  193. Value: "choice_custom_2",
  194. })
  195. }
  196. }
  197. err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  198. Type: discordgo.InteractionApplicationCommandAutocompleteResult,
  199. Data: &discordgo.InteractionResponseData{
  200. Choices: choices,
  201. },
  202. })
  203. if err != nil {
  204. panic(err)
  205. }
  206. }
  207. },
  208. }
  209. )
  210. func main() {
  211. s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { log.Println("Bot is up!") })
  212. s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
  213. if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
  214. h(s, i)
  215. }
  216. })
  217. err := s.Open()
  218. if err != nil {
  219. log.Fatalf("Cannot open the session: %v", err)
  220. }
  221. defer s.Close()
  222. createdCommands, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID, *GuildID, commands)
  223. if err != nil {
  224. log.Fatalf("Cannot register commands: %v", err)
  225. }
  226. stop := make(chan os.Signal, 1)
  227. signal.Notify(stop, os.Interrupt)
  228. <-stop
  229. log.Println("Gracefully shutting down")
  230. if *RemoveCommands {
  231. for _, cmd := range createdCommands {
  232. err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, cmd.ID)
  233. if err != nil {
  234. log.Fatalf("Cannot delete %q command: %v", cmd.Name, err)
  235. }
  236. }
  237. }
  238. }