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.
// @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 400 {object} boolResponse
// @Success 500 {object} boolResponse

View File

@ -41,6 +41,7 @@ func newDiscordDaemon(app *appContext) (*DiscordDaemon, error) {
for _, user := range app.storage.discord {
dd.users[user.ID] = user
}
return dd, nil
}
@ -91,13 +92,6 @@ func (d *DiscordDaemon) run() {
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) {
if m.GuildID != "" && d.channelName != "" {
if d.channelID == "" {
@ -235,16 +229,7 @@ func (d *DiscordDaemon) Send(message *Message, channelID ...string) error {
msg := ""
var embeds []*dg.MessageEmbed
if message.Markdown != "" {
var links []Link
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,
}
}
msg, embeds = StripAltText(message.Markdown, true)
} else {
msg = message.Text
}

View File

@ -3,6 +3,7 @@ package main
import (
"strings"
dg "github.com/bwmarrin/discordgo"
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.
// 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.
func StripAltText(md string, links bool) (string, []Link) {
func StripAltText(md string, links bool) (string, []*dg.MessageEmbed) {
altTextStart := -1 // Start of alt text (between '[' & ']')
URLStart := -1 // Start of url (between '(' & ')')
URLEnd := -1
previousURLEnd := -2
out := ""
embeds := []Link{}
embeds := []*dg.MessageEmbed{}
for i := range md {
if altTextStart != -1 && URLStart != -1 && md[i] == ')' {
URLEnd = i - 1
out += md[previousURLEnd+2 : altTextStart-1]
if links {
embeds = append(embeds, Link{
URL: md[URLStart : URLEnd+1],
Alt: md[altTextStart : URLStart-2],
})
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],
}
} else {
embed.URL = md[URLStart : URLEnd+1]
}
embeds = append(embeds, embed)
} else {
out += md[URLStart : URLEnd+1]
}