2020-06-14 22:46:07 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2020-06-16 03:57:33 +00:00
|
|
|
"context"
|
2020-06-30 03:14:44 +00:00
|
|
|
"time"
|
2020-06-16 03:57:33 +00:00
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
"github.com/diamondburned/arikawa/api"
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/cchat"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/diamondburned/cchat-discord/segments"
|
2020-06-14 22:46:07 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-06-16 03:57:33 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-06-14 22:46:07 +00:00
|
|
|
)
|
|
|
|
|
2020-06-19 08:09:41 +00:00
|
|
|
func chGuildCheck(chType discord.ChannelType) bool {
|
|
|
|
switch chType {
|
|
|
|
case discord.GuildCategory, discord.GuildText:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterAccessible(s *Session, chs []discord.Channel) []discord.Channel {
|
|
|
|
u, err := s.Me()
|
|
|
|
if err != nil {
|
|
|
|
// Shouldn't happen.
|
|
|
|
return chs
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := chs[:0]
|
|
|
|
|
|
|
|
for _, ch := range chs {
|
|
|
|
p, err := s.Permissions(ch.ID, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Has(discord.PermissionViewChannel) {
|
|
|
|
filtered = append(filtered, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterCategory(chs []discord.Channel, catID discord.Snowflake) []discord.Channel {
|
|
|
|
var filtered = chs[:0]
|
|
|
|
|
|
|
|
for _, ch := range chs {
|
|
|
|
if ch.CategoryID == catID && chGuildCheck(ch.Type) {
|
|
|
|
filtered = append(filtered, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:46:07 +00:00
|
|
|
type Channel struct {
|
|
|
|
id discord.Snowflake
|
|
|
|
guildID discord.Snowflake
|
|
|
|
session *Session
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
var (
|
2020-06-30 03:14:44 +00:00
|
|
|
_ cchat.Server = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessage = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessageSender = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessageSendCompleter = (*Channel)(nil)
|
|
|
|
_ cchat.ServerNickname = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessageEditor = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessageActioner = (*Channel)(nil)
|
|
|
|
_ cchat.ServerMessageTypingIndicator = (*Channel)(nil)
|
2020-06-16 03:57:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewChannel(s *Session, ch discord.Channel) *Channel {
|
2020-06-14 22:46:07 +00:00
|
|
|
return &Channel{
|
|
|
|
id: ch.ID,
|
|
|
|
guildID: ch.GuildID,
|
|
|
|
session: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 02:31:32 +00:00
|
|
|
// self does not do IO.
|
|
|
|
func (ch *Channel) self() (*discord.Channel, error) {
|
|
|
|
return ch.session.Store.Channel(ch.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// messages does not do IO.
|
|
|
|
func (ch *Channel) messages() ([]discord.Message, error) {
|
|
|
|
return ch.session.Store.Messages(ch.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) guild() (*discord.Guild, error) {
|
|
|
|
if ch.guildID.Valid() {
|
|
|
|
return ch.session.Guild(ch.guildID)
|
|
|
|
}
|
|
|
|
return nil, errors.New("channel not in a guild")
|
|
|
|
}
|
|
|
|
|
2020-06-14 22:46:07 +00:00
|
|
|
func (ch *Channel) ID() string {
|
|
|
|
return ch.id.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) Name() text.Rich {
|
2020-06-29 02:31:32 +00:00
|
|
|
c, err := ch.self()
|
2020-06-19 22:27:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return text.Rich{Content: ch.id.String()}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.NSFW {
|
|
|
|
return text.Rich{Content: "#!" + c.Name}
|
|
|
|
} else {
|
|
|
|
return text.Rich{Content: "#" + c.Name}
|
|
|
|
}
|
2020-06-14 22:46:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 03:14:44 +00:00
|
|
|
func (ch *Channel) Nickname(ctx context.Context, labeler cchat.LabelContainer) (func(), error) {
|
2020-06-16 03:57:33 +00:00
|
|
|
// We don't have a nickname if we're not in a guild.
|
|
|
|
if !ch.guildID.Valid() {
|
2020-06-30 03:14:44 +00:00
|
|
|
return func() {}, nil
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state := ch.session.WithContext(ctx)
|
|
|
|
|
|
|
|
// MemberColor should fill up the state cache.
|
|
|
|
c, err := state.MemberColor(ch.guildID, ch.session.userID)
|
|
|
|
if err != nil {
|
2020-06-30 03:14:44 +00:00
|
|
|
return nil, errors.Wrap(err, "Failed to get self member color")
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err := state.Member(ch.guildID, ch.session.userID)
|
|
|
|
if err != nil {
|
2020-06-30 03:14:44 +00:00
|
|
|
return nil, errors.Wrap(err, "Failed to get self member")
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var rich = text.Rich{Content: m.User.Username}
|
|
|
|
if m.Nick != "" {
|
|
|
|
rich.Content = m.Nick
|
|
|
|
}
|
|
|
|
if c > 0 {
|
|
|
|
rich.Segments = []text.Segment{
|
|
|
|
segments.NewColored(len(rich.Content), c.Uint32()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
labeler.SetLabel(rich)
|
2020-06-30 03:14:44 +00:00
|
|
|
|
|
|
|
// Copy the user ID to use.
|
|
|
|
var selfID = m.User.ID
|
|
|
|
|
|
|
|
return ch.session.AddHandler(func(g *gateway.GuildMemberUpdateEvent) {
|
|
|
|
if g.GuildID != ch.guildID || g.User.ID != selfID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rich = text.Rich{Content: m.User.Username}
|
|
|
|
if m.Nick != "" {
|
|
|
|
rich.Content = m.Nick
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := ch.session.MemberColor(g.GuildID, selfID)
|
|
|
|
if err == nil {
|
|
|
|
rich.Segments = []text.Segment{
|
|
|
|
segments.NewColored(len(rich.Content), c.Uint32()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
labeler.SetLabel(rich)
|
|
|
|
}), nil
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) (func(), error) {
|
|
|
|
state := ch.session.WithContext(ctx)
|
|
|
|
|
|
|
|
m, err := state.Messages(ch.id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var addcancel = newCancels()
|
2020-06-14 22:46:07 +00:00
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
var constructor func(discord.Message) cchat.MessageCreate
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
if ch.guildID.Valid() {
|
|
|
|
// Create the backlog without any member information.
|
|
|
|
g, err := state.Guild(ch.guildID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Failed to get guild")
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
constructor = func(m discord.Message) cchat.MessageCreate {
|
|
|
|
return NewBacklogMessage(m, ch.session, *g)
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
// Listen to new members before creating the backlog and requesting members.
|
|
|
|
addcancel(ch.session.AddHandler(func(c *gateway.GuildMembersChunkEvent) {
|
2020-06-17 07:20:46 +00:00
|
|
|
if c.GuildID != ch.guildID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-29 02:31:32 +00:00
|
|
|
m, err := ch.messages()
|
2020-06-16 03:57:33 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO: log
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-29 02:31:32 +00:00
|
|
|
g, err := ch.guild()
|
2020-06-16 03:57:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
// Loop over all messages and replace the author. The latest
|
|
|
|
// messages are in front.
|
|
|
|
for _, msg := range m {
|
|
|
|
for _, member := range c.Members {
|
2020-06-16 03:57:33 +00:00
|
|
|
if msg.Author.ID != member.User.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ct.UpdateMessage(NewMessageUpdateAuthor(msg, member, *g))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
} else {
|
2020-06-17 07:20:46 +00:00
|
|
|
constructor = func(m discord.Message) cchat.MessageCreate {
|
2020-06-19 01:00:24 +00:00
|
|
|
return NewDirectMessage(m, ch.session)
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:20:46 +00:00
|
|
|
// Iterate from the earliest messages to the latest messages.
|
|
|
|
for i := len(m) - 1; i >= 0; i-- {
|
|
|
|
ct.CreateMessage(constructor(m[i]))
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
// Bind the handler.
|
|
|
|
addcancel(
|
|
|
|
ch.session.AddHandler(func(m *gateway.MessageCreateEvent) {
|
2020-06-17 07:20:46 +00:00
|
|
|
if m.ChannelID == ch.id {
|
|
|
|
ct.CreateMessage(NewMessageCreate(m, ch.session))
|
|
|
|
}
|
2020-06-16 03:57:33 +00:00
|
|
|
}),
|
|
|
|
ch.session.AddHandler(func(m *gateway.MessageUpdateEvent) {
|
|
|
|
// If the updated content is empty. TODO: add embed support.
|
2020-06-17 07:20:46 +00:00
|
|
|
if m.ChannelID == ch.id && m.Content != "" {
|
|
|
|
ct.UpdateMessage(NewMessageUpdateContent(m.Message))
|
2020-06-16 03:57:33 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
ch.session.AddHandler(func(m *gateway.MessageDeleteEvent) {
|
2020-06-17 07:20:46 +00:00
|
|
|
if m.ChannelID == ch.id {
|
|
|
|
ct.DeleteMessage(NewHeaderDelete(m))
|
|
|
|
}
|
2020-06-16 03:57:33 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
return joinCancels(addcancel()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) SendMessage(msg cchat.SendableMessage) error {
|
2020-06-17 07:20:46 +00:00
|
|
|
var send = api.SendMessageData{Content: msg.Content()}
|
|
|
|
if noncer, ok := msg.(cchat.MessageNonce); ok {
|
|
|
|
send.Nonce = noncer.Nonce()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ch.session.SendMessageComplex(ch.id, send)
|
2020-06-16 03:57:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-29 02:31:32 +00:00
|
|
|
// MessageEditable returns true if the given message ID belongs to the current
|
|
|
|
// user.
|
|
|
|
func (ch *Channel) MessageEditable(id string) bool {
|
|
|
|
s, err := discord.ParseSnowflake(id)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := ch.session.Store.Message(ch.id, s)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.Author.ID == ch.session.userID
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawMessageContent returns the raw message content from Discord.
|
2020-06-28 02:38:27 +00:00
|
|
|
func (ch *Channel) RawMessageContent(id string) (string, error) {
|
|
|
|
s, err := discord.ParseSnowflake(id)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Failed to parse ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := ch.session.Store.Message(ch.id, s)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Failed to get the message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.Content, nil
|
|
|
|
}
|
|
|
|
|
2020-06-29 02:31:32 +00:00
|
|
|
// EditMessage edits the message to the given content string.
|
2020-06-28 02:38:27 +00:00
|
|
|
func (ch *Channel) EditMessage(id, content string) error {
|
|
|
|
s, err := discord.ParseSnowflake(id)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to parse ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ch.session.EditText(ch.id, s, content)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ActionDelete = "Delete"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrUnknownAction = errors.New("unknown message action")
|
|
|
|
|
|
|
|
func (ch *Channel) DoMessageAction(action, id string) error {
|
|
|
|
s, err := discord.ParseSnowflake(id)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to parse ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case ActionDelete:
|
|
|
|
return ch.session.DeleteMessage(ch.id, s)
|
|
|
|
default:
|
|
|
|
return ErrUnknownAction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) MessageActions(id string) []string {
|
|
|
|
s, err := discord.ParseSnowflake(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := ch.session.Store.Message(ch.id, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current user.
|
|
|
|
u, err := ch.session.Store.Me()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can we have delete? We can if this is our own message.
|
|
|
|
var canDelete = m.Author.ID == u.ID
|
|
|
|
|
|
|
|
// We also can if we have the Manage Messages permission, which would allow
|
|
|
|
// us to delete others' messages.
|
|
|
|
if !canDelete {
|
|
|
|
canDelete = ch.canManageMessages(u.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if canDelete {
|
|
|
|
return []string{ActionDelete}
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// canManageMessages returns whether or not the user is allowed to manage
|
|
|
|
// messages.
|
|
|
|
func (ch *Channel) canManageMessages(userID discord.Snowflake) bool {
|
|
|
|
// If we're not in a guild, then clearly we cannot.
|
|
|
|
if !ch.guildID.Valid() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need the guild, member and channel to calculate the permission
|
|
|
|
// overrides.
|
|
|
|
|
2020-06-29 02:31:32 +00:00
|
|
|
g, err := ch.guild()
|
2020-06-28 02:38:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-29 02:31:32 +00:00
|
|
|
c, err := ch.self()
|
2020-06-28 02:38:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := ch.session.Store.Member(ch.guildID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
p := discord.CalcOverwrites(*g, *c, *m)
|
|
|
|
// The Manage Messages permission allows the user to delete others'
|
|
|
|
// messages, so we'll return true if that is the case.
|
|
|
|
return p.Has(discord.PermissionManageMessages)
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:14:44 +00:00
|
|
|
// CompleteMessage implements message input completion capability for Discord.
|
|
|
|
// This method supports user mentions, channel mentions and emojis.
|
|
|
|
//
|
|
|
|
// For the individual implementations, refer to channel_completion.go.
|
2020-06-29 02:31:32 +00:00
|
|
|
func (ch *Channel) CompleteMessage(words []string, i int) (entries []cchat.CompletionEntry) {
|
|
|
|
var word = words[i]
|
|
|
|
// Word should have at least a character for the char check.
|
|
|
|
if len(word) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch word[0] {
|
|
|
|
case '@':
|
|
|
|
return ch.completeMentions(word[1:])
|
|
|
|
case '#':
|
|
|
|
return ch.completeChannels(word[1:])
|
|
|
|
case ':':
|
|
|
|
return ch.completeEmojis(word[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:14:44 +00:00
|
|
|
func (ch *Channel) Typing() error {
|
|
|
|
return ch.session.Typing(ch.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TypingTimeout returns 8 seconds.
|
|
|
|
func (ch *Channel) TypingTimeout() time.Duration {
|
|
|
|
return 8 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) TypingSubscribe(ti cchat.TypingIndicator) (func(), error) {
|
|
|
|
return ch.session.AddHandler(func(t *gateway.TypingStartEvent) {
|
|
|
|
if t.ChannelID != ch.id {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch.guildID.Valid() {
|
|
|
|
g, err := ch.session.Store.Guild(t.GuildID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Member == nil {
|
|
|
|
t.Member, err = ch.session.Store.Member(t.GuildID, t.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 03:23:09 +00:00
|
|
|
ti.AddTyper(NewTyper(NewGuildMember(*t.Member, *g), t))
|
2020-06-30 03:14:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := ch.self()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range c.DMRecipients {
|
|
|
|
if user.ID == t.UserID {
|
2020-07-03 03:23:09 +00:00
|
|
|
ti.AddTyper(NewTyper(NewUser(user), t))
|
2020-06-30 03:14:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:57:33 +00:00
|
|
|
func newCancels() func(...func()) []func() {
|
|
|
|
var cancels []func()
|
|
|
|
return func(appended ...func()) []func() {
|
|
|
|
cancels = append(cancels, appended...)
|
|
|
|
return cancels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinCancels(cancellers []func()) func() {
|
|
|
|
return func() {
|
|
|
|
for _, c := range cancellers {
|
|
|
|
c()
|
|
|
|
}
|
|
|
|
}
|
2020-06-14 22:46:07 +00:00
|
|
|
}
|