123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package main
- import (
- "flag"
- "log"
- "os"
- "os/signal"
- "github.com/bwmarrin/discordgo"
- )
- // Bot parameters
- var (
- GuildID = flag.String("guild", "", "Test guild ID")
- BotToken = flag.String("token", "", "Bot access token")
- AppID = flag.String("app", "", "Application ID")
- )
- var s *discordgo.Session
- func init() { flag.Parse() }
- func init() {
- var err error
- s, err = discordgo.New("Bot " + *BotToken)
- if err != nil {
- log.Fatalf("Invalid bot parameters: %v", err)
- }
- }
- func main() {
- s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
- log.Println("Bot is up!")
- })
- // Buttons are part of interactions, so we register InteractionCreate handler
- s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
- if i.Type == discordgo.InteractionApplicationCommand {
- if i.ApplicationCommandData().Name == "feedback" {
- err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
- Type: discordgo.InteractionResponseChannelMessageWithSource,
- Data: &discordgo.InteractionResponseData{
- Content: "Are you satisfied with Buttons?",
- // Buttons and other components are specified in Components field.
- Components: []discordgo.MessageComponent{
- // ActionRow is a container of all buttons within the same row.
- discordgo.ActionsRow{
- Components: []discordgo.MessageComponent{
- discordgo.Button{
- Label: "Yes",
- Style: discordgo.SuccessButton,
- Disabled: false,
- CustomID: "yes_btn",
- },
- discordgo.Button{
- Label: "No",
- Style: discordgo.DangerButton,
- Disabled: false,
- CustomID: "no_btn",
- },
- discordgo.Button{
- Label: "I don't know",
- Style: discordgo.LinkButton,
- Disabled: false,
- // Link buttons don't require CustomID and do not trigger the gateway/HTTP event
- URL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
- Emoji: discordgo.ButtonEmoji{
- Name: "🤷",
- },
- },
- },
- },
- // The message may have multiple actions rows.
- discordgo.ActionsRow{
- Components: []discordgo.MessageComponent{
- discordgo.Button{
- Label: "Discord Developers server",
- Style: discordgo.LinkButton,
- Disabled: false,
- URL: "https://discord.gg/discord-developers",
- },
- },
- },
- },
- },
- })
- if err != nil {
- panic(err)
- }
- }
- return
- }
- // Type for button press will be always InteractionButton (3)
- if i.Type != discordgo.InteractionMessageComponent {
- return
- }
- content := "Thanks for your feedback "
- // CustomID field contains the same id as when was sent. It's used to identify the which button was clicked.
- switch i.MessageComponentData().CustomID {
- case "yes_btn":
- content += "(yes)"
- case "no_btn":
- content += "(no)"
- }
- s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
- // Buttons also may update the message which to which they are attached.
- // Or may just acknowledge (InteractionResponseDeferredMessageUpdate) that the event was received and not update the message.
- // To update it later you need to use interaction response edit endpoint.
- Type: discordgo.InteractionResponseUpdateMessage,
- Data: &discordgo.InteractionResponseData{
- Content: content,
- Components: []discordgo.MessageComponent{
- discordgo.ActionsRow{
- Components: []discordgo.MessageComponent{
- discordgo.Button{
- Label: "Our sponsor",
- Style: discordgo.LinkButton,
- Disabled: false,
- URL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
- Emoji: discordgo.ButtonEmoji{
- Name: "💠",
- },
- },
- },
- },
- },
- },
- })
- })
- _, err := s.ApplicationCommandCreate(*AppID, *GuildID, &discordgo.ApplicationCommand{
- Name: "feedback",
- Description: "Give your feedback",
- })
- if err != nil {
- log.Fatalf("Cannot create slash command: %v", err)
- }
- err = s.Open()
- if err != nil {
- log.Fatalf("Cannot open the session: %v", err)
- }
- defer s.Close()
- stop := make(chan os.Signal, 1)
- signal.Notify(stop, os.Interrupt)
- <-stop
- log.Println("Graceful shutdown")
- }
|