mirror of
https://github.com/diamondburned/cchat-discord.git
synced 2024-12-23 12:56:42 +00:00
Fixed basic message loading and sending
This commit is contained in:
parent
e93f4b52f5
commit
316f0a8c9b
48
channel.go
48
channel.go
|
@ -3,6 +3,7 @@ package discord
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/diamondburned/arikawa/api"
|
||||
"github.com/diamondburned/arikawa/discord"
|
||||
"github.com/diamondburned/arikawa/gateway"
|
||||
"github.com/diamondburned/cchat"
|
||||
|
@ -88,6 +89,8 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
|
|||
|
||||
var addcancel = newCancels()
|
||||
|
||||
var constructor func(discord.Message) cchat.MessageCreate
|
||||
|
||||
if ch.guildID.Valid() {
|
||||
// Create the backlog without any member information.
|
||||
g, err := state.Guild(ch.guildID)
|
||||
|
@ -95,8 +98,16 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
|
|||
return nil, errors.Wrap(err, "Failed to get guild")
|
||||
}
|
||||
|
||||
constructor = func(m discord.Message) cchat.MessageCreate {
|
||||
return NewBacklogMessage(m, ch.session, *g)
|
||||
}
|
||||
|
||||
// Listen to new members before creating the backlog and requesting members.
|
||||
addcancel(ch.session.AddHandler(func(c *gateway.GuildMembersChunkEvent) {
|
||||
if c.GuildID != ch.guildID {
|
||||
return
|
||||
}
|
||||
|
||||
m, err := ch.session.Store.Messages(ch.id)
|
||||
if err != nil {
|
||||
// TODO: log
|
||||
|
@ -108,9 +119,10 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
|
|||
return
|
||||
}
|
||||
|
||||
for _, member := range c.Members {
|
||||
// Loop over all messages and replace the author.
|
||||
// Loop over all messages and replace the author. The latest
|
||||
// messages are in front.
|
||||
for _, msg := range m {
|
||||
for _, member := range c.Members {
|
||||
if msg.Author.ID != member.User.ID {
|
||||
continue
|
||||
}
|
||||
|
@ -119,31 +131,34 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
|
|||
}
|
||||
}
|
||||
}))
|
||||
|
||||
for _, m := range m {
|
||||
ct.CreateMessage(NewBacklogMessage(m, ch.session, *g))
|
||||
}
|
||||
|
||||
} else {
|
||||
for _, m := range m {
|
||||
ct.CreateMessage(NewDirectMessage(m))
|
||||
constructor = func(m discord.Message) cchat.MessageCreate {
|
||||
return NewDirectMessage(m)
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate from the earliest messages to the latest messages.
|
||||
for i := len(m) - 1; i >= 0; i-- {
|
||||
ct.CreateMessage(constructor(m[i]))
|
||||
}
|
||||
|
||||
// Bind the handler.
|
||||
addcancel(
|
||||
ch.session.AddHandler(func(m *gateway.MessageCreateEvent) {
|
||||
ct.CreateMessage(NewMessageWithMember(m.Message, ch.session, m.Member))
|
||||
if m.ChannelID == ch.id {
|
||||
ct.CreateMessage(NewMessageCreate(m, ch.session))
|
||||
}
|
||||
}),
|
||||
ch.session.AddHandler(func(m *gateway.MessageUpdateEvent) {
|
||||
// If the updated content is empty. TODO: add embed support.
|
||||
if m.Content == "" {
|
||||
return
|
||||
}
|
||||
if m.ChannelID == ch.id && m.Content != "" {
|
||||
ct.UpdateMessage(NewMessageUpdateContent(m.Message))
|
||||
}
|
||||
}),
|
||||
ch.session.AddHandler(func(m *gateway.MessageDeleteEvent) {
|
||||
if m.ChannelID == ch.id {
|
||||
ct.DeleteMessage(NewHeaderDelete(m))
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
|
@ -151,7 +166,12 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
|
|||
}
|
||||
|
||||
func (ch *Channel) SendMessage(msg cchat.SendableMessage) error {
|
||||
_, err := ch.session.SendText(ch.id, msg.Content())
|
||||
var send = api.SendMessageData{Content: msg.Content()}
|
||||
if noncer, ok := msg.(cchat.MessageNonce); ok {
|
||||
send.Nonce = noncer.Nonce()
|
||||
}
|
||||
|
||||
_, err := ch.session.SendMessageComplex(ch.id, send)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
20
message.go
20
message.go
|
@ -125,24 +125,24 @@ func NewMessageUpdateAuthor(msg discord.Message, member discord.Member, g discor
|
|||
}
|
||||
}
|
||||
|
||||
// NewMessageWithSession uses the session to create a message. It does not do
|
||||
// NewMessageCreate uses the session to create a message. It does not do
|
||||
// API calls. Member is optional.
|
||||
func NewMessageWithMember(m discord.Message, s *Session, mem *discord.Member) Message {
|
||||
func NewMessageCreate(c *gateway.MessageCreateEvent, s *Session) Message {
|
||||
// This should not error.
|
||||
g, err := s.Store.Guild(m.GuildID)
|
||||
g, err := s.Store.Guild(c.GuildID)
|
||||
if err != nil {
|
||||
return NewMessage(m, NewUser(m.Author))
|
||||
return NewMessage(c.Message, NewUser(c.Author))
|
||||
}
|
||||
|
||||
if mem == nil {
|
||||
mem, _ = s.Store.Member(m.GuildID, m.Author.ID)
|
||||
if c.Member == nil {
|
||||
c.Member, _ = s.Store.Member(c.GuildID, c.Author.ID)
|
||||
}
|
||||
if mem == nil {
|
||||
s.Members.RequestMember(m.GuildID, m.Author.ID)
|
||||
return NewMessage(m, NewUser(m.Author))
|
||||
if c.Member == nil {
|
||||
s.Members.RequestMember(c.GuildID, c.Author.ID)
|
||||
return NewMessage(c.Message, NewUser(c.Author))
|
||||
}
|
||||
|
||||
return NewMessage(m, NewGuildMember(*mem, *g))
|
||||
return NewMessage(c.Message, NewGuildMember(*c.Member, *g))
|
||||
}
|
||||
|
||||
// NewBacklogMessage uses the session to create a message fetched from the
|
||||
|
|
39
service.go
39
service.go
|
@ -16,6 +16,9 @@ func init() {
|
|||
services.RegisterService(&Service{})
|
||||
}
|
||||
|
||||
// ErrInvalidSession is returned if SessionRestore is given a bad session.
|
||||
var ErrInvalidSession = errors.New("invalid session")
|
||||
|
||||
type Service struct{}
|
||||
|
||||
var (
|
||||
|
@ -36,6 +39,15 @@ func (Service) Authenticate() cchat.Authenticator {
|
|||
return Authenticator{}
|
||||
}
|
||||
|
||||
func (s Service) RestoreSession(data map[string]string) (cchat.Session, error) {
|
||||
tk, ok := data["token"]
|
||||
if !ok {
|
||||
return nil, ErrInvalidSession
|
||||
}
|
||||
|
||||
return NewSessionToken(tk)
|
||||
}
|
||||
|
||||
type Authenticator struct{}
|
||||
|
||||
var _ cchat.Authenticator = (*Authenticator)(nil)
|
||||
|
@ -51,16 +63,7 @@ func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry {
|
|||
}
|
||||
|
||||
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
||||
s, err := state.New(form[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.Open(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewSession(s)
|
||||
return NewSessionToken(form[0])
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
|
@ -71,10 +74,22 @@ type Session struct {
|
|||
var (
|
||||
_ cchat.Icon = (*Session)(nil)
|
||||
_ cchat.Session = (*Session)(nil)
|
||||
_ cchat.ServerList = (*Session)(nil)
|
||||
_ cchat.SessionSaver = (*Session)(nil)
|
||||
)
|
||||
|
||||
func NewSessionToken(token string) (*Session, error) {
|
||||
s, err := state.New(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.Open(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewSession(s)
|
||||
}
|
||||
|
||||
func NewSession(s *state.State) (*Session, error) {
|
||||
// Prefetch user.
|
||||
u, err := s.Me()
|
||||
|
@ -108,7 +123,7 @@ func (s *Session) Name() text.Rich {
|
|||
}
|
||||
|
||||
func (s *Session) Icon(ctx context.Context, iconer cchat.IconContainer) error {
|
||||
u, err := s.Store.Me()
|
||||
u, err := s.Me()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to get the current user")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue