2020-01-02 05:39:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-01-02 19:55:45 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
2020-01-15 18:32:54 +00:00
|
|
|
"github.com/diamondburned/arikawa/internal/httputil"
|
2020-01-02 05:39:52 +00:00
|
|
|
)
|
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
// Messages gets all mesesages, automatically paginating. Use with care, as
|
|
|
|
// this could get as many as hundred thousands of messages, making a lot of
|
|
|
|
// queries.
|
|
|
|
func (c *Client) Messages(
|
2020-01-07 06:45:29 +00:00
|
|
|
channelID discord.Snowflake, max uint) ([]discord.Message, error) {
|
2020-01-06 05:22:26 +00:00
|
|
|
|
|
|
|
var msgs []discord.Message
|
|
|
|
var after discord.Snowflake = 0
|
|
|
|
|
2020-01-07 06:45:29 +00:00
|
|
|
const hardLimit int = 100
|
|
|
|
|
|
|
|
for fetch := uint(hardLimit); max > 0; fetch = uint(hardLimit) {
|
2020-01-18 19:34:08 +00:00
|
|
|
if max > 0 {
|
|
|
|
if fetch > max {
|
|
|
|
fetch = max
|
|
|
|
}
|
|
|
|
max -= fetch
|
2020-01-07 06:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err := c.messagesRange(channelID, 0, after, 0, fetch)
|
2020-01-06 05:22:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return msgs, err
|
|
|
|
}
|
|
|
|
msgs = append(msgs, m...)
|
|
|
|
|
2020-01-07 06:45:29 +00:00
|
|
|
if len(m) < hardLimit {
|
2020-01-06 05:22:26 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-01-07 06:45:29 +00:00
|
|
|
after = m[hardLimit-1].Author.ID
|
2020-01-06 05:22:26 +00:00
|
|
|
}
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
return msgs, nil
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
// MessagesAround returns messages around the ID, with a limit of 1-100.
|
2020-01-06 05:32:25 +00:00
|
|
|
func (c *Client) MessagesAround(
|
|
|
|
channelID, around discord.Snowflake,
|
2020-01-02 19:53:08 +00:00
|
|
|
limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
return c.messagesRange(channelID, 0, 0, around, limit)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
// MessagesBefore returns messages before the ID, with a limit of 1-100.
|
2020-01-06 05:32:25 +00:00
|
|
|
func (c *Client) MessagesBefore(
|
|
|
|
channelID, before discord.Snowflake,
|
2020-01-02 19:53:08 +00:00
|
|
|
limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
return c.messagesRange(channelID, before, 0, 0, limit)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
// MessagesAfter returns messages after the ID, with a limit of 1-100.
|
2020-01-06 05:32:25 +00:00
|
|
|
func (c *Client) MessagesAfter(
|
|
|
|
channelID, after discord.Snowflake,
|
2020-01-02 19:53:08 +00:00
|
|
|
limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
return c.messagesRange(channelID, 0, after, 0, limit)
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 05:22:26 +00:00
|
|
|
func (c *Client) messagesRange(channelID, before, after,
|
|
|
|
around discord.Snowflake, 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 {
|
|
|
|
Before discord.Snowflake `schema:"before,omitempty"`
|
|
|
|
After discord.Snowflake `schema:"after,omitempty"`
|
|
|
|
Around discord.Snowflake `schema:"around,omitempty"`
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Message(
|
2020-01-02 19:53:08 +00:00
|
|
|
channelID, messageID discord.Snowflake) (*discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2020-01-06 05:32:25 +00:00
|
|
|
func (c *Client) SendMessage(
|
|
|
|
channelID discord.Snowflake, content string,
|
|
|
|
embed *discord.Embed) (*discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
return c.SendMessageComplex(channelID, SendMessageData{
|
|
|
|
Content: content,
|
|
|
|
Embed: embed,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-06 05:32:25 +00:00
|
|
|
func (c *Client) EditMessage(
|
|
|
|
channelID, messageID discord.Snowflake, content string,
|
|
|
|
embed *discord.Embed, suppressEmbeds bool) (*discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
var param struct {
|
2020-01-02 19:53:08 +00:00
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
Embed *discord.Embed `json:"embed,omitempty"`
|
|
|
|
Flags discord.MessageFlags `json:"flags,omitempty"`
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
param.Content = content
|
|
|
|
param.Embed = embed
|
|
|
|
if suppressEmbeds {
|
2020-01-02 19:53:08 +00:00
|
|
|
param.Flags = discord.SuppressEmbeds
|
2020-01-02 05:39:52 +00:00
|
|
|
}
|
|
|
|
|
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, "PATCH",
|
|
|
|
EndpointChannels+channelID.String()+"/messages/"+messageID.String(),
|
|
|
|
httputil.WithJSONBody(c, param),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMessage deletes a message. Requires MANAGE_MESSAGES if the message is
|
|
|
|
// not made by yourself.
|
|
|
|
func (c *Client) DeleteMessage(channelID, messageID discord.Snowflake) error {
|
|
|
|
return c.FastRequest("DELETE", EndpointChannels+channelID.String()+
|
|
|
|
"/messages/"+messageID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteMessages only works for bots. It can't delete messages older than 2
|
|
|
|
// weeks, and will fail if tried. This endpoint requires MANAGE_MESSAGES.
|
2020-01-06 05:32:25 +00:00
|
|
|
func (c *Client) DeleteMessages(
|
|
|
|
channelID discord.Snowflake, messageIDs []discord.Snowflake) error {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
var param struct {
|
|
|
|
Messages []discord.Snowflake `json:"messages"`
|
|
|
|
}
|
|
|
|
|
|
|
|
param.Messages = messageIDs
|
|
|
|
|
|
|
|
return c.FastRequest("POST", EndpointChannels+channelID.String()+
|
|
|
|
"/messages/bulk-delete", httputil.WithJSONBody(c, param))
|
|
|
|
}
|