cchat-discord/internal/discord/session/channel/shared/channel.go

53 lines
1.1 KiB
Go
Raw Normal View History

2020-12-19 05:46:12 +00:00
// Package shared contains channel utilities.
2020-10-07 01:53:15 +00:00
package shared
import (
"errors"
2020-12-20 05:44:26 +00:00
"github.com/diamondburned/arikawa/v2/discord"
2020-10-07 01:53:15 +00:00
"github.com/diamondburned/cchat-discord/internal/discord/state"
)
type Channel struct {
ID discord.ChannelID
GuildID discord.GuildID
State *state.Instance
}
// HasPermission returns true if the current user has the given permissions in
// the channel.
func (ch Channel) HasPermission(perms ...discord.Permissions) bool {
2020-12-20 05:44:26 +00:00
// Assume we have permissions in a direct message channel.
if !ch.GuildID.IsValid() {
return true
}
p, err := ch.State.Permissions(ch.ID, ch.State.UserID)
2020-10-07 01:53:15 +00:00
if err != nil {
return false
}
for _, perm := range perms {
if !p.Has(perm) {
return false
}
}
return true
}
func (ch Channel) Messages() ([]discord.Message, error) {
2020-12-20 05:44:26 +00:00
return ch.State.Cabinet.Messages(ch.ID)
2020-10-07 01:53:15 +00:00
}
func (ch Channel) Guild() (*discord.Guild, error) {
if !ch.GuildID.IsValid() {
return nil, errors.New("channel not in guild")
}
2020-12-20 05:44:26 +00:00
return ch.State.Cabinet.Guild(ch.GuildID)
2020-10-07 01:53:15 +00:00
}
func (ch Channel) Self() (*discord.Channel, error) {
2020-12-20 05:44:26 +00:00
return ch.State.Cabinet.Channel(ch.ID)
2020-10-07 01:53:15 +00:00
}