discord: support sending files with just a URL

previously, the code assumed each file had a Data, and crashed if this was not present.
now, we detect this condition, and send the URL instead.

the discord client will display URLs in a message of their own
as inline embeds, without any special behavior needed on our part.

this commit makes the same changes to the webhook and non-webhook code.
This commit is contained in:
ubq323 2024-08-16 10:04:12 +01:00
parent e168a89632
commit 707a74ba0e
2 changed files with 90 additions and 35 deletions

View File

@ -380,7 +380,27 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st
// handleUploadFile handles native upload of files
func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) {
for _, f := range msg.Extra["file"] {
var err error
fi := f.(config.FileInfo)
if fi.URL != "" {
err = b.handleUploadFileFromURL(msg, channelID, &fi)
} else if fi.Data != nil {
err = b.handleUploadFileFromData(msg, channelID, &fi)
} else {
b.Log.Errorf("Attachment %#v for message %#v had neither Data nor URL", fi, msg)
}
if err != nil {
b.Log.Errorf("Could not send attachment %#v for message %#v: %s", fi, msg, err)
return "", err
}
}
return "", nil
}
func (b *Bdiscord) handleUploadFileFromData(msg *config.Message, channelID string, fi *config.FileInfo) error {
file := discordgo.File{
Name: fi.Name,
ContentType: "",
@ -393,13 +413,18 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri
}
res, err := b.c.ChannelMessageSendComplex(channelID, &m)
if err != nil {
return "", fmt.Errorf("file upload failed: %s", err)
return fmt.Errorf("file upload failed: %s", err)
}
// link file_upload_nativeID (file ID from the original bridge) to our upload id
// so that we can remove this later when it eg needs to be deleted
b.cache.Add(cFileUpload+fi.NativeID, res.ID)
}
return "", nil
return nil
}
func (b *Bdiscord) handleUploadFileFromURL(msg *config.Message, channelID string, fi *config.FileInfo) error {
_, err := b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{
Content: fi.URL,
AllowedMentions: b.getAllowedMentions(),
})
return err
}

View File

@ -69,6 +69,26 @@ func (b *Bdiscord) webhookSendTextOnly(msg *config.Message, channelID string) (s
func (b *Bdiscord) webhookSendFilesOnly(msg *config.Message, channelID string) error {
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) //nolint:forcetypeassert
var err error
if fi.URL != "" {
err = b.webhookSendFileFromURL(msg, channelID, &fi)
} else if fi.Data != nil {
err = b.webhookSendFileFromData(msg, channelID, &fi)
} else {
b.Log.Errorf("Attachment %#v for message %#v had neither Data nor URL", fi, msg)
}
if err != nil {
b.Log.Errorf("Could not send attachment %#v for message %#v: %s", fi, msg, err)
return err
}
}
return nil
}
func (b *Bdiscord) webhookSendFileFromData(msg *config.Message, channelID string, fi *config.FileInfo) error {
file := discordgo.File{
Name: fi.Name,
ContentType: "",
@ -88,12 +108,22 @@ func (b *Bdiscord) webhookSendFilesOnly(msg *config.Message, channelID string) e
AllowedMentions: b.getAllowedMentions(),
},
)
if err != nil {
b.Log.Errorf("Could not send file %#v for message %#v: %s", file, msg, err)
return err
}
}
return nil
}
func (b *Bdiscord) webhookSendFileFromURL(msg *config.Message, channelID string, fi *config.FileInfo) error {
// discord client will display any file url as an inline embed,
// without us having to do anything special.
_, err := b.transmitter.Send(
channelID,
&discordgo.WebhookParams{
Content: fi.URL,
Username: msg.Username,
AvatarURL: msg.Avatar,
AllowedMentions: b.getAllowedMentions(),
},
)
return err
}
// webhookSend send one or more message via webhook, taking care of file