1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-11-01 12:24:15 +00:00
cchat-discord/internal/discord/channel/editor.go

62 lines
1.5 KiB
Go
Raw Normal View History

2020-09-08 04:44:09 +00:00
package channel
import (
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/cchat"
"github.com/pkg/errors"
)
var _ cchat.Editor = (*Channel)(nil)
// IsEditor returns true if the user can send messages in this channel.
func (ch *Channel) IsEditor() bool {
p, err := ch.state.StateOnly().Permissions(ch.id, ch.state.UserID)
if err != nil {
return false
}
return p.Has(discord.PermissionSendMessages)
}
// 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.state.Store.Message(ch.id, discord.MessageID(s))
if err != nil {
return false
}
return m.Author.ID == ch.state.UserID
}
// RawMessageContent returns the raw message content from Discord.
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.state.Store.Message(ch.id, discord.MessageID(s))
if err != nil {
return "", errors.Wrap(err, "Failed to get the message")
}
return m.Content, nil
}
// EditMessage edits the message to the given content string.
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.state.EditText(ch.id, discord.MessageID(s), content)
return err
}