1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-01 12:24:15 +00:00
cchat-discord/internal/discord/guild/guild.go

123 lines
2.9 KiB
Go
Raw Normal View History

2020-09-08 04:44:09 +00:00
package guild
import (
"context"
"sort"
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/arikawa/v2/discord"
"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/category"
"github.com/diamondburned/cchat-discord/internal/discord/channel"
"github.com/diamondburned/cchat-discord/internal/discord/state"
"github.com/diamondburned/cchat-discord/internal/urlutils"
"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
"github.com/pkg/errors"
)
type Guild struct {
2020-10-07 01:53:15 +00:00
empty.Server
2020-09-08 04:44:09 +00:00
id discord.GuildID
state *state.Instance
}
2020-10-07 01:53:15 +00:00
func New(s *state.Instance, g *discord.Guild) cchat.Server {
2020-09-08 04:44:09 +00:00
return &Guild{
id: g.ID,
state: s,
}
}
2020-10-07 01:53:15 +00:00
func NewFromID(s *state.Instance, gID discord.GuildID) (cchat.Server, error) {
2020-09-08 04:44:09 +00:00
g, err := s.Guild(gID)
if err != nil {
return nil, err
}
return New(s, g), nil
}
func (g *Guild) self(ctx context.Context) (*discord.Guild, error) {
return g.state.WithContext(ctx).Guild(g.id)
}
func (g *Guild) selfState() (*discord.Guild, error) {
2020-12-20 05:44:26 +00:00
return g.state.Cabinet.Guild(g.id)
2020-09-08 04:44:09 +00:00
}
func (g *Guild) ID() cchat.ID {
return g.id.String()
}
func (g *Guild) Name() text.Rich {
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-10-07 01:53:15 +00:00
func (g *Guild) AsIconer() cchat.Iconer { return g }
2020-09-08 04:44:09 +00:00
func (g *Guild) Icon(ctx context.Context, iconer cchat.IconContainer) (func(), error) {
s, err := g.self(ctx)
if err != nil {
// This shouldn't happen.
return nil, errors.Wrap(err, "Failed to get guild")
}
// Used for comparison.
if s.Icon != "" {
iconer.SetIcon(urlutils.AvatarURL(s.IconURL()))
}
return g.state.AddHandler(func(update *gateway.GuildUpdateEvent) {
if g.id == update.ID {
iconer.SetIcon(urlutils.AvatarURL(s.IconURL()))
}
}), nil
}
2020-10-07 01:53:15 +00:00
func (g *Guild) AsLister() cchat.Lister { return g }
2020-09-08 04:44:09 +00:00
func (g *Guild) Servers(container cchat.ServersContainer) error {
c, err := g.state.Channels(g.id)
if err != nil {
return errors.Wrap(err, "Failed to get channels")
}
// Only get top-level channels (those with category ID being null).
var toplevels = category.FilterAccessible(g.state, category.FilterCategory(c, 0))
// Sort so that positions are correct.
sort.SliceStable(toplevels, func(i, j int) bool {
return toplevels[i].Position < toplevels[j].Position
})
// Sort so that channels are before categories.
sort.SliceStable(toplevels, func(i, _ int) bool {
return toplevels[i].Type != discord.GuildCategory
})
var chs = make([]cchat.Server, 0, len(toplevels))
for _, ch := range toplevels {
switch ch.Type {
case discord.GuildCategory:
chs = append(chs, category.New(g.state, ch))
case discord.GuildText:
c, err := channel.New(g.state, ch)
if err != nil {
return errors.Wrapf(err, "Failed to make channel %q: %v", ch.Name, err)
}
chs = append(chs, c)
}
}
container.SetServers(chs)
return nil
}