bwmarrin / discordgo

(Golang) Go bindings for Discord
BSD 3-Clause "New" or "Revised" License
5.11k stars 816 forks source link

How do I use `Interaction.Message` in a slash command? #1486

Closed XangelMusic closed 9 months ago

XangelMusic commented 10 months ago

I'm trying to create a slash command that gets the attachments of a message the interaction is used on.

Interaction.Message however says "this field is only filled when a button click triggered the interaction. Otherwise it will be nil." which seems to be the case whenever I use the command on a message.

How do I then get a message and all its data with a slash command?

bshore commented 9 months ago

Assuming you're following the example case here - https://github.com/bwmarrin/discordgo/blob/master/examples/slash_commands/main.go

I just encountered this with my Discord Bot and here's what I ended up having to do:

  1. Set up the slash command to accept an option
    commands := []*discordgo.ApplicationCommand{
    {
        Name:        "respond",
        Description: "example command with a message option",
        Options: []*discordgo.ApplicationCommandOption{
            {
                Type:        discordgo.ApplicationCommandOptionString,
                Name:        "message",
                Description: "The message to respond with",
                Required:    true,
            },
        },
    },
    }
  2. Where you handle your interaction, the message is part of the interaction struct:

    commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
    "respond": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
        // commandData has all the stuff captured by the command interaction
        commandData := i.ApplicationCommandData()
    
        // option '0' here is the 'message' option defined above
        message := commandData.Options[0].StringValue()
    
         // do something with 'message' i.e. look up info, call other apis, etc.
    
        s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
            Type: discordgo.InteractionResponseChannelMessageWithSource,
            Data: &discordgo.InteractionResponseData{
                Content: fmt.Sprintf("the bot handled the message: %s", message),
            },
        })
    },
    }
  3. Should get you a slash command something like this within Discord: image
FedorLap2006 commented 9 months ago

I'm trying to create a slash command that gets the attachments of a message the interaction is used on.

You can't use slash commands on messages. But you can use context menu (right click) commands. See the examples for more info. Though, if you don't want to use them - the alternative way could be to pass a message id, and then fetch it.

XangelMusic commented 9 months ago

This is what I was looking for, thank you. I was definitely looking in the wrong place, I was supposed to look at the Context Menus example. I confused myself thinking the Context Menu command was the same as a Slash Command.