2020-01-02 05:39:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2020-01-02 19:55:45 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
|
|
|
"github.com/diamondburned/arikawa/httputil"
|
2020-01-02 05:39:52 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
func (c *Client) Messages(channelID discord.Snowflake,
|
|
|
|
limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
return c.messages(channelID, limit, nil)
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
func (c *Client) MessagesAround(channelID, around discord.Snowflake,
|
|
|
|
limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
return c.messages(channelID, limit, map[string]interface{}{
|
|
|
|
"around": around,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
func (c *Client) MessagesBefore(channelID, before discord.Snowflake,
|
|
|
|
limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
return c.messages(channelID, limit, map[string]interface{}{
|
|
|
|
"before": before,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
func (c *Client) MessagesAfter(channelID, after discord.Snowflake,
|
|
|
|
limit uint) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
return c.messages(channelID, limit, map[string]interface{}{
|
|
|
|
"after": after,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) messages(channelID discord.Snowflake,
|
2020-01-02 19:53:08 +00:00
|
|
|
limit uint, body map[string]interface{}) ([]discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
body = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case limit == 0:
|
|
|
|
limit = 50
|
|
|
|
case limit > 100:
|
|
|
|
limit = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
body["limit"] = limit
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
var msgs []discord.Message
|
2020-01-02 05:39:52 +00:00
|
|
|
return msgs, c.RequestJSON(&msgs, "GET",
|
2020-01-06 03:48:39 +00:00
|
|
|
EndpointChannels+channelID.String(), httputil.WithSchema(c, body))
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SendMessage(channelID discord.Snowflake,
|
2020-01-02 19:53:08 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SendMessageComplex(channelID discord.Snowflake,
|
2020-01-02 19:53:08 +00:00
|
|
|
data SendMessageData) (*discord.Message, error) {
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
if data.Embed != nil {
|
|
|
|
if err := data.Embed.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Embed error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var URL = EndpointChannels + channelID.String()
|
2020-01-02 19:53:08 +00:00
|
|
|
var msg *discord.Message
|
2020-01-02 05:39:52 +00:00
|
|
|
|
|
|
|
if len(data.Files) == 0 {
|
|
|
|
// No files, no need for streaming
|
|
|
|
return msg, c.RequestJSON(&msg, "POST", URL,
|
|
|
|
httputil.WithJSONBody(c, data))
|
|
|
|
}
|
|
|
|
|
|
|
|
writer := func(w io.Writer) error {
|
|
|
|
return data.WriteMultipart(c, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.MeanwhileBody(writer, "POST", URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return msg, c.DecodeStream(resp.Body, &msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) EditMessage(channelID, messageID discord.Snowflake,
|
2020-01-02 19:53:08 +00:00
|
|
|
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.
|
|
|
|
func (c *Client) DeleteMessages(channelID discord.Snowflake,
|
|
|
|
messageIDs []discord.Snowflake) error {
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|