51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package cmds
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
func getImage(s *discordgo.Session, i *discordgo.InteractionCreate) (imageReader io.ReadCloser, err error) {
|
|
options := i.ApplicationCommandData().Options
|
|
|
|
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
|
|
for _, opt := range options {
|
|
optionMap[opt.Name] = opt
|
|
}
|
|
|
|
if option, ok := optionMap["user"]; ok {
|
|
if _, ok := optionMap["image"]; ok {
|
|
err = errors.New("Cannot use both user and image options")
|
|
return
|
|
}
|
|
calloutUser := option.UserValue(s)
|
|
|
|
userAvatarUrl := calloutUser.AvatarURL("256")
|
|
|
|
var avatarRes *http.Response
|
|
avatarRes, err = http.Get(userAvatarUrl)
|
|
if err != nil {
|
|
return
|
|
}
|
|
imageReader = avatarRes.Body
|
|
return
|
|
}
|
|
if option, ok := optionMap["image"]; ok {
|
|
var fileId string = option.Value.(string)
|
|
attachmentUrl := i.ApplicationCommandData().Resolved.Attachments[fileId].URL
|
|
var imageRes *http.Response
|
|
imageRes, err = http.Get(attachmentUrl)
|
|
if err != nil {
|
|
err = errors.New("Could not fetch attachment")
|
|
return
|
|
}
|
|
imageReader = imageRes.Body
|
|
return
|
|
}
|
|
err = errors.New("the command requires that you use either the user or the image option")
|
|
return
|
|
}
|