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
52
channel.go
52
channel.go
|
@ -3,6 +3,7 @@ package discord
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/diamondburned/arikawa/api"
|
||||||
"github.com/diamondburned/arikawa/discord"
|
"github.com/diamondburned/arikawa/discord"
|
||||||
"github.com/diamondburned/arikawa/gateway"
|
"github.com/diamondburned/arikawa/gateway"
|
||||||
"github.com/diamondburned/cchat"
|
"github.com/diamondburned/cchat"
|
||||||
|
@ -88,6 +89,8 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
|
||||||
|
|
||||||
var addcancel = newCancels()
|
var addcancel = newCancels()
|
||||||
|
|
||||||
|
var constructor func(discord.Message) cchat.MessageCreate
|
||||||
|
|
||||||
if ch.guildID.Valid() {
|
if ch.guildID.Valid() {
|
||||||
// Create the backlog without any member information.
|
// Create the backlog without any member information.
|
||||||
g, err := state.Guild(ch.guildID)
|
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")
|
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.
|
// Listen to new members before creating the backlog and requesting members.
|
||||||
addcancel(ch.session.AddHandler(func(c *gateway.GuildMembersChunkEvent) {
|
addcancel(ch.session.AddHandler(func(c *gateway.GuildMembersChunkEvent) {
|
||||||
|
if c.GuildID != ch.guildID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
m, err := ch.session.Store.Messages(ch.id)
|
m, err := ch.session.Store.Messages(ch.id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: log
|
// TODO: log
|
||||||
|
@ -108,9 +119,10 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, member := range c.Members {
|
// Loop over all messages and replace the author. The latest
|
||||||
// Loop over all messages and replace the author.
|
// messages are in front.
|
||||||
for _, msg := range m {
|
for _, msg := range m {
|
||||||
|
for _, member := range c.Members {
|
||||||
if msg.Author.ID != member.User.ID {
|
if msg.Author.ID != member.User.ID {
|
||||||
continue
|
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 {
|
} else {
|
||||||
for _, m := range m {
|
constructor = func(m discord.Message) cchat.MessageCreate {
|
||||||
ct.CreateMessage(NewDirectMessage(m))
|
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.
|
// Bind the handler.
|
||||||
addcancel(
|
addcancel(
|
||||||
ch.session.AddHandler(func(m *gateway.MessageCreateEvent) {
|
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) {
|
ch.session.AddHandler(func(m *gateway.MessageUpdateEvent) {
|
||||||
// If the updated content is empty. TODO: add embed support.
|
// If the updated content is empty. TODO: add embed support.
|
||||||
if m.Content == "" {
|
if m.ChannelID == ch.id && m.Content != "" {
|
||||||
return
|
ct.UpdateMessage(NewMessageUpdateContent(m.Message))
|
||||||
}
|
}
|
||||||
ct.UpdateMessage(NewMessageUpdateContent(m.Message))
|
|
||||||
}),
|
}),
|
||||||
ch.session.AddHandler(func(m *gateway.MessageDeleteEvent) {
|
ch.session.AddHandler(func(m *gateway.MessageDeleteEvent) {
|
||||||
ct.DeleteMessage(NewHeaderDelete(m))
|
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 {
|
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
|
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.
|
// 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.
|
// This should not error.
|
||||||
g, err := s.Store.Guild(m.GuildID)
|
g, err := s.Store.Guild(c.GuildID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return NewMessage(m, NewUser(m.Author))
|
return NewMessage(c.Message, NewUser(c.Author))
|
||||||
}
|
}
|
||||||
|
|
||||||
if mem == nil {
|
if c.Member == nil {
|
||||||
mem, _ = s.Store.Member(m.GuildID, m.Author.ID)
|
c.Member, _ = s.Store.Member(c.GuildID, c.Author.ID)
|
||||||
}
|
}
|
||||||
if mem == nil {
|
if c.Member == nil {
|
||||||
s.Members.RequestMember(m.GuildID, m.Author.ID)
|
s.Members.RequestMember(c.GuildID, c.Author.ID)
|
||||||
return NewMessage(m, NewUser(m.Author))
|
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
|
// 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{})
|
services.RegisterService(&Service{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrInvalidSession is returned if SessionRestore is given a bad session.
|
||||||
|
var ErrInvalidSession = errors.New("invalid session")
|
||||||
|
|
||||||
type Service struct{}
|
type Service struct{}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -36,6 +39,15 @@ func (Service) Authenticate() cchat.Authenticator {
|
||||||
return 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{}
|
type Authenticator struct{}
|
||||||
|
|
||||||
var _ cchat.Authenticator = (*Authenticator)(nil)
|
var _ cchat.Authenticator = (*Authenticator)(nil)
|
||||||
|
@ -51,16 +63,7 @@ func (Authenticator) AuthenticateForm() []cchat.AuthenticateEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
func (Authenticator) Authenticate(form []string) (cchat.Session, error) {
|
||||||
s, err := state.New(form[0])
|
return NewSessionToken(form[0])
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.Open(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewSession(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
@ -71,10 +74,22 @@ type Session struct {
|
||||||
var (
|
var (
|
||||||
_ cchat.Icon = (*Session)(nil)
|
_ cchat.Icon = (*Session)(nil)
|
||||||
_ cchat.Session = (*Session)(nil)
|
_ cchat.Session = (*Session)(nil)
|
||||||
_ cchat.ServerList = (*Session)(nil)
|
|
||||||
_ cchat.SessionSaver = (*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) {
|
func NewSession(s *state.State) (*Session, error) {
|
||||||
// Prefetch user.
|
// Prefetch user.
|
||||||
u, err := s.Me()
|
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 {
|
func (s *Session) Icon(ctx context.Context, iconer cchat.IconContainer) error {
|
||||||
u, err := s.Store.Me()
|
u, err := s.Me()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Failed to get the current user")
|
return errors.Wrap(err, "Failed to get the current user")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue