discordBots/cmd/funbot/command/img.go

48 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-03-09 22:37:01 +00:00
package command
import (
"errors"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"grow.rievo.dev/discordBots/cmd/funbot/imgur"
"log"
2023-03-09 22:37:01 +00:00
"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)
2023-03-09 22:37:01 +00:00
return
}
err = event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(link).
SetEphemeral(false).Build())
if err != nil {
log.Printf("ERROR: error on sending response: %v", err)
2023-03-09 22:37:01 +00:00
return
}
}