1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00

Discord: embed images

![alt](image link) is now converted to an image embed.
This commit is contained in:
Harvey Tindall 2021-05-22 15:32:51 +01:00
parent 35d407afef
commit 591e3c5ca1
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
3 changed files with 20 additions and 24 deletions

2
api.go
View File

@ -2048,7 +2048,7 @@ func (app *appContext) TelegramAddUser(gc *gin.Context) {
// @Summary Sets whether to notify a user through telegram or not. // @Summary Sets whether to notify a user through telegram or not.
// @Produce json // @Produce json
// @Param telegramNotifyDTO body telegramNotifyDTO true "User's Jellyfin ID and whether or not to notify then through Telegram." // @Param SetContactMethodDTO body SetContactMethodsDTO true "User's Jellyfin ID and whether or not to notify then through Telegram."
// @Success 200 {object} boolResponse // @Success 200 {object} boolResponse
// @Success 400 {object} boolResponse // @Success 400 {object} boolResponse
// @Success 500 {object} boolResponse // @Success 500 {object} boolResponse

View File

@ -41,6 +41,7 @@ func newDiscordDaemon(app *appContext) (*DiscordDaemon, error) {
for _, user := range app.storage.discord { for _, user := range app.storage.discord {
dd.users[user.ID] = user dd.users[user.ID] = user
} }
return dd, nil return dd, nil
} }
@ -91,13 +92,6 @@ func (d *DiscordDaemon) run() {
return return
} }
func (d *DiscordDaemon) Shutdown() {
d.Stopped = true
d.ShutdownChannel <- "Down"
<-d.ShutdownChannel
close(d.ShutdownChannel)
}
func (d *DiscordDaemon) messageHandler(s *dg.Session, m *dg.MessageCreate) { func (d *DiscordDaemon) messageHandler(s *dg.Session, m *dg.MessageCreate) {
if m.GuildID != "" && d.channelName != "" { if m.GuildID != "" && d.channelName != "" {
if d.channelID == "" { if d.channelID == "" {
@ -235,16 +229,7 @@ func (d *DiscordDaemon) Send(message *Message, channelID ...string) error {
msg := "" msg := ""
var embeds []*dg.MessageEmbed var embeds []*dg.MessageEmbed
if message.Markdown != "" { if message.Markdown != "" {
var links []Link msg, embeds = StripAltText(message.Markdown, true)
msg, links = StripAltText(message.Markdown, true)
embeds = make([]*dg.MessageEmbed, len(links))
for i := range links {
embeds[i] = &dg.MessageEmbed{
URL: links[i].URL,
Title: links[i].Alt,
Type: dg.EmbedTypeLink,
}
}
} else { } else {
msg = message.Text msg = message.Text
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
"strings" "strings"
dg "github.com/bwmarrin/discordgo"
stripmd "github.com/writeas/go-strip-markdown" stripmd "github.com/writeas/go-strip-markdown"
) )
@ -13,22 +14,32 @@ type Link struct {
// StripAltText removes Markdown alt text from links and images and replaces them with just the URL. // StripAltText removes Markdown alt text from links and images and replaces them with just the URL.
// Currently uses the deepest alt text when links/images are nested. // Currently uses the deepest alt text when links/images are nested.
// If links = true, links are completely removed, and a list of URLs and their alt text is also returned. // If links = true, links are completely removed, and a list of URLs and their alt text is also returned.
func StripAltText(md string, links bool) (string, []Link) { func StripAltText(md string, links bool) (string, []*dg.MessageEmbed) {
altTextStart := -1 // Start of alt text (between '[' & ']') altTextStart := -1 // Start of alt text (between '[' & ']')
URLStart := -1 // Start of url (between '(' & ')') URLStart := -1 // Start of url (between '(' & ')')
URLEnd := -1 URLEnd := -1
previousURLEnd := -2 previousURLEnd := -2
out := "" out := ""
embeds := []Link{} embeds := []*dg.MessageEmbed{}
for i := range md { for i := range md {
if altTextStart != -1 && URLStart != -1 && md[i] == ')' { if altTextStart != -1 && URLStart != -1 && md[i] == ')' {
URLEnd = i - 1 URLEnd = i - 1
out += md[previousURLEnd+2 : altTextStart-1] out += md[previousURLEnd+2 : altTextStart-1]
if links { if links {
embeds = append(embeds, Link{ embed := &dg.MessageEmbed{
Type: dg.EmbedTypeLink,
Title: md[altTextStart : URLStart-2],
}
if md[altTextStart-1] == '!' {
embed.Title = md[altTextStart+1 : URLStart-2]
embed.Type = dg.EmbedTypeImage
embed.Image = &dg.MessageEmbedImage{
URL: md[URLStart : URLEnd+1], URL: md[URLStart : URLEnd+1],
Alt: md[altTextStart : URLStart-2], }
}) } else {
embed.URL = md[URLStart : URLEnd+1]
}
embeds = append(embeds, embed)
} else { } else {
out += md[URLStart : URLEnd+1] out += md[URLStart : URLEnd+1]
} }