github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/examples/echo/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "log" 6 "os" 7 "os/signal" 8 "strings" 9 10 "github.com/bwmarrin/discordgo" 11 ) 12 13 type optionMap = map[string]*discordgo.ApplicationCommandInteractionDataOption 14 15 func parseOptions(options []*discordgo.ApplicationCommandInteractionDataOption) (om optionMap) { 16 om = make(optionMap) 17 for _, opt := range options { 18 om[opt.Name] = opt 19 } 20 return 21 } 22 23 func interactionAuthor(i *discordgo.Interaction) *discordgo.User { 24 if i.Member != nil { 25 return i.Member.User 26 } 27 return i.User 28 } 29 30 func handleEcho(s *discordgo.Session, i *discordgo.InteractionCreate, opts optionMap) { 31 builder := new(strings.Builder) 32 if v, ok := opts["author"]; ok && v.BoolValue() { 33 author := interactionAuthor(i.Interaction) 34 builder.WriteString("**" + author.String() + "** says: ") 35 } 36 builder.WriteString(opts["message"].StringValue()) 37 38 err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ 39 Type: discordgo.InteractionResponseChannelMessageWithSource, 40 Data: &discordgo.InteractionResponseData{ 41 Content: builder.String(), 42 }, 43 }) 44 45 if err != nil { 46 log.Panicf("could not respond to interaction: %s", err) 47 } 48 } 49 50 var commands = []*discordgo.ApplicationCommand{ 51 { 52 Name: "echo", 53 Description: "Say something through a bot", 54 Options: []*discordgo.ApplicationCommandOption{ 55 { 56 Name: "message", 57 Description: "Contents of the message", 58 Type: discordgo.ApplicationCommandOptionString, 59 Required: true, 60 }, 61 { 62 Name: "author", 63 Description: "Whether to prepend message's author", 64 Type: discordgo.ApplicationCommandOptionBoolean, 65 }, 66 }, 67 }, 68 } 69 70 var ( 71 Token = flag.String("token", "", "Bot authentication token") 72 App = flag.String("app", "", "Application ID") 73 Guild = flag.String("guild", "", "Guild ID") 74 ) 75 76 func main() { 77 flag.Parse() 78 if *App == "" { 79 log.Fatal("application id is not set") 80 } 81 82 session, _ := discordgo.New("Bot " + *Token) 83 84 session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { 85 if i.Type != discordgo.InteractionApplicationCommand { 86 return 87 } 88 89 data := i.ApplicationCommandData() 90 if data.Name != "echo" { 91 return 92 } 93 94 handleEcho(s, i, parseOptions(data.Options)) 95 }) 96 97 session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { 98 log.Printf("Logged in as %s", r.User.String()) 99 }) 100 101 _, err := session.ApplicationCommandBulkOverwrite(*App, *Guild, commands) 102 if err != nil { 103 log.Fatalf("could not register commands: %s", err) 104 } 105 106 err = session.Open() 107 if err != nil { 108 log.Fatalf("could not open session: %s", err) 109 } 110 111 sigch := make(chan os.Signal, 1) 112 signal.Notify(sigch, os.Interrupt) 113 <-sigch 114 115 err = session.Close() 116 if err != nil { 117 log.Printf("could not close session gracefully: %s", err) 118 } 119 }