main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "os/signal"
  7. "github.com/bwmarrin/discordgo"
  8. )
  9. // Bot parameters
  10. var (
  11. GuildID = flag.String("guild", "", "Test guild ID")
  12. BotToken = flag.String("token", "", "Bot access token")
  13. AppID = flag.String("app", "", "Application ID")
  14. )
  15. var s *discordgo.Session
  16. func init() { flag.Parse() }
  17. func init() {
  18. var err error
  19. s, err = discordgo.New("Bot " + *BotToken)
  20. if err != nil {
  21. log.Fatalf("Invalid bot parameters: %v", err)
  22. }
  23. }
  24. func main() {
  25. s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
  26. log.Println("Bot is up!")
  27. })
  28. // Buttons are part of interactions, so we register InteractionCreate handler
  29. s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
  30. if i.Type == discordgo.InteractionApplicationCommand {
  31. if i.ApplicationCommandData().Name == "feedback" {
  32. err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  33. Type: discordgo.InteractionResponseChannelMessageWithSource,
  34. Data: &discordgo.InteractionResponseData{
  35. Content: "Are you satisfied with Buttons?",
  36. // Buttons and other components are specified in Components field.
  37. Components: []discordgo.MessageComponent{
  38. // ActionRow is a container of all buttons within the same row.
  39. discordgo.ActionsRow{
  40. Components: []discordgo.MessageComponent{
  41. discordgo.Button{
  42. Label: "Yes",
  43. Style: discordgo.SuccessButton,
  44. Disabled: false,
  45. CustomID: "yes_btn",
  46. },
  47. discordgo.Button{
  48. Label: "No",
  49. Style: discordgo.DangerButton,
  50. Disabled: false,
  51. CustomID: "no_btn",
  52. },
  53. discordgo.Button{
  54. Label: "I don't know",
  55. Style: discordgo.LinkButton,
  56. Disabled: false,
  57. // Link buttons don't require CustomID and do not trigger the gateway/HTTP event
  58. URL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
  59. Emoji: discordgo.ButtonEmoji{
  60. Name: "🤷",
  61. },
  62. },
  63. },
  64. },
  65. // The message may have multiple actions rows.
  66. discordgo.ActionsRow{
  67. Components: []discordgo.MessageComponent{
  68. discordgo.Button{
  69. Label: "Discord Developers server",
  70. Style: discordgo.LinkButton,
  71. Disabled: false,
  72. URL: "https://discord.gg/discord-developers",
  73. },
  74. },
  75. },
  76. },
  77. },
  78. })
  79. if err != nil {
  80. panic(err)
  81. }
  82. }
  83. return
  84. }
  85. // Type for button press will be always InteractionButton (3)
  86. if i.Type != discordgo.InteractionMessageComponent {
  87. return
  88. }
  89. content := "Thanks for your feedback "
  90. // CustomID field contains the same id as when was sent. It's used to identify the which button was clicked.
  91. switch i.MessageComponentData().CustomID {
  92. case "yes_btn":
  93. content += "(yes)"
  94. case "no_btn":
  95. content += "(no)"
  96. }
  97. s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  98. // Buttons also may update the message which to which they are attached.
  99. // Or may just acknowledge (InteractionResponseDeferredMessageUpdate) that the event was received and not update the message.
  100. // To update it later you need to use interaction response edit endpoint.
  101. Type: discordgo.InteractionResponseUpdateMessage,
  102. Data: &discordgo.InteractionResponseData{
  103. Content: content,
  104. Components: []discordgo.MessageComponent{
  105. discordgo.ActionsRow{
  106. Components: []discordgo.MessageComponent{
  107. discordgo.Button{
  108. Label: "Our sponsor",
  109. Style: discordgo.LinkButton,
  110. Disabled: false,
  111. URL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
  112. Emoji: discordgo.ButtonEmoji{
  113. Name: "💠",
  114. },
  115. },
  116. },
  117. },
  118. },
  119. },
  120. })
  121. })
  122. _, err := s.ApplicationCommandCreate(*AppID, *GuildID, &discordgo.ApplicationCommand{
  123. Name: "feedback",
  124. Description: "Give your feedback",
  125. })
  126. if err != nil {
  127. log.Fatalf("Cannot create slash command: %v", err)
  128. }
  129. err = s.Open()
  130. if err != nil {
  131. log.Fatalf("Cannot open the session: %v", err)
  132. }
  133. defer s.Close()
  134. stop := make(chan os.Signal, 1)
  135. signal.Notify(stop, os.Interrupt)
  136. <-stop
  137. log.Println("Graceful shutdown")
  138. }