2020-01-02 05:39:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-06-18 06:32:11 +00:00
|
|
|
"mime/multipart"
|
|
|
|
"strconv"
|
|
|
|
|
2020-05-24 23:56:41 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/internal/intmath"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/httputil"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
2021-06-18 06:32:11 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/sendpart"
|
2020-01-02 05:39:52 +00:00
|
|
|
)
|
|
|
|
|
2020-11-10 23:34:05 +00:00
|
|
|
const (
|
|
|
|
// the limit of max messages per request, as imposed by Discord
|
|
|
|
maxMessageFetchLimit = 100
|
|
|
|
// maxMessageDeleteLimit is the limit of max message that can be deleted
|
|
|
|
// per bulk delete request, as imposed by Discord.
|
|
|
|
maxMessageDeleteLimit = 100
|
|
|
|
)
|
2020-10-19 05:14:06 +00:00
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
// Messages returns a slice filled with the most recent messages sent in the
|
|
|
|
// channel with the passed ID. The method automatically paginates until it
|
|
|
|
// reaches the passed limit, or, if the limit is set to 0, has fetched all
|
|
|
|
// messages in the channel.
|
2020-05-11 22:06:19 +00:00
|
|
|
//
|
2020-10-19 14:47:43 +00:00
|
|
|
// As the underlying endpoint is capped at a maximum of 100 messages per
|
|
|
|
// request, at maximum a total of limit/100 rounded up requests will be made,
|
|
|
|
// although they may be less, if no more messages are available.
|
2020-05-16 23:35:57 +00:00
|
|
|
//
|
2020-10-19 14:47:43 +00:00
|
|
|
// When fetching the messages, those with the highest ID, will be fetched
|
2020-05-16 23:35:57 +00:00
|
|
|
// first.
|
2020-10-19 14:47:43 +00:00
|
|
|
// The returned slice will be sorted from latest to oldest.
|
2020-07-21 20:27:59 +00:00
|
|
|
func (c *Client) Messages(channelID discord.ChannelID, limit uint) ([]discord.Message, error) {
|
2020-10-19 14:47:43 +00:00
|
|
|
// Since before is 0 it will be omitted by the http lib, which in turn
|
|
|
|
// will lead discord to send us the most recent messages without having to
|
|
|
|
// specify a Snowflake.
|
|
|
|
return c.MessagesBefore(channelID, 0, limit)
|
2020-05-16 23:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MessagesAround returns messages around the ID, with a limit of 100.
|
|
|
|
func (c *Client) MessagesAround(
|
2020-07-21 20:27:59 +00:00
|
|
|
channelID discord.ChannelID, around discord.MessageID, limit uint) ([]discord.Message, error) {
|
2020-05-16 23:35:57 +00:00
|
|
|
|
|
|
|
return c.messagesRange(channelID, 0, 0, around, limit)
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
// MessagesBefore returns a slice filled with the messages sent in the channel
|
|
|
|
// with the passed id. The method automatically paginates until it reaches the
|
|
|
|
// passed limit, or, if the limit is set to 0, has fetched all messages in the
|
|
|
|
// channel with an id smaller than before.
|
2020-05-16 23:35:57 +00:00
|
|
|
//
|
|
|
|
// As the underlying endpoint has a maximum of 100 messages per request, at
|
|
|
|
// maximum a total of limit/100 rounded up requests will be made, although they
|
|
|
|
// may be less, if no more messages are available.
|
2020-10-19 14:47:43 +00:00
|
|
|
//
|
|
|
|
// The returned slice will be sorted from latest to oldest.
|
2020-05-16 23:35:57 +00:00
|
|
|
func (c *Client) MessagesBefore(
|
2020-07-21 20:27:59 +00:00
|
|
|
channelID discord.ChannelID, before discord.MessageID, limit uint) ([]discord.Message, error) {
|
2020-05-16 23:35:57 +00:00
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
msgs := make([]discord.Message, 0, limit)
|
2020-01-06 05:22:26 +00:00
|
|
|
|
2020-10-19 05:14:06 +00:00
|
|
|
fetch := uint(maxMessageFetchLimit)
|
2020-05-11 22:06:19 +00:00
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
// Check if we are truly fetching unlimited messages to avoid confusion
|
|
|
|
// later on, if the limit reaches 0.
|
|
|
|
unlimited := limit == 0
|
|
|
|
|
|
|
|
for limit > 0 || unlimited {
|
2020-05-16 23:35:57 +00:00
|
|
|
if limit > 0 {
|
2020-10-19 05:14:06 +00:00
|
|
|
// Only fetch as much as we need. Since limit gradually decreases,
|
2020-12-27 00:08:41 +00:00
|
|
|
// we only need to fetch intmath.Min(fetch, limit).
|
|
|
|
fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit)))
|
2020-10-19 05:14:06 +00:00
|
|
|
limit -= maxMessageFetchLimit
|
2020-01-07 06:45:29 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 23:35:57 +00:00
|
|
|
m, err := c.messagesRange(channelID, before, 0, 0, fetch)
|
2020-01-06 05:22:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return msgs, err
|
|
|
|
}
|
2020-10-19 05:14:06 +00:00
|
|
|
// Append the older messages into the list of newer messages.
|
|
|
|
msgs = append(msgs, m...)
|
2020-01-06 05:22:26 +00:00
|
|
|
|
2020-10-19 05:14:06 +00:00
|
|
|
if len(m) < maxMessageFetchLimit {
|
2020-01-06 05:22:26 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-10-19 05:14:06 +00:00
|
|
|
before = m[len(m)-1].ID
|
2020-01-06 05:22:26 +00:00
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
if len(msgs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
return msgs, nil
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
// MessagesAfter returns a slice filled with the messages sent in the channel
|
|
|
|
// with the passed ID. The method automatically paginates until it reaches the
|
|
|
|
// passed limit, or, if the limit is set to 0, has fetched all messages in the
|
|
|
|
// channel with an id higher than after.
|
2020-05-16 23:35:57 +00:00
|
|
|
//
|
|
|
|
// As the underlying endpoint has a maximum of 100 messages per request, at
|
|
|
|
// maximum a total of limit/100 rounded up requests will be made, although they
|
|
|
|
// may be less, if no more messages are available.
|
2020-10-19 14:47:43 +00:00
|
|
|
//
|
|
|
|
// The returned slice will be sorted from latest to oldest.
|
2020-05-16 23:35:57 +00:00
|
|
|
func (c *Client) MessagesAfter(
|
2020-07-21 20:27:59 +00:00
|
|
|
channelID discord.ChannelID, after discord.MessageID, limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
// 0 is uint's zero value and will lead to the after param getting omitted,
|
|
|
|
// which in turn will lead to the most recent messages being returned.
|
|
|
|
// Setting this to 1 will prevent that.
|
|
|
|
if after == 0 {
|
|
|
|
after = 1
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:35:57 +00:00
|
|
|
var msgs []discord.Message
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-10-19 05:14:06 +00:00
|
|
|
fetch := uint(maxMessageFetchLimit)
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
// Check if we are truly fetching unlimited messages to avoid confusion
|
|
|
|
// later on, if the limit reaches 0.
|
|
|
|
unlimited := limit == 0
|
|
|
|
|
|
|
|
for limit > 0 || unlimited {
|
2020-05-16 23:35:57 +00:00
|
|
|
if limit > 0 {
|
2020-10-19 05:14:06 +00:00
|
|
|
// Only fetch as much as we need. Since limit gradually decreases,
|
2020-12-27 00:08:41 +00:00
|
|
|
// we only need to fetch intmath.Min(fetch, limit).
|
|
|
|
fetch = uint(intmath.Min(maxMessageFetchLimit, int(limit)))
|
2020-10-19 05:14:06 +00:00
|
|
|
limit -= maxMessageFetchLimit
|
2020-05-16 23:35:57 +00:00
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-05-16 23:35:57 +00:00
|
|
|
m, err := c.messagesRange(channelID, 0, after, 0, fetch)
|
|
|
|
if err != nil {
|
|
|
|
return msgs, err
|
|
|
|
}
|
2020-10-19 05:14:06 +00:00
|
|
|
// Prepend the older messages into the newly-fetched messages list.
|
|
|
|
msgs = append(m, msgs...)
|
2020-05-16 23:35:57 +00:00
|
|
|
|
2020-10-19 05:14:06 +00:00
|
|
|
if len(m) < maxMessageFetchLimit {
|
2020-05-16 23:35:57 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-10-19 05:14:06 +00:00
|
|
|
after = m[0].ID
|
2020-05-16 23:35:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:47:43 +00:00
|
|
|
if len(msgs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:35:57 +00:00
|
|
|
return msgs, nil
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 23:35:37 +00:00
|
|
|
func (c *Client) messagesRange(
|
2021-08-15 16:33:33 +00:00
|
|
|
channelID discord.ChannelID,
|
|
|
|
before, after, around discord.MessageID, limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case limit == 0:
|
|
|
|
limit = 50
|
|
|
|
case limit > 100:
|
|
|
|
limit = 100
|
|
|
|
}
|
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
var param struct {
|
2020-07-21 20:27:59 +00:00
|
|
|
Before discord.MessageID `schema:"before,omitempty"`
|
|
|
|
After discord.MessageID `schema:"after,omitempty"`
|
|
|
|
Around discord.MessageID `schema:"around,omitempty"`
|
2020-01-06 05:22:26 +00:00
|
|
|
|
|
|
|
Limit uint `schema:"limit"`
|
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-01-16 04:27:57 +00:00
|
|
|
param.Before = before
|
|
|
|
param.After = after
|
|
|
|
param.Around = around
|
|
|
|
param.Limit = limit
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
var msgs []discord.Message
|
2020-01-20 07:21:57 +00:00
|
|
|
return msgs, c.RequestJSON(
|
|
|
|
&msgs, "GET",
|
|
|
|
EndpointChannels+channelID.String()+"/messages",
|
|
|
|
httputil.WithSchema(c, param),
|
|
|
|
)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:06:19 +00:00
|
|
|
// Message returns a specific message in the channel.
|
|
|
|
//
|
|
|
|
// If operating on a guild channel, this endpoint requires the
|
|
|
|
// READ_MESSAGE_HISTORY permission to be present on the current user.
|
2021-08-15 16:33:33 +00:00
|
|
|
func (c *Client) Message(
|
|
|
|
channelID discord.ChannelID, messageID discord.MessageID) (*discord.Message, error) {
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
var msg *discord.Message
|
2020-01-02 05:39:52 +00:00
|
|
|
return msg, c.RequestJSON(&msg, "GET",
|
|
|
|
EndpointChannels+channelID.String()+"/messages/"+messageID.String())
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:29:41 +00:00
|
|
|
// SendTextReply posts a text-only reply to a message ID in a guild text or DM channel
|
|
|
|
//
|
|
|
|
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
|
|
|
|
// permission to be present on the current user.
|
|
|
|
//
|
|
|
|
// Fires a Message Create Gateway event.
|
|
|
|
func (c *Client) SendTextReply(
|
2021-04-07 18:38:26 +00:00
|
|
|
channelID discord.ChannelID,
|
2021-08-15 16:33:33 +00:00
|
|
|
content string, referenceID discord.MessageID) (*discord.Message, error) {
|
2021-04-07 18:38:26 +00:00
|
|
|
|
2021-02-14 20:29:41 +00:00
|
|
|
return c.SendMessageComplex(channelID, SendMessageData{
|
|
|
|
Content: content,
|
|
|
|
Reference: &discord.MessageReference{MessageID: referenceID},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
// SendEmbeds sends embeds to a guild text or DM channel.
|
2020-05-12 03:21:48 +00:00
|
|
|
//
|
|
|
|
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
|
|
|
|
// permission to be present on the current user.
|
|
|
|
//
|
|
|
|
// Fires a Message Create Gateway event.
|
2021-06-18 06:32:11 +00:00
|
|
|
func (c *Client) SendEmbeds(
|
|
|
|
channelID discord.ChannelID, e ...discord.Embed) (*discord.Message, error) {
|
2020-05-12 03:21:48 +00:00
|
|
|
|
|
|
|
return c.SendMessageComplex(channelID, SendMessageData{
|
2021-06-18 06:32:11 +00:00
|
|
|
Embeds: e,
|
2020-05-12 03:21:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:29:41 +00:00
|
|
|
// SendEmbedReply posts an Embed reply to a message ID in a guild text or DM channel.
|
|
|
|
//
|
|
|
|
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
|
|
|
|
// permission to be present on the current user.
|
|
|
|
//
|
|
|
|
// Fires a Message Create Gateway event.
|
|
|
|
func (c *Client) SendEmbedReply(
|
2021-04-07 18:38:26 +00:00
|
|
|
channelID discord.ChannelID,
|
2021-08-16 09:25:37 +00:00
|
|
|
referenceID discord.MessageID, embeds ...discord.Embed) (*discord.Message, error) {
|
2021-02-14 20:29:41 +00:00
|
|
|
|
|
|
|
return c.SendMessageComplex(channelID, SendMessageData{
|
2021-08-16 09:25:37 +00:00
|
|
|
Embeds: embeds,
|
2021-02-14 20:29:41 +00:00
|
|
|
Reference: &discord.MessageReference{MessageID: referenceID},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:06:19 +00:00
|
|
|
// SendMessage posts a message to a guild text or DM channel.
|
|
|
|
//
|
|
|
|
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
|
|
|
|
// permission to be present on the current user.
|
|
|
|
//
|
|
|
|
// Fires a Message Create Gateway event.
|
2020-01-06 05:32:25 +00:00
|
|
|
func (c *Client) SendMessage(
|
2021-08-15 16:33:33 +00:00
|
|
|
channelID discord.ChannelID,
|
|
|
|
content string, embeds ...discord.Embed) (*discord.Message, error) {
|
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
data := SendMessageData{
|
2020-01-02 05:39:52 +00:00
|
|
|
Content: content,
|
2021-06-18 06:32:11 +00:00
|
|
|
Embeds: embeds,
|
|
|
|
}
|
|
|
|
return c.SendMessageComplex(channelID, data)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 20:29:41 +00:00
|
|
|
// SendMessageReply posts a reply to a message ID in a guild text or DM channel.
|
|
|
|
//
|
|
|
|
// If operating on a guild channel, this endpoint requires the SEND_MESSAGES
|
|
|
|
// permission to be present on the current user.
|
|
|
|
//
|
|
|
|
// Fires a Message Create Gateway event.
|
|
|
|
func (c *Client) SendMessageReply(
|
2021-08-15 16:33:33 +00:00
|
|
|
channelID discord.ChannelID, content string,
|
2021-08-16 09:25:37 +00:00
|
|
|
referenceID discord.MessageID, embeds ...discord.Embed) (*discord.Message, error) {
|
2021-08-15 16:33:33 +00:00
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
data := SendMessageData{
|
2021-02-14 20:29:41 +00:00
|
|
|
Content: content,
|
|
|
|
Reference: &discord.MessageReference{MessageID: referenceID},
|
2021-08-16 09:25:37 +00:00
|
|
|
Embeds: embeds,
|
2021-06-18 06:32:11 +00:00
|
|
|
}
|
2021-08-16 09:25:37 +00:00
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
return c.SendMessageComplex(channelID, data)
|
2021-02-14 20:29:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
// https://discord.com/developers/docs/resources/channel#edit-message
|
2020-05-11 22:06:19 +00:00
|
|
|
type EditMessageData struct {
|
|
|
|
// Content is the new message contents (up to 2000 characters).
|
|
|
|
Content option.NullableString `json:"content,omitempty"`
|
2021-06-18 06:32:11 +00:00
|
|
|
// Embeds contains embedded rich content.
|
|
|
|
Embeds *[]discord.Embed `json:"embeds,omitempty"`
|
2021-05-30 04:28:37 +00:00
|
|
|
// Components contains the new components to attach.
|
2021-10-10 22:44:31 +00:00
|
|
|
Components *discord.ContainerComponents `json:"components,omitempty"`
|
2020-05-15 17:14:37 +00:00
|
|
|
// AllowedMentions are the allowed mentions for a message.
|
2020-05-11 22:06:19 +00:00
|
|
|
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"`
|
2021-06-18 06:32:11 +00:00
|
|
|
// Attachments are the attached files to keep
|
|
|
|
Attachments *[]discord.Attachment `json:"attachments,omitempty"`
|
2020-05-11 22:06:19 +00:00
|
|
|
// Flags edits the flags of a message (only SUPPRESS_EMBEDS can currently
|
|
|
|
// be set/unset)
|
|
|
|
//
|
|
|
|
// This field is nullable.
|
|
|
|
Flags *discord.MessageFlags `json:"flags,omitempty"`
|
2021-06-18 06:32:11 +00:00
|
|
|
|
|
|
|
Files []sendpart.File `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeedsMultipart returns true if the SendMessageData has files.
|
|
|
|
func (data EditMessageData) NeedsMultipart() bool {
|
|
|
|
return len(data.Files) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (data EditMessageData) WriteMultipart(body *multipart.Writer) error {
|
|
|
|
return sendpart.Write(body, data, data.Files)
|
2020-05-11 22:06:19 +00:00
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-05-15 17:17:52 +00:00
|
|
|
// EditText edits the contents of a previously sent message. For more
|
|
|
|
// documentation, refer to EditMessageComplex.
|
|
|
|
func (c *Client) EditText(
|
2021-08-15 16:33:33 +00:00
|
|
|
channelID discord.ChannelID,
|
|
|
|
messageID discord.MessageID, content string) (*discord.Message, error) {
|
2020-05-15 17:17:52 +00:00
|
|
|
|
|
|
|
return c.EditMessageComplex(channelID, messageID, EditMessageData{
|
|
|
|
Content: option.NewNullableString(content),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
// EditEmbeds edits the embed of a previously sent message. For more
|
2020-05-15 17:17:52 +00:00
|
|
|
// documentation, refer to EditMessageComplex.
|
2021-06-18 06:32:11 +00:00
|
|
|
func (c *Client) EditEmbeds(
|
2021-08-15 16:33:33 +00:00
|
|
|
channelID discord.ChannelID,
|
|
|
|
messageID discord.MessageID, embeds ...discord.Embed) (*discord.Message, error) {
|
2020-05-15 17:17:52 +00:00
|
|
|
|
|
|
|
return c.EditMessageComplex(channelID, messageID, EditMessageData{
|
2021-06-18 06:32:11 +00:00
|
|
|
Embeds: &embeds,
|
2020-05-15 17:17:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:25:37 +00:00
|
|
|
// EditMessage edits a previously sent message. If content or embeds are empty
|
|
|
|
// the original content or embed will remain untouched. This means EditMessage
|
|
|
|
// will only update, but not remove parts of the message.
|
|
|
|
//
|
|
|
|
// For more documentation, refer to EditMessageComplex.
|
2020-05-12 23:51:35 +00:00
|
|
|
func (c *Client) EditMessage(
|
2021-08-16 09:25:37 +00:00
|
|
|
channelID discord.ChannelID, messageID discord.MessageID,
|
|
|
|
content string, embeds ...discord.Embed) (*discord.Message, error) {
|
2020-05-12 23:51:35 +00:00
|
|
|
|
2021-08-16 09:25:37 +00:00
|
|
|
var data EditMessageData
|
|
|
|
|
|
|
|
if len(content) > 0 {
|
|
|
|
data.Content = option.NewNullableString(content)
|
2020-05-12 23:51:35 +00:00
|
|
|
}
|
2021-08-16 09:25:37 +00:00
|
|
|
|
|
|
|
if len(embeds) > 0 {
|
|
|
|
data.Embeds = &embeds
|
2020-05-12 23:51:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.EditMessageComplex(channelID, messageID, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditMessageComplex edits a previously sent message. The fields Content,
|
|
|
|
// Embed, AllowedMentions and Flags can be edited by the original message
|
|
|
|
// author. Other users can only edit flags and only if they have the
|
|
|
|
// MANAGE_MESSAGES permission in the corresponding channel. When specifying
|
|
|
|
// flags, ensure to include all previously set flags/bits in addition to ones
|
|
|
|
// that you are modifying. Only flags documented in EditMessageData may be
|
|
|
|
// modified by users (unsupported flag changes are currently ignored without
|
|
|
|
// error).
|
2020-05-11 22:06:19 +00:00
|
|
|
//
|
|
|
|
// Fires a Message Update Gateway event.
|
2020-05-12 23:51:35 +00:00
|
|
|
func (c *Client) EditMessageComplex(
|
2021-08-15 16:33:33 +00:00
|
|
|
channelID discord.ChannelID,
|
|
|
|
messageID discord.MessageID, data EditMessageData) (*discord.Message, error) {
|
|
|
|
|
2020-05-24 23:56:41 +00:00
|
|
|
if data.AllowedMentions != nil {
|
|
|
|
if err := data.AllowedMentions.Verify(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "allowedMentions error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
if data.Embeds != nil {
|
|
|
|
sum := 0
|
|
|
|
for i, embed := range *data.Embeds {
|
|
|
|
if err := embed.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "embed error at "+strconv.Itoa(i))
|
|
|
|
}
|
|
|
|
sum += embed.Length()
|
|
|
|
if sum > 6000 {
|
2021-08-24 18:23:49 +00:00
|
|
|
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of all text in embeds"}
|
2021-06-18 06:32:11 +00:00
|
|
|
}
|
2021-08-24 18:23:49 +00:00
|
|
|
|
|
|
|
(*data.Embeds)[i] = embed // embed.Validate changes fields
|
2020-05-24 23:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-24 18:23:49 +00:00
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
var msg *discord.Message
|
2021-06-18 06:32:11 +00:00
|
|
|
return msg, sendpart.PATCH(c.Client, data, &msg,
|
|
|
|
EndpointChannels+channelID.String()+"/messages/"+messageID.String())
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 22:12:32 +00:00
|
|
|
// CrosspostMessage crossposts a message in a news channel to following channels.
|
|
|
|
// This endpoint requires the SEND_MESSAGES permission if the current user sent the message,
|
|
|
|
// or additionally the MANAGE_MESSAGES permission for all other messages.
|
2021-08-15 16:33:33 +00:00
|
|
|
func (c *Client) CrosspostMessage(
|
|
|
|
channelID discord.ChannelID, messageID discord.MessageID) (*discord.Message, error) {
|
|
|
|
|
2021-04-06 22:12:32 +00:00
|
|
|
var msg *discord.Message
|
|
|
|
|
|
|
|
return msg, c.RequestJSON(
|
|
|
|
&msg,
|
|
|
|
"POST",
|
|
|
|
EndpointChannels+channelID.String()+"/messages/"+messageID.String()+"/crosspost",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-11 22:06:19 +00:00
|
|
|
// DeleteMessage delete a message. If operating on a guild channel and trying
|
|
|
|
// to delete a message that was not sent by the current user, this endpoint
|
|
|
|
// requires the MANAGE_MESSAGES permission.
|
2021-08-15 16:33:33 +00:00
|
|
|
func (c *Client) DeleteMessage(
|
|
|
|
channelID discord.ChannelID, messageID discord.MessageID, reason AuditLogReason) error {
|
|
|
|
|
|
|
|
return c.FastRequest(
|
|
|
|
"DELETE", EndpointChannels+channelID.String()+"/messages/"+messageID.String(),
|
|
|
|
httputil.WithHeaders(reason.Header()))
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:06:19 +00:00
|
|
|
// DeleteMessages deletes multiple messages in a single request. This endpoint
|
|
|
|
// can only be used on guild channels and requires the MANAGE_MESSAGES
|
|
|
|
// permission. This endpoint only works for bots.
|
|
|
|
//
|
|
|
|
// This endpoint will not delete messages older than 2 weeks, and will fail if
|
|
|
|
// any message provided is older than that or if any duplicate message IDs are
|
|
|
|
// provided.
|
|
|
|
//
|
2020-11-10 23:34:05 +00:00
|
|
|
// Because the underlying endpoint only supports a maximum of 100 message IDs
|
|
|
|
// per request, DeleteMessages will make a total of messageIDs/100 rounded up
|
|
|
|
// requests.
|
|
|
|
//
|
2020-05-11 22:06:19 +00:00
|
|
|
// Fires a Message Delete Bulk Gateway event.
|
2021-08-15 16:33:33 +00:00
|
|
|
func (c *Client) DeleteMessages(
|
|
|
|
channelID discord.ChannelID, messageIDs []discord.MessageID, reason AuditLogReason) error {
|
|
|
|
|
2020-11-10 23:34:05 +00:00
|
|
|
switch {
|
|
|
|
case len(messageIDs) == 0:
|
|
|
|
return nil
|
|
|
|
case len(messageIDs) == 1:
|
2021-08-15 16:33:33 +00:00
|
|
|
return c.DeleteMessage(channelID, messageIDs[0], reason)
|
2020-11-10 23:34:05 +00:00
|
|
|
case len(messageIDs) <= maxMessageDeleteLimit: // Fast path
|
2021-08-15 16:33:33 +00:00
|
|
|
return c.deleteMessages(channelID, messageIDs, reason)
|
2020-11-10 23:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 19:07:30 +00:00
|
|
|
// If the number of messages to be deleted exceeds the amount discord is willing
|
|
|
|
// to accept at one time then batches of messages will be deleted
|
2020-11-10 23:34:05 +00:00
|
|
|
for start := 0; start < len(messageIDs); start += maxMessageDeleteLimit {
|
2020-12-27 00:08:41 +00:00
|
|
|
end := intmath.Min(len(messageIDs), start+maxMessageDeleteLimit)
|
2021-08-15 16:33:33 +00:00
|
|
|
err := c.deleteMessages(channelID, messageIDs[start:end], reason)
|
2020-11-10 23:34:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:33:33 +00:00
|
|
|
func (c *Client) deleteMessages(
|
|
|
|
channelID discord.ChannelID, messageIDs []discord.MessageID, reason AuditLogReason) error {
|
|
|
|
|
2020-01-02 05:39:52 +00:00
|
|
|
var param struct {
|
2020-07-21 20:27:59 +00:00
|
|
|
Messages []discord.MessageID `json:"messages"`
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
param.Messages = messageIDs
|
|
|
|
|
2020-05-12 02:05:08 +00:00
|
|
|
return c.FastRequest(
|
|
|
|
"POST",
|
|
|
|
EndpointChannels+channelID.String()+"/messages/bulk-delete",
|
2021-08-15 16:33:33 +00:00
|
|
|
httputil.WithJSONBody(param), httputil.WithHeaders(reason.Header()),
|
2020-05-12 02:05:08 +00:00
|
|
|
)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|