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

128 lines
2.8 KiB
Go
Raw Normal View History

2020-09-08 04:44:09 +00:00
package channel
import (
2020-12-20 05:44:26 +00:00
"context"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/gateway"
2020-09-08 04:44:09 +00:00
"github.com/diamondburned/cchat"
2020-10-07 01:53:15 +00:00
"github.com/diamondburned/cchat-discord/internal/discord/channel/message"
"github.com/diamondburned/cchat-discord/internal/discord/channel/shared"
2020-09-08 04:44:09 +00:00
"github.com/diamondburned/cchat-discord/internal/discord/state"
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/cchat-discord/internal/urlutils"
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
"github.com/pkg/errors"
)
type Channel struct {
empty.Server
shared.Channel
commander cchat.Commander
2020-09-08 04:44:09 +00:00
}
var _ cchat.Server = (*Channel)(nil)
func New(s *state.Instance, ch discord.Channel) (cchat.Server, error) {
channel, err := NewChannel(s, ch)
if err != nil {
return nil, err
}
return channel, nil
}
func NewChannel(s *state.Instance, ch discord.Channel) (Channel, error) {
2020-09-08 04:44:09 +00:00
// Ensure the state keeps the channel's permission.
2020-12-20 05:44:26 +00:00
if ch.GuildID.IsValid() {
_, err := s.Permissions(ch.ID, s.UserID)
if err != nil {
return Channel{}, errors.Wrap(err, "failed to get permission")
}
2020-09-08 04:44:09 +00:00
}
sharedCh := shared.Channel{
ID: ch.ID,
GuildID: ch.GuildID,
State: s,
}
2020-10-07 01:53:15 +00:00
return Channel{
Channel: sharedCh,
commander: NewCommander(sharedCh),
2020-09-08 04:44:09 +00:00
}, nil
}
2020-10-07 01:53:15 +00:00
func (ch Channel) ID() cchat.ID {
return ch.Channel.ID.String()
2020-09-08 04:44:09 +00:00
}
2020-10-07 01:53:15 +00:00
func (ch Channel) Name() text.Rich {
c, err := ch.Self()
2020-09-08 04:44:09 +00:00
if err != nil {
2020-12-19 05:46:12 +00:00
return text.Rich{Content: ch.ID()}
2020-09-08 04:44:09 +00:00
}
2020-12-20 05:44:26 +00:00
return text.Plain(shared.ChannelName(*c))
2020-09-08 04:44:09 +00:00
}
2020-10-07 01:53:15 +00:00
func (ch Channel) AsCommander() cchat.Commander {
return ch.commander
}
2020-10-07 01:53:15 +00:00
func (ch Channel) AsMessenger() cchat.Messenger {
if !ch.HasPermission(discord.PermissionViewChannel) {
return nil
}
return message.New(ch.Channel)
}
2020-12-20 05:44:26 +00:00
func (ch Channel) AsIconer() cchat.Iconer {
// Guild channels never have an icon.
if ch.GuildID.IsValid() {
return nil
}
c, err := ch.Self()
if err != nil {
return nil
}
// Only DM channels should have an icon.
if c.Type != discord.DirectMessage {
return nil
}
return PresenceAvatar{
user: c.DMRecipients[0],
guild: ch.GuildID,
state: ch.State,
}
}
type PresenceAvatar struct {
user discord.User
guild discord.GuildID
state *state.Instance
}
func (avy PresenceAvatar) Icon(ctx context.Context, iconer cchat.IconContainer) (func(), error) {
if avy.user.Avatar != "" {
iconer.SetIcon(urlutils.AvatarURL(avy.user.AvatarURL()))
}
// There are so many other places that could be checked, but this is good
// enough.
c, err := avy.state.Presence(avy.guild, avy.user.ID)
if err == nil && c.User.Avatar != "" {
iconer.SetIcon(urlutils.AvatarURL(c.User.AvatarURL()))
}
return avy.state.AddHandler(func(update *gateway.PresenceUpdateEvent) {
if avy.user.ID == update.User.ID {
iconer.SetIcon(urlutils.AvatarURL(update.User.AvatarURL()))
}
}), nil
}