2020-06-14 22:46:07 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2020-06-16 03:57:33 +00:00
|
|
|
"context"
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
"github.com/diamondburned/arikawa/api"
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/cchat"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/segments"
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-06-14 22:46:07 +00:00
|
|
|
)
|
|
|
|
|
2020-06-19 08:09:41 +00:00
|
|
|
func chGuildCheck(chType discord.ChannelType) bool {
|
|
|
|
switch chType {
|
|
|
|
case discord.GuildCategory, discord.GuildText:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterAccessible(s *Session, chs []discord.Channel) []discord.Channel {
|
|
|
|
u, err := s.Me()
|
|
|
|
if err != nil {
|
|
|
|
// Shouldn't happen.
|
|
|
|
return chs
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := chs[:0]
|
|
|
|
|
|
|
|
for _, ch := range chs {
|
|
|
|
p, err := s.Permissions(ch.ID, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Has(discord.PermissionViewChannel) {
|
|
|
|
filtered = append(filtered, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterCategory(chs []discord.Channel, catID discord.Snowflake) []discord.Channel {
|
|
|
|
var filtered = chs[:0]
|
|
|
|
|
|
|
|
for _, ch := range chs {
|
|
|
|
if ch.CategoryID == catID && chGuildCheck(ch.Type) {
|
|
|
|
filtered = append(filtered, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:46:07 +00:00
|
|
|
type Channel struct {
|
|
|
|
id discord.Snowflake
|
|
|
|
guildID discord.Snowflake
|
|
|
|
name string
|
|
|
|
session *Session
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
var (
|
|
|
|
_ cchat.Server = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessage = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessageSender = (*Channel)(nil)
|
|
|
|
// _ cchat.ServerMessageSendCompleter = (*Channel)(nil)
|
|
|
|
_ cchat.ServerNickname = (*Channel)(nil)
|
|
|
|
// _ cchat.ServerMessageEditor = (*Channel)(nil)
|
|
|
|
// _ cchat.ServerMessageActioner = (*Channel)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewChannel(s *Session, ch discord.Channel) *Channel {
|
2020-06-14 22:46:07 +00:00
|
|
|
return &Channel{
|
|
|
|
id: ch.ID,
|
|
|
|
guildID: ch.GuildID,
|
|
|
|
name: ch.Name,
|
|
|
|
session: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) ID() string {
|
|
|
|
return ch.id.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) Name() text.Rich {
|
|
|
|
return text.Rich{Content: "#" + ch.name}
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
func (ch *Channel) Nickname(ctx context.Context, labeler cchat.LabelContainer) error {
|
|
|
|
// We don't have a nickname if we're not in a guild.
|
|
|
|
if !ch.guildID.Valid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
state := ch.session.WithContext(ctx)
|
|
|
|
|
|
|
|
// MemberColor should fill up the state cache.
|
|
|
|
c, err := state.MemberColor(ch.guildID, ch.session.userID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to get self member color")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := state.Member(ch.guildID, ch.session.userID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to get self member")
|
|
|
|
}
|
|
|
|
|
|
|
|
var rich = text.Rich{Content: m.User.Username}
|
|
|
|
if m.Nick != "" {
|
|
|
|
rich.Content = m.Nick
|
|
|
|
}
|
|
|
|
if c > 0 {
|
|
|
|
rich.Segments = []text.Segment{
|
|
|
|
segments.NewColored(len(rich.Content), c.Uint32()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
labeler.SetLabel(rich)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (func(), error) {
|
|
|
|
state := ch.session.WithContext(ctx)
|
|
|
|
|
|
|
|
m, err := state.Messages(ch.id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var addcancel = newCancels()
|
2020-06-14 22:46:07 +00:00
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
var constructor func(discord.Message) cchat.MessageCreate
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
if ch.guildID.Valid() {
|
|
|
|
// Create the backlog without any member information.
|
|
|
|
g, err := state.Guild(ch.guildID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to get guild")
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
constructor = func(m discord.Message) cchat.MessageCreate {
|
|
|
|
return NewBacklogMessage(m, ch.session, *g)
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
// Listen to new members before creating the backlog and requesting members.
|
|
|
|
addcancel(ch.session.AddHandler(func(c *gateway.GuildMembersChunkEvent) {
|
2020-06-17 07:20:46 +00:00
|
|
|
if c.GuildID != ch.guildID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
m, err := ch.session.Store.Messages(ch.id)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: log
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := ch.session.Store.Guild(c.GuildID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
// Loop over all messages and replace the author. The latest
|
|
|
|
// messages are in front.
|
|
|
|
for _, msg := range m {
|
|
|
|
for _, member := range c.Members {
|
2020-06-16 03:57:33 +00:00
|
|
|
if msg.Author.ID != member.User.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ct.UpdateMessage(NewMessageUpdateAuthor(msg, member, *g))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
} else {
|
2020-06-17 07:20:46 +00:00
|
|
|
constructor = func(m discord.Message) cchat.MessageCreate {
|
2020-06-19 01:00:24 +00:00
|
|
|
return NewDirectMessage(m, ch.session)
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
// Iterate from the earliest messages to the latest messages.
|
|
|
|
for i := len(m) - 1; i >= 0; i-- {
|
|
|
|
ct.CreateMessage(constructor(m[i]))
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
// Bind the handler.
|
|
|
|
addcancel(
|
|
|
|
ch.session.AddHandler(func(m *gateway.MessageCreateEvent) {
|
2020-06-17 07:20:46 +00:00
|
|
|
if m.ChannelID == ch.id {
|
|
|
|
ct.CreateMessage(NewMessageCreate(m, ch.session))
|
|
|
|
}
|
2020-06-16 03:57:33 +00:00
|
|
|
}),
|
|
|
|
ch.session.AddHandler(func(m *gateway.MessageUpdateEvent) {
|
|
|
|
// If the updated content is empty. TODO: add embed support.
|
2020-06-17 07:20:46 +00:00
|
|
|
if m.ChannelID == ch.id && m.Content != "" {
|
|
|
|
ct.UpdateMessage(NewMessageUpdateContent(m.Message))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
ch.session.AddHandler(func(m *gateway.MessageDeleteEvent) {
|
2020-06-17 07:20:46 +00:00
|
|
|
if m.ChannelID == ch.id {
|
|
|
|
ct.DeleteMessage(NewHeaderDelete(m))
|
|
|
|
}
|
2020-06-16 03:57:33 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
return joinCancels(addcancel()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) SendMessage(msg cchat.SendableMessage) error {
|
2020-06-17 07:20:46 +00:00
|
|
|
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)
|
2020-06-16 03:57:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCancels() func(...func()) []func() {
|
|
|
|
var cancels []func()
|
|
|
|
return func(appended ...func()) []func() {
|
|
|
|
cancels = append(cancels, appended...)
|
|
|
|
return cancels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinCancels(cancellers []func()) func() {
|
|
|
|
return func() {
|
|
|
|
for _, c := range cancellers {
|
|
|
|
c()
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 22:46:07 +00:00
|
|
|
}
|