mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-23 01:20:11 +00:00
Discord: send links as embeds
Kind of janky but works. This kind of messes up the layout if you write links in-line.
This commit is contained in:
parent
f8f5f35cc1
commit
3bf722c5fe
40
discord.go
40
discord.go
@ -232,13 +232,42 @@ func (d *DiscordDaemon) commandPIN(s *dg.Session, m *dg.MessageCreate, sects []s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *DiscordDaemon) Send(message *Message, channelID ...string) error {
|
func (d *DiscordDaemon) Send(message *Message, channelID ...string) error {
|
||||||
for _, id := range channelID {
|
|
||||||
msg := ""
|
msg := ""
|
||||||
if message.Markdown == "" {
|
var embeds []*dg.MessageEmbed
|
||||||
msg = message.Text
|
if message.Markdown != "" {
|
||||||
} else {
|
var links []Link
|
||||||
msg = message.Markdown
|
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 {
|
||||||
|
msg = message.Text
|
||||||
|
}
|
||||||
|
for _, id := range channelID {
|
||||||
|
var err error
|
||||||
|
if len(embeds) != 0 {
|
||||||
|
_, err = d.bot.ChannelMessageSendComplex(
|
||||||
|
id,
|
||||||
|
&dg.MessageSend{
|
||||||
|
Content: msg,
|
||||||
|
Embed: embeds[0],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for i := 1; i < len(embeds); i++ {
|
||||||
|
_, err := d.bot.ChannelMessageSendEmbed(id, embeds[i])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
_, err := d.bot.ChannelMessageSend(
|
_, err := d.bot.ChannelMessageSend(
|
||||||
id,
|
id,
|
||||||
msg,
|
msg,
|
||||||
@ -247,5 +276,6 @@ func (d *DiscordDaemon) Send(message *Message, channelID ...string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
25
stripmd.go
25
stripmd.go
@ -6,18 +6,32 @@ import (
|
|||||||
stripmd "github.com/writeas/go-strip-markdown"
|
stripmd "github.com/writeas/go-strip-markdown"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Link struct {
|
||||||
|
Alt, URL string
|
||||||
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
func StripAltText(md string) string {
|
// 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) {
|
||||||
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{}
|
||||||
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] + md[URLStart:URLEnd+1]
|
out += md[previousURLEnd+2 : altTextStart-1]
|
||||||
|
if links {
|
||||||
|
embeds = append(embeds, Link{
|
||||||
|
URL: md[URLStart : URLEnd+1],
|
||||||
|
Alt: md[altTextStart : URLStart-2],
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
out += md[URLStart : URLEnd+1]
|
||||||
|
}
|
||||||
previousURLEnd = URLEnd
|
previousURLEnd = URLEnd
|
||||||
altTextStart, URLStart, URLEnd = -1, -1, -1
|
altTextStart, URLStart, URLEnd = -1, -1, -1
|
||||||
continue
|
continue
|
||||||
@ -36,11 +50,12 @@ func StripAltText(md string) string {
|
|||||||
out += md[previousURLEnd+2:]
|
out += md[previousURLEnd+2:]
|
||||||
}
|
}
|
||||||
if out == "" {
|
if out == "" {
|
||||||
return md
|
return md, embeds
|
||||||
}
|
}
|
||||||
return out
|
return out, embeds
|
||||||
}
|
}
|
||||||
|
|
||||||
func stripMarkdown(md string) string {
|
func stripMarkdown(md string) string {
|
||||||
return strings.TrimPrefix(strings.TrimSuffix(stripmd.Strip(StripAltText(md)), "</p>"), "<p>")
|
stripped, _ := StripAltText(md, false)
|
||||||
|
return strings.TrimPrefix(strings.TrimSuffix(stripmd.Strip(stripped), "</p>"), "<p>")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user