2022-07-21 15:25:10 +00:00
|
|
|
package cowboy
|
|
|
|
|
|
|
|
import (
|
2022-07-21 15:39:50 +00:00
|
|
|
"fmt"
|
2022-07-22 21:04:51 +00:00
|
|
|
"github.com/velour/catbase/connectors/discord"
|
2022-07-21 15:25:10 +00:00
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2022-07-22 21:04:51 +00:00
|
|
|
switch conn := b.DefaultConnector().(type) {
|
|
|
|
case *discord.Discord:
|
|
|
|
c.registerCmds(conn)
|
|
|
|
}
|
2022-07-21 15:25:10 +00:00
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Cowboy) register() {
|
|
|
|
tbl := bot.HandlerTable{
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
log.Debug().Msgf("makeCowboy: %s", r.Values["what"])
|
|
|
|
base := p.c.Get("baseURL", "http://127.0.0.1:1337")
|
|
|
|
u := base + "/cowboy/img/" + r.Values["what"]
|
2022-07-21 15:39:50 +00:00
|
|
|
p.b.Send(r.Conn, bot.Delete, r.Msg.Channel, r.Msg.ID)
|
2022-07-21 15:25:10 +00:00
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "", bot.ImageAttachment{
|
|
|
|
URL: u,
|
2022-07-21 15:39:50 +00:00
|
|
|
AltTxt: fmt.Sprintf("%s: %s", r.Msg.User.Name, r.Msg.Body),
|
2022-07-21 15:25:10 +00:00
|
|
|
Width: 64,
|
|
|
|
Height: 64,
|
|
|
|
})
|
|
|
|
}
|
2022-07-22 21:04:51 +00:00
|
|
|
|
|
|
|
func (p *Cowboy) registerCmds(d *discord.Discord) {
|
|
|
|
//d.RegisterSlashCmd()
|
|
|
|
}
|