Compare commits

..

2 Commits

Author SHA1 Message Date
Chris Sexton da780a7a92 cowboy: who sent that emojy? 2022-10-19 18:08:25 -04:00
Chris Sexton e8ca86d008 cowboy: make the bare command make an emojy 2022-10-19 14:52:31 -04:00
1 changed files with 61 additions and 49 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/velour/catbase/plugins/emojy" "github.com/velour/catbase/plugins/emojy"
"regexp" "regexp"
"strings"
"github.com/velour/catbase/connectors/discord" "github.com/velour/catbase/connectors/discord"
@ -72,26 +73,25 @@ func (p *Cowboy) register() {
} }
func (p *Cowboy) makeCowboy(r bot.Request) { func (p *Cowboy) makeCowboy(r bot.Request) {
what := r.Values["what"]
// This'll add the image to the cowboy_cache before discord tries to access it over http
overlays := p.c.GetMap("cowboy.overlays", defaultOverlays) overlays := p.c.GetMap("cowboy.overlays", defaultOverlays)
hat := overlays["hat"] hat := overlays["hat"]
i, err := cowboy(p.emojyPath, p.baseEmojyURL, hat, what) what := r.Values["what"]
_, err := p.mkEmojy(what, hat)
if err != nil { if err != nil {
log.Error().Err(err).Msg(":cowboy_fail:") p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Couldn't cowboify that, pardner.")
p.b.Send(r.Conn, bot.Ephemeral, r.Msg.Channel, r.Msg.User.ID, "Hey cowboy, that image wasn't there.")
return return
} }
log.Debug().Msgf("makeCowboy: %s", r.Values["what"])
base := p.c.Get("baseURL", "http://127.0.0.1:1337")
u := base + "/cowboy/img/hat/" + r.Values["what"]
p.b.Send(r.Conn, bot.Delete, r.Msg.Channel, r.Msg.ID) p.b.Send(r.Conn, bot.Delete, r.Msg.Channel, r.Msg.ID)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "", bot.ImageAttachment{ e := ":cowboy_" + what + ":"
URL: u,
AltTxt: fmt.Sprintf("%s: %s", r.Msg.User.Name, r.Msg.Body), switch c := r.Conn.(type) {
Width: i.Bounds().Max.X, case *discord.Discord:
Height: i.Bounds().Max.Y, list := emojy.InvertEmojyList(c.GetEmojiList(true))
}) e = strings.Trim(e, ":")
e = fmt.Sprintf("<:%s:%s>", e, list[e])
}
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, r.Msg.User.Name+":")
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, e)
} }
func (p *Cowboy) registerCmds(d *discord.Discord) { func (p *Cowboy) registerCmds(d *discord.Discord) {
@ -142,17 +142,11 @@ func (p *Cowboy) registerCmds(d *discord.Discord) {
} }
} }
func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *discordgo.InteractionCreate) { func (p *Cowboy) mkEmojy(name, overlay string) (string, error) {
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
lastEmojy := p.c.Get("cowboy.lastEmojy", "rust") lastEmojy := p.c.Get("cowboy.lastEmojy", "rust")
emojyPlugin := emojy.NewAPI(p.b) emojyPlugin := emojy.NewAPI(p.b)
list := map[string]string{} list := map[string]string{}
name := i.ApplicationCommandData().Options[0].StringValue()
if overlay == "" {
overlay = name
name = i.ApplicationCommandData().Options[1].StringValue()
}
msg := fmt.Sprintf("You asked for %s overlaid by %s", name, overlay) msg := fmt.Sprintf("You asked for %s overlaid by %s", name, overlay)
log.Debug().Msgf("got a cowboy command for %s overlaid by %s replacing %s", log.Debug().Msgf("got a cowboy command for %s overlaid by %s replacing %s",
name, overlay, lastEmojy) name, overlay, lastEmojy)
@ -160,14 +154,12 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco
newEmojy, err := cowboy(p.emojyPath, p.baseEmojyURL, overlay, name) newEmojy, err := cowboy(p.emojyPath, p.baseEmojyURL, overlay, name)
if err != nil { if err != nil {
msg = err.Error() return "", err
goto resp
} }
err = emojyPlugin.RmEmojy(p.b.DefaultConnector(), lastEmojy) err = emojyPlugin.RmEmojy(p.b.DefaultConnector(), lastEmojy)
if err != nil { if err != nil {
msg = err.Error() return "", err
goto resp
} }
if overlay == "hat" { if overlay == "hat" {
@ -176,8 +168,7 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco
name = emojy.SanitizeName(prefix + "_" + name) name = emojy.SanitizeName(prefix + "_" + name)
err = emojyPlugin.UploadEmojyImage(p.b.DefaultConnector(), name, newEmojy) err = emojyPlugin.UploadEmojyImage(p.b.DefaultConnector(), name, newEmojy)
if err != nil { if err != nil {
msg = err.Error() return "", err
goto resp
} }
p.c.Set("cowboy.lastEmojy", name) p.c.Set("cowboy.lastEmojy", name)
@ -186,12 +177,33 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco
msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!", msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!",
lastEmojy, name, name, list[name]) lastEmojy, name, name, list[name])
resp: return msg, nil
}
func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *discordgo.InteractionCreate) {
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
name := i.ApplicationCommandData().Options[0].StringValue()
if overlay == "" {
overlay = name
name = i.ApplicationCommandData().Options[1].StringValue()
}
msg, err := p.mkEmojy(name, overlay)
if err != nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: err.Error(),
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: msg, Content: msg,
Flags: discordgo.MessageFlagsEphemeral,
}, },
}) })
} }