2020-06-14 22:46:07 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2020-06-16 03:57:33 +00:00
|
|
|
"context"
|
2020-06-19 08:09:41 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-06-16 03:57:33 +00:00
|
|
|
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
2020-06-19 08:09:41 +00:00
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/cchat"
|
2020-06-19 08:09:41 +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
|
|
|
type GuildFolder struct {
|
|
|
|
gateway.GuildFolder
|
|
|
|
session *Session
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ cchat.Server = (*Guild)(nil)
|
|
|
|
_ cchat.ServerList = (*Guild)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewGuildFolder(s *Session, gf gateway.GuildFolder) *GuildFolder {
|
|
|
|
// Name should never be empty.
|
|
|
|
if gf.Name == "" {
|
|
|
|
var names = make([]string, 0, len(gf.GuildIDs))
|
|
|
|
|
|
|
|
for _, id := range gf.GuildIDs {
|
|
|
|
if g, _ := s.Store.Guild(id); g != nil {
|
|
|
|
names = append(names, g.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gf.Name = strings.Join(names, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GuildFolder{
|
|
|
|
GuildFolder: gf,
|
|
|
|
session: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gf *GuildFolder) ID() string {
|
|
|
|
return gf.GuildFolder.ID.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gf *GuildFolder) Name() text.Rich {
|
|
|
|
var name = text.Rich{
|
|
|
|
// 1en space for style.
|
|
|
|
Content: gf.GuildFolder.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
if gf.GuildFolder.Color > 0 {
|
|
|
|
name.Segments = []text.Segment{
|
|
|
|
// The length of this black box is actually 3. Mind == blown.
|
|
|
|
segments.NewColored(len(name.Content), gf.GuildFolder.Color.Uint32()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gf *GuildFolder) Servers(container cchat.ServersContainer) error {
|
|
|
|
var servers = make([]cchat.Server, len(gf.GuildIDs))
|
|
|
|
|
|
|
|
for i, id := range gf.GuildIDs {
|
|
|
|
g, err := gf.session.Guild(id)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to get guild ID "+id.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
servers[i] = NewGuild(gf.session, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
container.SetServers(servers)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:46:07 +00:00
|
|
|
type Guild struct {
|
|
|
|
id discord.Snowflake
|
|
|
|
session *Session
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-06-16 03:57:33 +00:00
|
|
|
_ cchat.Icon = (*Guild)(nil)
|
|
|
|
_ cchat.Server = (*Guild)(nil)
|
|
|
|
_ cchat.ServerList = (*Guild)(nil)
|
2020-06-14 22:46:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewGuild(s *Session, g *discord.Guild) *Guild {
|
|
|
|
return &Guild{
|
|
|
|
id: g.ID,
|
|
|
|
session: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:09:41 +00:00
|
|
|
func NewGuildFromID(s *Session, gID discord.Snowflake) (*Guild, error) {
|
|
|
|
g, err := s.Guild(gID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewGuild(s, g), nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
func (g *Guild) self(ctx context.Context) (*discord.Guild, error) {
|
|
|
|
return g.session.WithContext(ctx).Guild(g.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Guild) selfState() (*discord.Guild, error) {
|
|
|
|
return g.session.Store.Guild(g.id)
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:46:07 +00:00
|
|
|
func (g *Guild) ID() string {
|
|
|
|
return g.id.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Guild) Name() text.Rich {
|
2020-06-16 03:57:33 +00:00
|
|
|
s, err := g.selfState()
|
|
|
|
if err != nil {
|
|
|
|
// This shouldn't happen.
|
|
|
|
return text.Rich{Content: g.id.String()}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.Rich{Content: s.Name}
|
2020-06-14 22:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:14:44 +00:00
|
|
|
func (g *Guild) Icon(ctx context.Context, iconer cchat.IconContainer) (func(), error) {
|
2020-06-16 03:57:33 +00:00
|
|
|
s, err := g.self(ctx)
|
|
|
|
if err != nil {
|
|
|
|
// This shouldn't happen.
|
2020-06-30 03:14:44 +00:00
|
|
|
return nil, errors.Wrap(err, "Failed to get guild")
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:14:44 +00:00
|
|
|
// Used for comparison.
|
|
|
|
var hash = s.Icon
|
|
|
|
if hash != "" {
|
|
|
|
iconer.SetIcon(AvatarURL(s.IconURL()))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
2020-06-30 03:14:44 +00:00
|
|
|
|
|
|
|
return g.session.AddHandler(func(g *gateway.GuildUpdateEvent) {
|
|
|
|
if g.Icon != hash {
|
|
|
|
hash = g.Icon
|
|
|
|
iconer.SetIcon(AvatarURL(s.IconURL()))
|
|
|
|
}
|
|
|
|
}), nil
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Guild) Servers(container cchat.ServersContainer) error {
|
|
|
|
c, err := g.session.Channels(g.id)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to get channels")
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:09:41 +00:00
|
|
|
// Only get top-level channels (those with category ID being null).
|
|
|
|
var toplevels = filterAccessible(g.session, filterCategory(c, discord.NullSnowflake))
|
|
|
|
|
|
|
|
sort.Slice(toplevels, func(i, j int) bool {
|
|
|
|
return toplevels[i].Position < toplevels[j].Position
|
|
|
|
})
|
|
|
|
|
|
|
|
var chs = make([]cchat.Server, 0, len(toplevels))
|
|
|
|
|
|
|
|
for _, ch := range toplevels {
|
|
|
|
switch ch.Type {
|
|
|
|
case discord.GuildCategory:
|
|
|
|
chs = append(chs, NewCategory(g.session, ch))
|
|
|
|
case discord.GuildText:
|
|
|
|
chs = append(chs, NewChannel(g.session, ch))
|
|
|
|
}
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 08:09:41 +00:00
|
|
|
container.SetServers(chs)
|
2020-06-14 22:46:07 +00:00
|
|
|
return nil
|
|
|
|
}
|