main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/signal"
  8. "strings"
  9. "github.com/bwmarrin/discordgo"
  10. )
  11. // Bot parameters
  12. var (
  13. GuildID = flag.String("guild", "", "Test guild ID")
  14. BotToken = flag.String("token", "", "Bot access token")
  15. AppID = flag.String("app", "", "Application ID")
  16. Cleanup = flag.Bool("cleanup", true, "Cleanup of commands")
  17. ResultsChannel = flag.String("results", "", "Channel where send survey results to")
  18. )
  19. var s *discordgo.Session
  20. func init() {
  21. flag.Parse()
  22. }
  23. func init() {
  24. var err error
  25. s, err = discordgo.New("Bot " + *BotToken)
  26. if err != nil {
  27. log.Fatalf("Invalid bot parameters: %v", err)
  28. }
  29. }
  30. var (
  31. commands = []discordgo.ApplicationCommand{
  32. {
  33. Name: "modals-survey",
  34. Description: "Take a survey about modals",
  35. },
  36. }
  37. commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
  38. "modals-survey": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
  39. err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  40. Type: discordgo.InteractionResponseModal,
  41. Data: &discordgo.InteractionResponseData{
  42. CustomID: "modals_survey_" + i.Interaction.Member.User.ID,
  43. Title: "Modals survey",
  44. Components: []discordgo.MessageComponent{
  45. discordgo.ActionsRow{
  46. Components: []discordgo.MessageComponent{
  47. discordgo.TextInput{
  48. CustomID: "opinion",
  49. Label: "What is your opinion on them?",
  50. Style: discordgo.TextInputShort,
  51. Placeholder: "Don't be shy, share your opinion with us",
  52. Required: true,
  53. MaxLength: 300,
  54. MinLength: 10,
  55. },
  56. },
  57. },
  58. discordgo.ActionsRow{
  59. Components: []discordgo.MessageComponent{
  60. discordgo.TextInput{
  61. CustomID: "suggestions",
  62. Label: "What would you suggest to improve them?",
  63. Style: discordgo.TextInputParagraph,
  64. Required: false,
  65. MaxLength: 2000,
  66. },
  67. },
  68. },
  69. },
  70. },
  71. })
  72. if err != nil {
  73. panic(err)
  74. }
  75. },
  76. }
  77. )
  78. func main() {
  79. s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
  80. log.Println("Bot is up!")
  81. })
  82. s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
  83. switch i.Type {
  84. case discordgo.InteractionApplicationCommand:
  85. if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
  86. h(s, i)
  87. }
  88. case discordgo.InteractionModalSubmit:
  89. err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
  90. Type: discordgo.InteractionResponseChannelMessageWithSource,
  91. Data: &discordgo.InteractionResponseData{
  92. Content: "Thank you for taking your time to fill this survey",
  93. Flags: 1 << 6,
  94. },
  95. })
  96. if err != nil {
  97. panic(err)
  98. }
  99. data := i.ModalSubmitData()
  100. if !strings.HasPrefix(data.CustomID, "modals_survey") {
  101. return
  102. }
  103. userid := strings.Split(data.CustomID, "_")[2]
  104. _, err = s.ChannelMessageSend(*ResultsChannel, fmt.Sprintf(
  105. "Feedback received. From <@%s>\n\n**Opinion**:\n%s\n\n**Suggestions**:\n%s",
  106. userid,
  107. data.Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value,
  108. data.Components[1].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value,
  109. ))
  110. if err != nil {
  111. panic(err)
  112. }
  113. }
  114. })
  115. cmdIDs := make(map[string]string, len(commands))
  116. for _, cmd := range commands {
  117. rcmd, err := s.ApplicationCommandCreate(*AppID, *GuildID, &cmd)
  118. if err != nil {
  119. log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
  120. }
  121. cmdIDs[rcmd.ID] = rcmd.Name
  122. }
  123. err := s.Open()
  124. if err != nil {
  125. log.Fatalf("Cannot open the session: %v", err)
  126. }
  127. defer s.Close()
  128. stop := make(chan os.Signal, 1)
  129. signal.Notify(stop, os.Interrupt)
  130. <-stop
  131. log.Println("Graceful shutdown")
  132. if !*Cleanup {
  133. return
  134. }
  135. for id, name := range cmdIDs {
  136. err := s.ApplicationCommandDelete(*AppID, *GuildID, id)
  137. if err != nil {
  138. log.Fatalf("Cannot delete slash command %q: %v", name, err)
  139. }
  140. }
  141. }