2022-07-21 15:25:10 +00:00
|
|
|
package cowboy
|
|
|
|
|
|
|
|
import (
|
2022-07-21 15:39:50 +00:00
|
|
|
"fmt"
|
2022-07-23 04:17:44 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"github.com/velour/catbase/plugins/emojy"
|
2022-07-21 15:25:10 +00:00
|
|
|
"regexp"
|
2022-10-19 18:49:28 +00:00
|
|
|
"strings"
|
2022-07-21 15:25:10 +00:00
|
|
|
|
2022-07-22 21:06:55 +00:00
|
|
|
"github.com/velour/catbase/connectors/discord"
|
|
|
|
|
2022-07-21 15:25:10 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Cowboy struct {
|
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
2022-07-22 13:44:42 +00:00
|
|
|
|
|
|
|
emojyPath string
|
|
|
|
baseEmojyURL string
|
2022-07-21 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
var defaultOverlays = map[string]string{"hat": "hat"}
|
|
|
|
|
2022-07-21 15:25:10 +00:00
|
|
|
func New(b bot.Bot) *Cowboy {
|
2022-07-22 13:44:42 +00:00
|
|
|
emojyPath := b.Config().Get("emojy.path", "emojy")
|
|
|
|
baseURL := b.Config().Get("emojy.baseURL", "/emojy/file")
|
2022-07-21 15:25:10 +00:00
|
|
|
c := Cowboy{
|
2022-07-22 13:44:42 +00:00
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
emojyPath: emojyPath,
|
|
|
|
baseEmojyURL: baseURL,
|
2022-07-21 15:25:10 +00:00
|
|
|
}
|
|
|
|
c.register()
|
|
|
|
c.registerWeb()
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Cowboy) register() {
|
|
|
|
tbl := bot.HandlerTable{
|
2022-07-23 04:17:44 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Startup, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`.*`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
switch conn := r.Conn.(type) {
|
|
|
|
case *discord.Discord:
|
|
|
|
p.registerCmds(conn)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
2022-07-22 21:06:55 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^:cowboy_clear_cache:$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
cowboyClearCache()
|
|
|
|
p.b.Send(r.Conn, bot.Ephemeral, r.Msg.Channel, r.Msg.User.ID, ":cowboy_cache_cleared:")
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
2022-07-21 15:25:10 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^:cowboy_(?P<what>.+):$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
p.makeCowboy(r)
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p.b.RegisterTable(p, tbl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Cowboy) makeCowboy(r bot.Request) {
|
2022-07-23 04:17:44 +00:00
|
|
|
overlays := p.c.GetMap("cowboy.overlays", defaultOverlays)
|
|
|
|
hat := overlays["hat"]
|
2022-10-19 18:49:28 +00:00
|
|
|
what := r.Values["what"]
|
|
|
|
_, err := p.mkEmojy(what, hat)
|
2022-07-22 21:06:55 +00:00
|
|
|
if err != nil {
|
2022-10-19 18:49:28 +00:00
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Couldn't cowboify that, pardner.")
|
2022-07-22 21:06:55 +00:00
|
|
|
return
|
|
|
|
}
|
2022-07-21 15:39:50 +00:00
|
|
|
p.b.Send(r.Conn, bot.Delete, r.Msg.Channel, r.Msg.ID)
|
2022-10-19 18:49:28 +00:00
|
|
|
e := ":cowboy_" + what + ":"
|
|
|
|
|
|
|
|
switch c := r.Conn.(type) {
|
|
|
|
case *discord.Discord:
|
|
|
|
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, e)
|
2022-07-21 15:25:10 +00:00
|
|
|
}
|
2022-07-22 21:04:51 +00:00
|
|
|
|
|
|
|
func (p *Cowboy) registerCmds(d *discord.Discord) {
|
2022-07-23 04:17:44 +00:00
|
|
|
log.Debug().Msg("About to register some startup commands")
|
|
|
|
cmd := discordgo.ApplicationCommand{
|
|
|
|
Name: "cowboy",
|
|
|
|
Description: "cowboy-ify an emojy",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
Name: "emojy",
|
|
|
|
Description: "which emojy you want cowboied",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
overlays := p.c.GetMap("cowboy.overlays", defaultOverlays)
|
|
|
|
hat := overlays["hat"]
|
2022-07-23 04:37:00 +00:00
|
|
|
log.Debug().Msgf("Overlay: %s", hat)
|
2022-07-23 04:17:44 +00:00
|
|
|
if err := d.RegisterSlashCmd(cmd, p.mkOverlayCB(hat)); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not register cowboy command")
|
|
|
|
}
|
2022-07-23 04:19:15 +00:00
|
|
|
|
|
|
|
if p.c.GetInt("cowboy.overlaysEnabled", 0) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
cmd = discordgo.ApplicationCommand{
|
|
|
|
Name: "overlay",
|
|
|
|
Description: "overlay-ify an emojy",
|
|
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
|
|
{
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
Name: "overlay",
|
|
|
|
Description: "which overlay you want",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
|
|
Name: "emojy",
|
|
|
|
Description: "which emojy you want overlaid",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := d.RegisterSlashCmd(cmd, p.mkOverlayCB("")); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not register cowboy command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 18:49:28 +00:00
|
|
|
func (p *Cowboy) mkEmojy(name, overlay string) (string, error) {
|
|
|
|
lastEmojy := p.c.Get("cowboy.lastEmojy", "rust")
|
|
|
|
emojyPlugin := emojy.NewAPI(p.b)
|
|
|
|
list := map[string]string{}
|
|
|
|
|
|
|
|
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",
|
|
|
|
name, overlay, lastEmojy)
|
|
|
|
prefix := overlay
|
|
|
|
|
|
|
|
newEmojy, err := cowboy(p.emojyPath, p.baseEmojyURL, overlay, name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = emojyPlugin.RmEmojy(p.b.DefaultConnector(), lastEmojy)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if overlay == "hat" {
|
|
|
|
prefix = "cowboy"
|
|
|
|
}
|
|
|
|
name = emojy.SanitizeName(prefix + "_" + name)
|
|
|
|
err = emojyPlugin.UploadEmojyImage(p.b.DefaultConnector(), name, newEmojy)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.c.Set("cowboy.lastEmojy", name)
|
|
|
|
|
|
|
|
list = emojy.InvertEmojyList(p.b.DefaultConnector().GetEmojiList(true))
|
|
|
|
msg = fmt.Sprintf("You replaced %s with a new emojy %s <:%s:%s>, pardner!",
|
|
|
|
lastEmojy, name, name, list[name])
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-10-19 18:49:28 +00:00
|
|
|
msg, err := p.mkEmojy(name, overlay)
|
2022-07-23 04:17:44 +00:00
|
|
|
if err != nil {
|
2022-10-19 18:49:28 +00:00
|
|
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: err.Error(),
|
|
|
|
Flags: discordgo.MessageFlagsEphemeral,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return
|
2022-07-23 04:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
|
|
Data: &discordgo.InteractionResponseData{
|
|
|
|
Content: msg,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-07-22 21:04:51 +00:00
|
|
|
}
|