package discordbot import ( "fmt" "github.com/bwmarrin/discordgo" ) type DiscordBot struct { Session *discordgo.Session } func NewDiscordBot(token string) (*DiscordBot, error) { discord, err := discordgo.New("Bot " + token) if err != nil { return nil, err } return &DiscordBot{ Session: discord, }, nil } type CommandHandler struct { Command discordgo.ApplicationCommand Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) } func CreateRepoHandler(repoNameMinLength int) CommandHandler { return CommandHandler{ Command: discordgo.ApplicationCommand{ Name: "repo", Description: "Command for repository creation", Options: []*discordgo.ApplicationCommandOption{ { Type: discordgo.ApplicationCommandOptionString, Name: "repo_type", Description: "The type of repo", Required: true, Choices: []*discordgo.ApplicationCommandOptionChoice{ { Name: "Unity project repo", Value: "project_repo", }, { Name: "XCode build repo", Value: "build_repo", }, }, }, { Type: discordgo.ApplicationCommandOptionString, Name: "repo_name", Description: "Type the repository's name", Required: true, MinLength: &repoNameMinLength, }, }, }, Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { // Access options in the order provided by the user. options := i.ApplicationCommandData().Options // Or convert the slice into a map optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options)) for _, opt := range options { optionMap[opt.Name] = opt } // This example stores the provided arguments in an []interface{} // which will be used to format the bot's response margs := make([]interface{}, 0, len(options)) msgformat := "You learned how to use command options! " + "Take a look at the value(s) you entered:\n" if option, ok := optionMap["repo_type"]; ok { // Option values must be type asserted from interface{}. // Discordgo provides utility functions to make this simple. margs = append(margs, option.StringValue()) msgformat += "> string-option: %s\n" } if option, ok := optionMap["repo_name"]; ok { // Option values must be type asserted from interface{}. // Discordgo provides utility functions to make this simple. margs = append(margs, option.StringValue()) msgformat += "> string-option: %s\n" } s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ // Ignore type for now, they will be discussed in "responses" Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: fmt.Sprintf( msgformat, margs..., ), }, }) }, } }