2020-10-05 03:45:34 +00:00
|
|
|
package mention
|
|
|
|
|
|
|
|
import (
|
2020-12-31 02:58:36 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
2020-10-05 03:45:34 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/segments/renderer"
|
|
|
|
"github.com/diamondburned/cchat/text"
|
|
|
|
"github.com/diamondburned/cchat/utils/empty"
|
2020-12-20 05:44:26 +00:00
|
|
|
"github.com/diamondburned/ningen/v2/md"
|
2020-10-05 03:45:34 +00:00
|
|
|
"github.com/yuin/goldmark/ast"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
renderer.Register(md.KindMention, mention)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mention(r *renderer.Text, node ast.Node, enter bool) ast.WalkStatus {
|
|
|
|
n := node.(*md.Mention)
|
|
|
|
|
|
|
|
if enter {
|
|
|
|
var seg = Segment{}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case n.Channel != nil:
|
|
|
|
seg.Start, seg.End = r.WriteString("#" + n.Channel.Name)
|
|
|
|
seg.Channel = NewChannel(*n.Channel)
|
|
|
|
case n.GuildUser != nil:
|
|
|
|
seg.Start, seg.End = r.WriteString("@" + n.GuildUser.Username)
|
|
|
|
seg.User = NewUser(r.Store, r.Message.GuildID, *n.GuildUser)
|
|
|
|
case n.GuildRole != nil:
|
|
|
|
seg.Start, seg.End = r.WriteString("@" + n.GuildRole.Name)
|
|
|
|
seg.Role = NewRole(*n.GuildRole)
|
|
|
|
default:
|
|
|
|
// Unexpected error; skip.
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Append(seg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
type Segment struct {
|
|
|
|
empty.TextSegment
|
|
|
|
Start, End int
|
|
|
|
|
|
|
|
// enums?
|
|
|
|
Channel *Channel
|
|
|
|
User *User
|
|
|
|
Role *Role
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Segment) Bounds() (start, end int) {
|
|
|
|
return s.Start, s.End
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Segment) AsColorer() text.Colorer {
|
|
|
|
switch {
|
2020-12-31 02:58:36 +00:00
|
|
|
case s.User != nil && s.User.HasColor():
|
2020-10-05 03:45:34 +00:00
|
|
|
return s.User
|
|
|
|
case s.Role != nil:
|
|
|
|
return s.Role
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Segment) AsAvatarer() text.Avatarer {
|
|
|
|
switch {
|
|
|
|
case s.User != nil:
|
|
|
|
return s.User
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Segment) AsMentioner() text.Mentioner {
|
|
|
|
switch {
|
|
|
|
case s.Channel != nil:
|
|
|
|
return s.Channel
|
|
|
|
case s.User != nil:
|
|
|
|
return s.User
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-31 02:58:36 +00:00
|
|
|
|
|
|
|
func MemberColor(guild discord.Guild, member discord.Member) (c uint32, ok bool) {
|
|
|
|
var pos int
|
|
|
|
|
|
|
|
for _, r := range guild.Roles {
|
|
|
|
for _, mr := range member.RoleIDs {
|
|
|
|
if mr != r.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Color > 0 && r.Position > pos {
|
|
|
|
c = r.Color.Uint32()
|
|
|
|
ok = true
|
|
|
|
pos = r.Position
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|