1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-01 20:34:27 +00:00
cchat-discord/guild.go

79 lines
1.5 KiB
Go
Raw Normal View History

2020-06-14 22:46:07 +00:00
package discord
import (
"context"
2020-06-14 22:46:07 +00:00
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat/text"
"github.com/pkg/errors"
2020-06-14 22:46:07 +00:00
)
type Guild struct {
id discord.Snowflake
session *Session
}
var (
_ 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,
}
}
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 {
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
}
func (g *Guild) Icon(ctx context.Context, iconer cchat.IconContainer) error {
s, err := g.self(ctx)
if err != nil {
// This shouldn't happen.
return errors.Wrap(err, "Failed to get guild")
}
if s.Icon != "" {
iconer.SetIcon(s.IconURL() + "?size=64")
}
return nil
}
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")
}
var channels = make([]cchat.Server, len(c))
for i := range c {
channels[i] = NewChannel(g.session, c[i])
}
container.SetServers(channels)
2020-06-14 22:46:07 +00:00
return nil
}