2020-12-19 05:46:12 +00:00
|
|
|
// Package shared contains channel utilities.
|
2020-10-07 01:53:15 +00:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
2020-12-17 08:01:58 +00:00
|
|
|
"errors"
|
2020-12-19 05:46:12 +00:00
|
|
|
"strings"
|
2020-12-17 08:01:58 +00:00
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
|
|
|
"github.com/diamondburned/cchat-discord/internal/discord/state"
|
|
|
|
)
|
|
|
|
|
2020-12-19 05:46:12 +00:00
|
|
|
// PrivateName returns the channel name if any, otherwise it formats its own
|
|
|
|
// name into a list of recipients.
|
|
|
|
func PrivateName(privCh discord.Channel) string {
|
|
|
|
if privCh.Name != "" {
|
|
|
|
return privCh.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return FormatRecipients(privCh.DMRecipients)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormatRecipients joins the given list of users into a string listing all
|
|
|
|
// recipients with English punctuation rules.
|
|
|
|
func FormatRecipients(users []discord.User) string {
|
|
|
|
switch len(users) {
|
|
|
|
case 0:
|
|
|
|
return "<Nobody>"
|
|
|
|
case 1:
|
|
|
|
return users[0].Username
|
|
|
|
case 2:
|
|
|
|
return users[0].Username + " and " + users[1].Username
|
|
|
|
}
|
|
|
|
|
|
|
|
var usernames = make([]string, len(users))
|
|
|
|
for i, user := range users[:len(users)-1] {
|
|
|
|
usernames[i] = user.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(usernames, ", ") + " and " + users[len(users)-1].Username
|
|
|
|
}
|
|
|
|
|
2020-10-07 01:53:15 +00:00
|
|
|
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 {
|
|
|
|
p, err := ch.State.StateOnly().Permissions(ch.ID, ch.State.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, perm := range perms {
|
|
|
|
if !p.Has(perm) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch Channel) Messages() ([]discord.Message, error) {
|
|
|
|
return ch.State.Store.Messages(ch.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch Channel) Guild() (*discord.Guild, error) {
|
2020-12-17 08:01:58 +00:00
|
|
|
if !ch.GuildID.IsValid() {
|
|
|
|
return nil, errors.New("channel not in guild")
|
|
|
|
}
|
2020-10-07 01:53:15 +00:00
|
|
|
return ch.State.Store.Guild(ch.GuildID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch Channel) Self() (*discord.Channel, error) {
|
|
|
|
return ch.State.Store.Channel(ch.ID)
|
|
|
|
}
|