discordBots/cmd/funbot/command/img.go
2023-04-23 00:33:20 +02:00

47 lines
1.1 KiB
Go

package command
import (
"errors"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"grow.rievo.dev/discordBots/cmd/funbot/imgur"
"log"
"strings"
)
func Listener(event *events.ApplicationCommandInteractionCreate) {
data := event.SlashCommandInteractionData()
if data.CommandName() == "img" {
handlerImg(event)
}
}
func handlerImg(event *events.ApplicationCommandInteractionCreate) {
data := event.SlashCommandInteractionData()
value := data.String("type")
link, err := func(value string) (string, error) {
switch {
case strings.HasPrefix(value, "imgur-"):
link, err := imgur.GetRandomImgur(strings.TrimPrefix(value, "imgur-"))
if err != nil {
return "", err
}
return link, nil
default:
return "", errors.New("value not found")
}
}(value)
if err != nil {
log.Printf("ERROR: error on getting link: %v", err)
return
}
err = event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(link).
SetEphemeral(false).Build())
if err != nil {
log.Printf("ERROR: error on sending response: %v", err)
return
}
}