1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-01 04:14:21 +00:00
cchat-discord/internal/discord/folder/folder.go

81 lines
1.7 KiB
Go
Raw Normal View History

2020-09-08 04:44:09 +00:00
package folder
import (
"strconv"
"strings"
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/arikawa/v2/gateway"
2020-09-08 04:44:09 +00:00
"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 {
2020-12-20 05:44:26 +00:00
g, err := s.Cabinet.Guild(id)
if err == nil {
2020-09-08 04:44:09 +00:00
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 {
2021-01-06 05:02:54 +00:00
g, err := gf.state.Cabinet.Guild(id)
2020-09-08 04:44:09 +00:00
if err != nil {
continue
}
servers = append(servers, guild.New(gf.state, g))
}
container.SetServers(servers)
return nil
}