2020-09-08 04:44:09 +00:00
|
|
|
package folder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/guild"
|
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/state"
|
2020-10-07 01:53:15 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/internal/segments/colored"
|
2020-09-08 04:44:09 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-10-07 01:53:15 +00:00
|
|
|
"github.com/diamondburned/cchat/utils/empty"
|
2020-09-08 04:44:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GuildFolder struct {
|
2020-10-07 01:53:15 +00:00
|
|
|
empty.Server
|
2020-09-08 04:44:09 +00:00
|
|
|
gateway.GuildFolder
|
|
|
|
state *state.Instance
|
|
|
|
}
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
func New(s *state.Instance, gf gateway.GuildFolder) cchat.Server {
|
2020-09-08 04:44:09 +00:00
|
|
|
// 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,
|
|
|
|
state: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gf *GuildFolder) ID() cchat.ID {
|
|
|
|
return strconv.FormatInt(int64(gf.GuildFolder.ID), 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2020-10-07 01:53:15 +00:00
|
|
|
colored.New(len(name.Content), gf.GuildFolder.Color.Uint32()),
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLister returns true.
|
2020-10-07 01:53:15 +00:00
|
|
|
func (gf *GuildFolder) AsLister() cchat.Lister { return gf }
|
2020-09-08 04:44:09 +00:00
|
|
|
|
|
|
|
func (gf *GuildFolder) Servers(container cchat.ServersContainer) error {
|
|
|
|
var servers = make([]cchat.Server, 0, len(gf.GuildIDs))
|
|
|
|
|
|
|
|
for _, id := range gf.GuildIDs {
|
|
|
|
g, err := gf.state.Guild(id)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
servers = append(servers, guild.New(gf.state, g))
|
|
|
|
}
|
|
|
|
|
|
|
|
container.SetServers(servers)
|
|
|
|
return nil
|
|
|
|
}
|