github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/examples/modals/main.go (about)

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