Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Too long textmessages can now be sent as a file instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Oct 26, 2019
1 parent c555e36 commit c6bd8fc
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,14 @@ func (window *Window) TrySendMessage(targetChannel *discordgo.Channel, message s
message = window.prepareMessage(targetChannel, message)
if len(message) > 2000 {
window.app.QueueUpdateDraw(func() {
window.ShowErrorDialog("Messages must be 2000 characters or less to send")
sendAsFile := "Send as file"
window.ShowDialog(config.GetTheme().PrimitiveBackgroundColor, "Your message is too long, what do you want to do?",
func(button string) {
if button == sendAsFile {
window.messageInput.SetText("")
go window.sendMessageAsFile(message, targetChannel.ID)
}
}, sendAsFile, "Nothing")
})
return
}
Expand All @@ -1042,6 +1049,39 @@ func (window *Window) TrySendMessage(targetChannel *discordgo.Channel, message s
go window.sendMessage(targetChannel.ID, message)
}

func (window *Window) sendMessageAsFile(message string, channel string) {
reader := bytes.NewBufferString(message)
messageAsFile := &discordgo.File{
Name: "message.txt",
ContentType: "text",
Reader: reader,
}
complexMessage := &discordgo.MessageSend{
Content: "The message was too long, therefore, you get a file:",
Embed: nil,
Tts: false,
Files: nil,
File: messageAsFile,
}
_, sendError := window.session.ChannelMessageSendComplex(channel, complexMessage)
if sendError != nil {
retry := "Retry sending"
edit := "Edit"
window.app.QueueUpdateDraw(func() {
window.ShowDialog(config.GetTheme().ErrorColor,
fmt.Sprintf("Error sending message: %s.\n\nWhat do you want to do?", sendError),
func(button string) {
switch button {
case retry:
go window.sendMessageAsFile(channel, message)
case edit:
window.messageInput.SetText(message)
}
}, retry, edit, "Cancel")
})
}
}

func (window *Window) sendMessage(targetChannelID, message string) {
window.app.QueueUpdateDraw(func() {
window.messageInput.SetText("")
Expand Down

0 comments on commit c6bd8fc

Please sign in to comment.