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-07 01:53:15 +00:00
|
|
|
*empty.Server
|
|
|
|
*shared.Channel
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ cchat.Server = (*Channel)(nil)
|
|
|
|
|
|
|
|
func New(s *state.Instance, ch discord.Channel) (cchat.Server, error) {
|
|
|
|
// Ensure the state keeps the channel's permission.
|
|
|
|
_, err := s.Permissions(ch.ID, s.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to get permission")
|
|
|
|
}
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
return Channel{
|
|
|
|
Channel: &shared.Channel{
|
|
|
|
ID: ch.ID,
|
|
|
|
GuildID: ch.GuildID,
|
|
|
|
State: s,
|
|
|
|
},
|
2020-09-08 04:44:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// self does not do IO.
|
2020-10-07 01:53:15 +00:00
|
|
|
func (ch Channel) self() (*discord.Channel, error) {
|
|
|
|
return ch.State.Store.Channel(ch.Channel.ID)
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// messages does not do IO.
|
2020-10-07 01:53:15 +00:00
|
|
|
func (ch Channel) messages() ([]discord.Message, error) {
|
|
|
|
return ch.State.Store.Messages(ch.Channel.ID)
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
func (ch Channel) guild() (*discord.Guild, error) {
|
|
|
|
if ch.GuildID.IsValid() {
|
|
|
|
return ch.State.Store.Guild(ch.GuildID)
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
return nil, errors.New("channel not in a guild")
|
|
|
|
}
|
|
|
|
|
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-09-08 04:44:09 +00:00
|
|
|
c, err := ch.self()
|
|
|
|
if err != nil {
|
2020-10-07 01:53:15 +00:00
|
|
|
return text.Rich{Content: ch.Channel.ID.String()}
|
2020-09-08 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.NSFW {
|
|
|
|
return text.Rich{Content: "#!" + c.Name}
|
|
|
|
} else {
|
|
|
|
return text.Rich{Content: "#" + c.Name}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|