cchat-discord/internal/discord/channel/message/edit/editor.go

59 lines
1.3 KiB
Go
Raw Normal View History

2020-10-07 01:53:15 +00:00
package edit
2020-09-08 04:44:09 +00:00
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/shared"
2020-09-08 04:44:09 +00:00
"github.com/pkg/errors"
)
2020-10-07 01:53:15 +00:00
type Editor struct {
shared.Channel
2020-10-07 01:53:15 +00:00
}
2020-09-08 04:44:09 +00:00
func New(ch shared.Channel) cchat.Editor {
2020-10-07 01:53:15 +00:00
return Editor{ch}
2020-09-08 04:44:09 +00:00
}
2020-10-09 17:33:16 +00:00
// IsEditable returns true if the given message ID belongs to the current
2020-09-08 04:44:09 +00:00
// user.
2020-10-09 17:33:16 +00:00
func (ed Editor) IsEditable(id string) bool {
2020-09-08 04:44:09 +00:00
s, err := discord.ParseSnowflake(id)
if err != nil {
return false
}
2020-10-07 01:53:15 +00:00
m, err := ed.State.Store.Message(ed.ID, discord.MessageID(s))
2020-09-08 04:44:09 +00:00
if err != nil {
return false
}
2020-10-07 01:53:15 +00:00
return m.Author.ID == ed.State.UserID
2020-09-08 04:44:09 +00:00
}
2020-10-09 17:33:16 +00:00
// RawContent returns the raw message content from Discord.
func (ed Editor) RawContent(id string) (string, error) {
2020-09-08 04:44:09 +00:00
s, err := discord.ParseSnowflake(id)
if err != nil {
return "", errors.Wrap(err, "Failed to parse ID")
}
2020-10-07 01:53:15 +00:00
m, err := ed.State.Store.Message(ed.ID, discord.MessageID(s))
2020-09-08 04:44:09 +00:00
if err != nil {
return "", errors.Wrap(err, "Failed to get the message")
}
return m.Content, nil
}
2020-10-09 17:33:16 +00:00
// Edit edits the message to the given content string.
func (ed Editor) Edit(id, content string) error {
2020-09-08 04:44:09 +00:00
s, err := discord.ParseSnowflake(id)
if err != nil {
return errors.Wrap(err, "Failed to parse ID")
}
2020-10-07 01:53:15 +00:00
_, err = ed.State.EditText(ed.ID, discord.MessageID(s), content)
2020-09-08 04:44:09 +00:00
return err
}