github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/examples/context_menus/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  )
    21  
    22  var s *discordgo.Session
    23  
    24  func init() { flag.Parse() }
    25  
    26  func init() {
    27  	var err error
    28  	s, err = discordgo.New("Bot " + *BotToken)
    29  	if err != nil {
    30  		log.Fatalf("Invalid bot parameters: %v", err)
    31  	}
    32  }
    33  
    34  func searchLink(message, format, sep string) string {
    35  	return fmt.Sprintf(format, strings.Join(
    36  		strings.Split(
    37  			message,
    38  			" ",
    39  		),
    40  		sep,
    41  	))
    42  }
    43  
    44  var (
    45  	commands = []discordgo.ApplicationCommand{
    46  		{
    47  			Name: "rickroll-em",
    48  			Type: discordgo.UserApplicationCommand,
    49  		},
    50  		{
    51  			Name: "google-it",
    52  			Type: discordgo.MessageApplicationCommand,
    53  		},
    54  		{
    55  			Name: "stackoverflow-it",
    56  			Type: discordgo.MessageApplicationCommand,
    57  		},
    58  		{
    59  			Name: "godoc-it",
    60  			Type: discordgo.MessageApplicationCommand,
    61  		},
    62  		{
    63  			Name: "discordjs-it",
    64  			Type: discordgo.MessageApplicationCommand,
    65  		},
    66  		{
    67  			Name: "discordpy-it",
    68  			Type: discordgo.MessageApplicationCommand,
    69  		},
    70  	}
    71  	commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
    72  		"rickroll-em": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
    73  			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
    74  				Type: discordgo.InteractionResponseChannelMessageWithSource,
    75  				Data: &discordgo.InteractionResponseData{
    76  					Content: "Operation rickroll has begun",
    77  					Flags:   discordgo.MessageFlagsEphemeral,
    78  				},
    79  			})
    80  			if err != nil {
    81  				panic(err)
    82  			}
    83  
    84  			ch, err := s.UserChannelCreate(
    85  				i.ApplicationCommandData().TargetID,
    86  			)
    87  			if err != nil {
    88  				_, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
    89  					Content: fmt.Sprintf("Mission failed. Cannot send a message to this user: %q", err.Error()),
    90  					Flags:   discordgo.MessageFlagsEphemeral,
    91  				})
    92  				if err != nil {
    93  					panic(err)
    94  				}
    95  			}
    96  			_, err = s.ChannelMessageSend(
    97  				ch.ID,
    98  				fmt.Sprintf("%s sent you this: https://youtu.be/dQw4w9WgXcQ", i.Member.Mention()),
    99  			)
   100  			if err != nil {
   101  				panic(err)
   102  			}
   103  		},
   104  		"google-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
   105  			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
   106  				Type: discordgo.InteractionResponseChannelMessageWithSource,
   107  				Data: &discordgo.InteractionResponseData{
   108  					Content: searchLink(
   109  						i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
   110  						"https://google.com/search?q=%s", "+"),
   111  					Flags: discordgo.MessageFlagsEphemeral,
   112  				},
   113  			})
   114  			if err != nil {
   115  				panic(err)
   116  			}
   117  		},
   118  		"stackoverflow-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
   119  			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
   120  				Type: discordgo.InteractionResponseChannelMessageWithSource,
   121  				Data: &discordgo.InteractionResponseData{
   122  					Content: searchLink(
   123  						i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
   124  						"https://stackoverflow.com/search?q=%s", "+"),
   125  					Flags: discordgo.MessageFlagsEphemeral,
   126  				},
   127  			})
   128  			if err != nil {
   129  				panic(err)
   130  			}
   131  		},
   132  		"godoc-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
   133  			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
   134  				Type: discordgo.InteractionResponseChannelMessageWithSource,
   135  				Data: &discordgo.InteractionResponseData{
   136  					Content: searchLink(
   137  						i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
   138  						"https://pkg.go.dev/search?q=%s", "+"),
   139  					Flags: discordgo.MessageFlagsEphemeral,
   140  				},
   141  			})
   142  			if err != nil {
   143  				panic(err)
   144  			}
   145  		},
   146  		"discordjs-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
   147  			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
   148  				Type: discordgo.InteractionResponseChannelMessageWithSource,
   149  				Data: &discordgo.InteractionResponseData{
   150  					Content: searchLink(
   151  						i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
   152  						"https://discord.js.org/#/docs/main/stable/search?query=%s", "+"),
   153  					Flags: discordgo.MessageFlagsEphemeral,
   154  				},
   155  			})
   156  			if err != nil {
   157  				panic(err)
   158  			}
   159  		},
   160  		"discordpy-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
   161  			err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
   162  				Type: discordgo.InteractionResponseChannelMessageWithSource,
   163  				Data: &discordgo.InteractionResponseData{
   164  					Content: searchLink(
   165  						i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
   166  						"https://discordpy.readthedocs.io/en/stable/search.html?q=%s", "+"),
   167  					Flags: discordgo.MessageFlagsEphemeral,
   168  				},
   169  			})
   170  			if err != nil {
   171  				panic(err)
   172  			}
   173  		},
   174  	}
   175  )
   176  
   177  func main() {
   178  	s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
   179  		log.Println("Bot is up!")
   180  	})
   181  
   182  	s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
   183  		if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
   184  			h(s, i)
   185  		}
   186  	})
   187  
   188  	cmdIDs := make(map[string]string, len(commands))
   189  
   190  	for _, cmd := range commands {
   191  		rcmd, err := s.ApplicationCommandCreate(*AppID, *GuildID, &cmd)
   192  		if err != nil {
   193  			log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
   194  		}
   195  
   196  		cmdIDs[rcmd.ID] = rcmd.Name
   197  
   198  	}
   199  
   200  	err := s.Open()
   201  	if err != nil {
   202  		log.Fatalf("Cannot open the session: %v", err)
   203  	}
   204  	defer s.Close()
   205  
   206  	stop := make(chan os.Signal, 1)
   207  	signal.Notify(stop, os.Interrupt)
   208  	<-stop
   209  	log.Println("Graceful shutdown")
   210  
   211  	if !*Cleanup {
   212  		return
   213  	}
   214  
   215  	for id, name := range cmdIDs {
   216  		err := s.ApplicationCommandDelete(*AppID, *GuildID, id)
   217  		if err != nil {
   218  			log.Fatalf("Cannot delete slash command %q: %v", name, err)
   219  		}
   220  	}
   221  
   222  }