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 {
|
2020-10-14 20:22:11 +00:00
|
|
|
empty.Server
|
2020-12-17 08:01:58 +00:00
|
|
|
shared.Channel
|
2020-10-14 20:22:11 +00:00
|
|
|
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) {
|
2020-12-17 08:01:58 +00:00
|
|
|
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 {
|
2020-12-17 08:01:58 +00:00
|
|
|
return Channel{}, errors.Wrap(err, "Failed to get permission")
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 08:01:58 +00:00
|
|
|
sharedCh := shared.Channel{
|
2020-10-14 20:22:11 +00:00
|
|
|
ID: ch.ID,
|
|
|
|
GuildID: ch.GuildID,
|
|
|
|
State: s,
|
|
|
|
}
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
return Channel{
|
2020-10-14 20:22:11 +00:00
|
|
|
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 {
|
2020-10-14 20:22:11 +00:00
|
|
|
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 {
|
2020-12-17 08:01:58 +00:00
|
|
|
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
|
|
|
|
2020-10-14 20:22:11 +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)
|
|
|
|
}
|