Compare commits

..

6 Commits

Author SHA1 Message Date
Chris Sexton b1cec209fe emojy: show names on stats page 2022-07-23 12:42:58 -04:00
Chris Sexton 668b0dcb7b emojy: sort the emojy list page 2022-07-23 09:49:27 -04:00
Chris Sexton 4c1a02e9fb cowboy: reuse some code 2022-07-23 09:27:21 -04:00
Chris Sexton 274d8f6966 cowboy: fix original url 2022-07-23 09:10:38 -04:00
Chris Sexton 51da6187b0 emojy: swap in emojy command 2022-07-23 09:02:57 -04:00
Chris Sexton 0453c5ec24 cowboy: show the emojy when its made 2022-07-23 08:59:32 -04:00
5 changed files with 97 additions and 12 deletions

View File

@ -43,10 +43,8 @@ func (p *Cowboy) register() {
Kind: bot.Startup, IsCmd: false,
Regex: regexp.MustCompile(`.*`),
Handler: func(r bot.Request) bool {
log.Debug().Msgf("Got bot.Startup")
switch conn := r.Conn.(type) {
case *discord.Discord:
log.Debug().Msg("Found a discord connection")
p.registerCmds(conn)
}
return false
@ -86,7 +84,7 @@ 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"]
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.Message, r.Msg.Channel, "", bot.ImageAttachment{
URL: u,
@ -148,6 +146,7 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
lastEmojy := p.c.Get("cowboy.lastEmojy", "rust")
emojyPlugin := emojy.NewAPI(p.b)
list := map[string]string{}
name := i.ApplicationCommandData().Options[0].StringValue()
if overlay == "" {
@ -183,7 +182,9 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco
p.c.Set("cowboy.lastEmojy", name)
msg = fmt.Sprintf("You replaced %s with a new emojy %s, pardner!", 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])
resp:
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{

View File

@ -0,0 +1,67 @@
package emojy
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/connectors/discord"
)
func (p *EmojyPlugin) registerCmds(d *discord.Discord) {
log.Debug().Msg("About to register some startup commands")
cmd := discordgo.ApplicationCommand{
Name: "emojy",
Description: "swap in an emojy",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "emojy",
Description: "which emojy you want swapped in",
Required: true,
},
},
}
if err := d.RegisterSlashCmd(cmd, p.overlayCB); err != nil {
log.Error().Err(err).Msg("could not register emojy command")
}
}
func (p *EmojyPlugin) overlayCB(s *discordgo.Session, i *discordgo.InteractionCreate) {
lastEmojy := p.c.Get("emojy.lastEmojy", "rust")
list := map[string]string{}
var err error
msg := "hello, user"
name := i.ApplicationCommandData().Options[0].StringValue()
if ok, _, _, err := p.isKnownEmojy(name); !ok || err != nil {
msg = "I could not find your emojy"
goto resp
}
err = p.RmEmojy(p.b.DefaultConnector(), lastEmojy)
if err != nil {
msg = err.Error()
goto resp
}
err = p.UploadEmojyInCache(p.b.DefaultConnector(), name)
if err != nil {
msg = err.Error()
goto resp
}
p.c.Set("emojy.lastEmojy", name)
list = 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])
resp:
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
Flags: uint64(discordgo.MessageFlagsEphemeral),
},
})
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"fmt"
"github.com/velour/catbase/connectors/discord"
"image"
"image/draw"
"math"
@ -67,6 +68,17 @@ func (p *EmojyPlugin) setupDB() {
func (p *EmojyPlugin) register() {
ht := bot.HandlerTable{
{
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
},
},
{
Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`.*`),
@ -121,7 +133,7 @@ func (p *EmojyPlugin) register() {
}
func (p *EmojyPlugin) RmEmojy(c bot.Connector, name string) error {
onServerList := invertEmojyList(p.b.GetEmojiList(false))
onServerList := InvertEmojyList(p.b.GetEmojiList(false))
// Call a non-existent emojy a successful remove
if _, ok := onServerList[name]; !ok {
return fmt.Errorf("could not find emojy %s", name)
@ -143,7 +155,7 @@ func (p *EmojyPlugin) rmEmojyHandler(r bot.Request, name string) bool {
}
func (p *EmojyPlugin) AddEmojy(c bot.Connector, name string) error {
onServerList := invertEmojyList(p.b.GetEmojiList(false))
onServerList := InvertEmojyList(p.b.GetEmojiList(false))
if _, ok := onServerList[name]; ok {
return fmt.Errorf("emojy already exists")
}
@ -187,7 +199,7 @@ type EmojyCount struct {
OnServer bool `json:"onServer"`
}
func invertEmojyList(emojy map[string]string) map[string]string {
func InvertEmojyList(emojy map[string]string) map[string]string {
out := map[string]string{}
for k, v := range emojy {
out[v] = k
@ -197,7 +209,7 @@ func invertEmojyList(emojy map[string]string) map[string]string {
func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) {
out := map[string][]EmojyCount{}
onServerList := invertEmojyList(p.b.GetEmojiList(true))
onServerList := InvertEmojyList(p.b.GetEmojiList(true))
q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc`
result := []EmojyCount{}
err := p.db.Select(&result, q)

View File

@ -44,8 +44,8 @@
</div>
<div class="row row-cols-5">
<div class="card text-center" v-for="(path, name) in fileList" key="name">
<img :src="path" class="card-img-top mx-auto d-block" :alt="name" style="max-width: 100px">
<div class="card text-center" v-for="name in fileKeys" key="name">
<img :src="fileList[name]" class="card-img-top mx-auto d-block" :alt="name" style="max-width: 100px">
<div class="card-body">
<h5 class="card-title">{{name}}</h5>
</div>
@ -62,6 +62,7 @@
view: '',
nav: [],
results: [],
fileKeys: [],
fileList: {},
image: null,
password: ''
@ -90,6 +91,8 @@
.catch(err => (this.err = err))
axios.get('/emojy/allFiles')
.then(resp => {
// stole this somewhere or other as a quick hack
this.fileKeys = Object.keys(resp.data).sort()
this.fileList = resp.data
this.err = ''
})

View File

@ -54,8 +54,10 @@
<span v-else></span>
-
</span>
<img v-if="emojy.url" :src="emojy.url" :alt="emojy.name" class="img-thumbnail"
style="max-width: 64px; max-height: 64px"/> {{emojy.name}}
<span v-if="emojy.url">
<img :src="emojy.url" :alt="emojy.name" class="img-thumbnail"
style="max-width: 64px; max-height: 64px"/> {{emojy.emojy}}
</span>
<span v-else>{{emojy.emojy}}</span>
</li>
</ul>