From 5c8031b8a2d34f3f023ad6278accf6b47b35ce37 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Thu, 9 Jul 2020 16:32:49 -0700 Subject: [PATCH] Added attachment support --- channel.go | 38 ++++++++++++++++++++++++++++++-------- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/channel.go b/channel.go index 96b6611..876c951 100644 --- a/channel.go +++ b/channel.go @@ -71,14 +71,15 @@ type Channel struct { } var ( - _ cchat.Server = (*Channel)(nil) - _ cchat.ServerMessage = (*Channel)(nil) - _ cchat.ServerMessageSender = (*Channel)(nil) - _ cchat.ServerMessageSendCompleter = (*Channel)(nil) - _ cchat.ServerNickname = (*Channel)(nil) - _ cchat.ServerMessageEditor = (*Channel)(nil) - _ cchat.ServerMessageActioner = (*Channel)(nil) - _ cchat.ServerMessageTypingIndicator = (*Channel)(nil) + _ cchat.Server = (*Channel)(nil) + _ cchat.ServerMessage = (*Channel)(nil) + _ cchat.ServerMessageSender = (*Channel)(nil) + _ cchat.ServerMessageAttachmentSender = (*Channel)(nil) + _ cchat.ServerMessageSendCompleter = (*Channel)(nil) + _ cchat.ServerNickname = (*Channel)(nil) + _ cchat.ServerMessageEditor = (*Channel)(nil) + _ cchat.ServerMessageActioner = (*Channel)(nil) + _ cchat.ServerMessageTypingIndicator = (*Channel)(nil) ) func NewChannel(s *Session, ch discord.Channel) *Channel { @@ -272,11 +273,32 @@ func (ch *Channel) SendMessage(msg cchat.SendableMessage) error { if noncer, ok := msg.(cchat.MessageNonce); ok { send.Nonce = noncer.Nonce() } + if attcher, ok := msg.(cchat.SendableMessageAttachments); ok { + send.Files = addAttachments(attcher.Attachments()) + } _, err := ch.session.SendMessageComplex(ch.id, send) return err } +func (ch *Channel) SendAttachments(atts []cchat.MessageAttachment) error { + _, err := ch.session.SendMessageComplex(ch.id, api.SendMessageData{ + Files: addAttachments(atts), + }) + return err +} + +func addAttachments(atts []cchat.MessageAttachment) []api.SendMessageFile { + var files = make([]api.SendMessageFile, len(atts)) + for i, a := range atts { + files[i] = api.SendMessageFile{ + Name: a.Name, + Reader: a, + } + } + return files +} + // MessageEditable returns true if the given message ID belongs to the current // user. func (ch *Channel) MessageEditable(id string) bool { diff --git a/go.mod b/go.mod index 3db38ab..89d42d9 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.14 require ( github.com/diamondburned/arikawa v0.9.6 - github.com/diamondburned/cchat v0.0.42 + github.com/diamondburned/cchat v0.0.43 github.com/diamondburned/ningen v0.1.1-0.20200708211706-57c712372ede github.com/dustin/go-humanize v1.0.0 github.com/go-test/deep v1.0.6 diff --git a/go.sum b/go.sum index a7e2c42..fd159d6 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/diamondburned/cchat v0.0.41 h1:6y32s2wWTiDw4hWN/Gna6ay3uUrRAW5V8Cj0/x github.com/diamondburned/cchat v0.0.41/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/diamondburned/cchat v0.0.42 h1:FVMLy9hOTxKju8OWDBIStrekbgTHCaH8+GVnV4LOByg= github.com/diamondburned/cchat v0.0.42/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= +github.com/diamondburned/cchat v0.0.43 h1:HetAujSaUSdnQgAUZgprNLARjf/MSWXpCfWdvX2wOCU= +github.com/diamondburned/cchat v0.0.43/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/diamondburned/ningen v0.1.1-0.20200621014632-6babb812b249 h1:yP7kJ+xCGpDz6XbcfACJcju4SH1XDPwlrvbofz3lP8I= github.com/diamondburned/ningen v0.1.1-0.20200621014632-6babb812b249/go.mod h1:xW9hpBZsGi8KpAh10TyP+YQlYBo+Xc+2w4TR6N0951A= github.com/diamondburned/ningen v0.1.1-0.20200708085949-b64e350f3b8c h1:3h/kyk6HplYZF3zLi106itjYJWjbuMK/twijeGLEy2M=