refactor funbot to packages

This commit is contained in:
Seraphim Strub 2023-03-09 23:37:01 +01:00
parent 21c6e7a748
commit 5d96c929bb
5 changed files with 182 additions and 160 deletions

46
cmd/funbot/command/img.go Normal file
View file

@ -0,0 +1,46 @@
package command
import (
"errors"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"grow.rievo.dev/discordBots/cmd/funbot/imgur"
"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 {
event.Client().Logger().Error("error on getting link: ", err)
return
}
err = event.CreateMessage(discord.NewMessageCreateBuilder().
SetContent(link).
SetEphemeral(false).Build())
if err != nil {
event.Client().Logger().Error("error on sending response: ", err)
return
}
}