mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 17:10:10 +00:00
Discord: embed images
![alt](image link) is now converted to an image embed.
This commit is contained in:
parent
35d407afef
commit
591e3c5ca1
2
api.go
2
api.go
@ -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
|
||||||
|
19
discord.go
19
discord.go
@ -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
|
||||||
}
|
}
|
||||||
|
23
stripmd.go
23
stripmd.go
@ -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{
|
||||||
URL: md[URLStart : URLEnd+1],
|
Type: dg.EmbedTypeLink,
|
||||||
Alt: md[altTextStart : URLStart-2],
|
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 {
|
} else {
|
||||||
out += md[URLStart : URLEnd+1]
|
out += md[URLStart : URLEnd+1]
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user