Closed XangelMusic closed 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:
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,
},
},
},
}
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),
},
})
},
}
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.
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.
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?