From df9db4e6fda7f341c22897e612a5a8dd686cf9ea Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Fri, 14 Oct 2022 23:06:15 -0400 Subject: [PATCH] tappd: optionally use files instead of embeds --- config/config.go | 7 +++++++ plugins/tappd/tappd.go | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index eeecd06..7a6f9a4 100644 --- a/config/config.go +++ b/config/config.go @@ -73,6 +73,13 @@ func (c *Config) GetInt(key string, fallback int) int { return i } +// GetBool returns true or false for config key +// It will assume false for any string except "true" +func (c *Config) GetBool(key string, fallback bool) bool { + val := c.GetString(key, strconv.FormatBool(fallback)) + return val == "true" +} + // Get is a shortcut for GetString func (c *Config) Get(key, fallback string) string { return c.GetString(key, fallback) diff --git a/plugins/tappd/tappd.go b/plugins/tappd/tappd.go index 0596fce..c1114ac 100644 --- a/plugins/tappd/tappd.go +++ b/plugins/tappd/tappd.go @@ -1,8 +1,10 @@ package tappd import ( + "bytes" "fmt" "github.com/bwmarrin/discordgo" + "github.com/gabriel-vasile/mimetype" "github.com/rs/zerolog/log" "github.com/velour/catbase/bot" "github.com/velour/catbase/config" @@ -134,18 +136,35 @@ func (p *Tappd) tap(s *discordgo.Session, i *discordgo.InteractionCreate) { } return } - embed := &discordgo.MessageEmbed{ + embeds := []*discordgo.MessageEmbed{{ Description: longMsg, Image: &discordgo.MessageEmbedImage{ URL: info.BotURL, Width: info.W, Height: info.H, }, + }} + mime := mimetype.Detect(info.Repr) + files := []*discordgo.File{{ + Name: info.ID + mime.Extension(), + ContentType: mime.String(), + Reader: bytes.NewBuffer(info.Repr), + }} + content := info.BotURL + // Yes, the configs are all stringly typed. Get over it. + useEmbed := p.c.GetBool("tappd.embed", false) + if useEmbed { + files = nil + } else { + embeds = nil + content = "" } err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Embeds: []*discordgo.MessageEmbed{embed}, + Embeds: embeds, + Files: files, + Content: content, }, }) if err != nil {