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/channel/channel.go

77 lines
1.7 KiB
Go
Raw Normal View History

2020-09-08 04:44:09 +00:00
package channel
import (
"github.com/diamondburned/arikawa/discord"
"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"
"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.
_, 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
}
if c.NSFW {
return text.Rich{Content: "#" + c.Name + " (nsfw)"}
2020-09-08 04:44:09 +00:00
} else {
return text.Rich{Content: "#" + c.Name}
}
}
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)
}