1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-25 23:52:48 +00:00

partially fixed references

This commit is contained in:
diamondburned 2021-01-02 00:52:53 -08:00
parent 73b0d3f39c
commit a691813b90
3 changed files with 44 additions and 35 deletions

View file

@ -64,26 +64,26 @@ func (msgr *Messenger) JoinServer(ctx context.Context, ct cchat.MessagesContaine
return
}
m, err := msgr.Messages()
messages, err := msgr.Messages()
if err != nil {
// TODO: log
return
}
g, err := msgr.Guild()
guild, err := msgr.Guild()
if err != nil {
return
}
// 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 {
for _, msg := range messages {
for _, m := range c.Members {
if msg.Author.ID != m.User.ID {
continue
}
ct.UpdateMessage(message.NewMessageUpdateAuthor(msg, member, *g, msgr.State))
ct.UpdateMessage(message.NewMessageUpdateAuthor(msg, m, *guild, msgr.State))
}
}
}))

View file

@ -135,22 +135,22 @@ func (a *Author) addAuthorReference(msgref discord.Message, s *state.Instance) {
}
// AddMessageReference adds a message reference to the author.
func (a *Author) AddMessageReference(msgref discord.Message, s *state.Instance) {
if !msgref.GuildID.IsValid() {
a.addAuthorReference(msgref, s)
func (a *Author) AddMessageReference(ref discord.Message, s *state.Instance) {
if !ref.GuildID.IsValid() {
a.addAuthorReference(ref, s)
return
}
g, err := s.Cabinet.Guild(msgref.GuildID)
g, err := s.Cabinet.Guild(ref.GuildID)
if err != nil {
a.addAuthorReference(msgref, s)
a.addAuthorReference(ref, s)
return
}
m, err := s.Cabinet.Member(g.ID, msgref.Author.ID)
m, err := s.Cabinet.Member(g.ID, ref.Author.ID)
if err != nil {
a.addAuthorReference(msgref, s)
s.MemberState.RequestMember(msgref.GuildID, msgref.Author.ID)
a.addAuthorReference(ref, s)
s.MemberState.RequestMember(g.ID, ref.Author.ID)
return
}
@ -158,6 +158,6 @@ func (a *Author) AddMessageReference(msgref discord.Message, s *state.Instance)
start, end := richMember(&a.name, *m, *g, s)
a.name.Segments = append(a.name.Segments,
reference.NewMessageSegment(start, end, msgref.ID),
reference.NewMessageSegment(start, end, ref.ID),
)
}

View file

@ -174,7 +174,7 @@ func NewMessage(m discord.Message, s *state.Instance, author Author) Message {
content.Content = strings.ReplaceAll(refmsg, "\n", " ") + "\n"
content.Segments = []text.Segment{
reference.NewMessageSegment(0, len(refmsg), ref.ID),
reference.NewMessageSegment(0, len(content.Content), ref.ID),
}
author.AddMessageReference(*ref, s)
@ -186,22 +186,20 @@ func NewMessage(m discord.Message, s *state.Instance, author Author) Message {
// Request members in mentions if we're in a guild.
if m.GuildID.IsValid() {
for _, segment := range content.Segments {
if mention, ok := segment.(*mention.Segment); ok {
// If this is not a user mention, then skip.
if mention.User == nil {
continue
}
// If we already have a member, then skip. We could check this
// using the timestamp, as we might have a user set into the
// member field
if mention.User.Member.Joined.IsValid() {
continue
}
// Request the member.
s.MemberState.RequestMember(m.GuildID, mention.User.Member.User.ID)
mention, ok := segment.(*mention.Segment)
if !ok {
continue
}
// If this is not a user mention, then skip. If we already have a
// member, then skip. We could check this using the timestamp, as we
// might have a user set into the member field.
if mention.User == nil || mention.User.Member.Joined.IsValid() {
continue
}
// Request the member.
s.MemberState.RequestMember(m.GuildID, mention.User.Member.User.ID)
}
}
@ -233,15 +231,26 @@ func (m Message) Mentioned() bool {
// ReferencedMessage searches for the referenced message if needed.
func ReferencedMessage(m discord.Message, s *state.Instance, wait bool) (reply *discord.Message) {
if m.ReferencedMessage != nil {
return m.ReferencedMessage
}
// Deleted or does not exist.
if m.Reference == nil || !m.Reference.MessageID.IsValid() {
return nil
}
// Check these in case.
if !m.Reference.ChannelID.IsValid() {
m.Reference.ChannelID = m.ChannelID
}
if !m.Reference.GuildID.IsValid() {
m.Reference.GuildID = m.GuildID
}
if m.ReferencedMessage != nil {
// Set these in case Discord acts dumb.
m.ReferencedMessage.GuildID = m.Reference.GuildID
m.ReferencedMessage.ChannelID = m.Reference.ChannelID
return m.ReferencedMessage
}
if !wait {
reply, _ = s.Cabinet.Message(m.Reference.ChannelID, m.Reference.MessageID)
} else {