From fef47e3574fbe553220340c24a7796bcd2161ad4 Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Thu, 2 Jul 2020 20:23:09 -0700 Subject: [PATCH] Implemented better typing --- channel.go | 4 ++-- go.mod | 2 +- go.sum | 2 ++ typer.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 typer.go diff --git a/channel.go b/channel.go index b1e2811..4456b32 100644 --- a/channel.go +++ b/channel.go @@ -445,7 +445,7 @@ func (ch *Channel) TypingSubscribe(ti cchat.TypingIndicator) (func(), error) { } } - ti.AddTyper(NewGuildMember(*t.Member, *g)) + ti.AddTyper(NewTyper(NewGuildMember(*t.Member, *g), t)) return } @@ -456,7 +456,7 @@ func (ch *Channel) TypingSubscribe(ti cchat.TypingIndicator) (func(), error) { for _, user := range c.DMRecipients { if user.ID == t.UserID { - ti.AddTyper(NewUser(user)) + ti.AddTyper(NewTyper(NewUser(user), t)) return } } diff --git a/go.mod b/go.mod index d58d395..3b88b41 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.14 require ( github.com/diamondburned/arikawa v0.9.5 - github.com/diamondburned/cchat v0.0.39 + github.com/diamondburned/cchat v0.0.40 github.com/diamondburned/ningen v0.1.1-0.20200621014632-6babb812b249 github.com/go-test/deep v1.0.6 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 692f35f..11f7c29 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,8 @@ github.com/diamondburned/cchat v0.0.37 h1:yGz9yls5Lb/vLkU/DU53GjC80WOqoRe229DXsu github.com/diamondburned/cchat v0.0.37/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= github.com/diamondburned/cchat v0.0.39 h1:Hxd7swmAIECm0MBd5wb1IFvreChwDFwnAshqgAstWGA= github.com/diamondburned/cchat v0.0.39/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU= +github.com/diamondburned/cchat v0.0.40 h1:38gPyJnnDoNDHrXcV8Qchfv3y6jlS3Fzz/6FY0BPH6I= +github.com/diamondburned/cchat v0.0.40/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/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= diff --git a/typer.go b/typer.go new file mode 100644 index 0000000..221843c --- /dev/null +++ b/typer.go @@ -0,0 +1,63 @@ +package discord + +import ( + "time" + + "github.com/diamondburned/arikawa/discord" + "github.com/diamondburned/arikawa/gateway" + "github.com/diamondburned/arikawa/state" + "github.com/diamondburned/cchat" +) + +type Typer struct { + Author + time discord.UnixTimestamp +} + +var _ cchat.Typer = (*Typer)(nil) + +func NewTyperAuthor(author Author, ev *gateway.TypingStartEvent) Typer { + return Typer{ + Author: author, + time: ev.Timestamp, + } +} + +func NewTyper(store state.Store, ev *gateway.TypingStartEvent) (*Typer, error) { + if t.ChannelID != ch.id { + return + } + + if ch.guildID.Valid() { + g, err := ch.session.Store.Guild(t.GuildID) + if err != nil { + return + } + + if t.Member == nil { + t.Member, err = ch.session.Store.Member(t.GuildID, t.UserID) + if err != nil { + return + } + } + + ti.AddTyper(NewTyper(NewGuildMember(*t.Member, *g), t)) + return + } + + c, err := ch.self() + if err != nil { + return + } + + for _, user := range c.DMRecipients { + if user.ID == t.UserID { + ti.AddTyper(NewTyper(NewUser(user), t)) + return + } + } +} + +func (t Typer) Time() time.Time { + return t.time.Time() +}