2020-07-29 19:29:30 +00:00
|
|
|
// Package webhook provides means to interact with webhooks directly and not
|
|
|
|
// through the bot API.
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2021-03-06 04:01:33 +00:00
|
|
|
"context"
|
2020-12-16 21:11:11 +00:00
|
|
|
"mime/multipart"
|
2020-07-29 19:29:30 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
|
|
|
"github.com/diamondburned/arikawa/v3/api/rate"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/httputil"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/httputil/httpdriver"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/sendpart"
|
2020-07-29 19:29:30 +00:00
|
|
|
)
|
|
|
|
|
2021-03-06 04:01:33 +00:00
|
|
|
// TODO: if there's ever an Arikawa v3, then a new Client abstraction could be
|
|
|
|
// made that wraps around Session being an interface. Just a food for thought.
|
|
|
|
|
|
|
|
// Session keeps a single webhook session. It is referenced by other webhook
|
|
|
|
// clients using the same session.
|
|
|
|
type Session struct {
|
|
|
|
// Limiter is the rate limiter used for the client. This field should not be
|
|
|
|
// changed, as doing so is potentially racy.
|
|
|
|
Limiter *rate.Limiter
|
|
|
|
|
|
|
|
// ID is the ID of the webhook.
|
|
|
|
ID discord.WebhookID
|
|
|
|
// Token is the token of the webhook.
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnRequest should be called on each client request to inject itself.
|
|
|
|
func (s *Session) OnRequest(r httpdriver.Request) error {
|
|
|
|
return s.Limiter.Acquire(r.GetContext(), r.GetPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnResponse should be called after each client request to clean itself up.
|
|
|
|
func (s *Session) OnResponse(r httpdriver.Request, resp httpdriver.Response) error {
|
|
|
|
return s.Limiter.Release(r.GetPath(), httpdriver.OptHeader(resp))
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:29:30 +00:00
|
|
|
// Client is the client used to interact with a webhook.
|
|
|
|
type Client struct {
|
|
|
|
// Client is the httputil.Client used to call Discord's API.
|
|
|
|
*httputil.Client
|
2021-03-06 04:01:33 +00:00
|
|
|
*Session
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 04:01:33 +00:00
|
|
|
// New creates a new Client using the passed webhook token and ID. It uses its
|
|
|
|
// own rate limiter.
|
2020-12-16 21:22:26 +00:00
|
|
|
func New(id discord.WebhookID, token string) *Client {
|
|
|
|
return NewCustom(id, token, httputil.NewClient())
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 04:01:33 +00:00
|
|
|
// NewCustom creates a new webhook client using the passed webhook token, ID and
|
|
|
|
// a copy of the given httputil.Client. The copy will have a new rate limiter
|
|
|
|
// added in.
|
|
|
|
func NewCustom(id discord.WebhookID, token string, hcl *httputil.Client) *Client {
|
|
|
|
ses := Session{
|
|
|
|
Limiter: rate.NewLimiter(api.Path),
|
|
|
|
ID: id,
|
|
|
|
Token: token,
|
|
|
|
}
|
|
|
|
|
|
|
|
hcl = hcl.Copy()
|
|
|
|
hcl.OnRequest = append(hcl.OnRequest, ses.OnRequest)
|
|
|
|
hcl.OnResponse = append(hcl.OnResponse, ses.OnResponse)
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
Client: hcl,
|
|
|
|
Session: &ses,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromAPI creates a new client that shares the same internal HTTP client with
|
|
|
|
// the one in the API's. This is often useful for bots that need webhook
|
|
|
|
// interaction, since the rate limiter is shared.
|
|
|
|
func FromAPI(id discord.WebhookID, token string, c *api.Client) *Client {
|
|
|
|
return &Client{
|
|
|
|
Client: c.Client,
|
|
|
|
Session: &Session{
|
|
|
|
Limiter: c.Limiter,
|
|
|
|
ID: id,
|
|
|
|
Token: token,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContext returns a shallow copy of Client with the given context. It's
|
|
|
|
// used for method timeouts and such. This method is thread-safe.
|
|
|
|
func (c *Client) WithContext(ctx context.Context) *Client {
|
2020-07-29 19:29:30 +00:00
|
|
|
return &Client{
|
2021-03-06 04:01:33 +00:00
|
|
|
Client: c.Client.WithContext(ctx),
|
|
|
|
Session: c.Session,
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get gets the webhook.
|
|
|
|
func (c *Client) Get() (*discord.Webhook, error) {
|
|
|
|
var w *discord.Webhook
|
|
|
|
return w, c.RequestJSON(&w, "GET", api.EndpointWebhooks+c.ID.String()+"/"+c.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify modifies the webhook.
|
|
|
|
func (c *Client) Modify(data api.ModifyWebhookData) (*discord.Webhook, error) {
|
|
|
|
var w *discord.Webhook
|
|
|
|
return w, c.RequestJSON(
|
|
|
|
&w, "PATCH",
|
|
|
|
api.EndpointWebhooks+c.ID.String()+"/"+c.Token,
|
|
|
|
httputil.WithJSONBody(data),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a webhook permanently.
|
|
|
|
func (c *Client) Delete() error {
|
|
|
|
return c.FastRequest("DELETE", api.EndpointWebhooks+c.ID.String()+"/"+c.Token)
|
|
|
|
}
|
|
|
|
|
2020-12-16 21:11:11 +00:00
|
|
|
// https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params
|
2020-12-16 21:17:56 +00:00
|
|
|
type ExecuteData struct {
|
2020-12-16 21:11:11 +00:00
|
|
|
// Content are the message contents (up to 2000 characters).
|
|
|
|
//
|
|
|
|
// Required: one of content, file, embeds
|
|
|
|
Content string `json:"content,omitempty"`
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// ThreadID causes the message to be sent to the specified thread within
|
|
|
|
// the webhook's channel. The thread will automatically be unarchived.
|
|
|
|
ThreadID discord.CommandID `json:"-"`
|
|
|
|
|
2020-12-16 21:11:11 +00:00
|
|
|
// Username overrides the default username of the webhook
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
// AvatarURL overrides the default avatar of the webhook.
|
|
|
|
AvatarURL discord.URL `json:"avatar_url,omitempty"`
|
|
|
|
|
|
|
|
// TTS is true if this is a TTS message.
|
|
|
|
TTS bool `json:"tts,omitempty"`
|
|
|
|
// Embeds contains embedded rich content.
|
|
|
|
//
|
|
|
|
// Required: one of content, file, embeds
|
|
|
|
Embeds []discord.Embed `json:"embeds,omitempty"`
|
|
|
|
|
2021-06-18 06:32:11 +00:00
|
|
|
// Components is the list of components (such as buttons) to be attached to
|
|
|
|
// the message.
|
|
|
|
Components []discord.Component `json:"components,omitempty"`
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// Files represents a list of files to upload. This will not be
|
|
|
|
// JSON-encoded and will only be available through WriteMultipart.
|
2020-12-16 21:11:11 +00:00
|
|
|
Files []sendpart.File `json:"-"`
|
|
|
|
|
|
|
|
// AllowedMentions are the allowed mentions for the message.
|
|
|
|
AllowedMentions *api.AllowedMentions `json:"allowed_mentions,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeedsMultipart returns true if the ExecuteWebhookData has files.
|
2020-12-16 21:17:56 +00:00
|
|
|
func (data ExecuteData) NeedsMultipart() bool {
|
2020-12-16 21:11:11 +00:00
|
|
|
return len(data.Files) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMultipart writes the webhook data into the given multipart body. It does
|
|
|
|
// not close body.
|
2020-12-16 21:17:56 +00:00
|
|
|
func (data ExecuteData) WriteMultipart(body *multipart.Writer) error {
|
2020-12-16 21:11:11 +00:00
|
|
|
return sendpart.Write(body, data, data.Files)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:29:30 +00:00
|
|
|
// Execute sends a message to the webhook, but doesn't wait for the message to
|
|
|
|
// get created. This is generally faster, but only applicable if no further
|
|
|
|
// interaction is required.
|
2020-12-16 21:17:56 +00:00
|
|
|
func (c *Client) Execute(data ExecuteData) (err error) {
|
2020-07-29 19:29:30 +00:00
|
|
|
_, err = c.execute(data, false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteAndWait executes the webhook, and waits for the generated
|
|
|
|
// discord.Message to be returned.
|
2020-12-16 21:17:56 +00:00
|
|
|
func (c *Client) ExecuteAndWait(data ExecuteData) (*discord.Message, error) {
|
2020-07-29 19:29:30 +00:00
|
|
|
return c.execute(data, true)
|
|
|
|
}
|
|
|
|
|
2020-12-16 21:17:56 +00:00
|
|
|
func (c *Client) execute(data ExecuteData, wait bool) (*discord.Message, error) {
|
2020-07-29 19:29:30 +00:00
|
|
|
if data.Content == "" && len(data.Embeds) == 0 && len(data.Files) == 0 {
|
|
|
|
return nil, api.ErrEmptyMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
sum := 0
|
2020-07-29 19:29:30 +00:00
|
|
|
for i, embed := range data.Embeds {
|
|
|
|
if err := embed.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "embed error at "+strconv.Itoa(i))
|
|
|
|
}
|
2021-06-18 06:32:11 +00:00
|
|
|
sum += embed.Length()
|
|
|
|
if sum > 6000 {
|
|
|
|
return nil, &discord.OverboundError{sum, 6000, "sum of all text in embeds"}
|
|
|
|
}
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
param := make(url.Values, 2)
|
2020-07-29 19:29:30 +00:00
|
|
|
if wait {
|
2021-08-03 18:44:20 +00:00
|
|
|
param["wait"] = []string{"true"}
|
|
|
|
}
|
|
|
|
if data.ThreadID.IsValid() {
|
|
|
|
param["thread_id"] = []string{data.ThreadID.String()}
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var URL = api.EndpointWebhooks + c.ID.String() + "/" + c.Token + "?" + param.Encode()
|
|
|
|
|
2020-12-16 21:11:11 +00:00
|
|
|
var msg *discord.Message
|
|
|
|
var ptr interface{}
|
|
|
|
if wait {
|
|
|
|
ptr = &msg
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 21:11:11 +00:00
|
|
|
return msg, sendpart.POST(c.Client, data, ptr, URL)
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// Message returns a previously-sent webhook message from the same token.
|
|
|
|
func (c *Client) Message(messageID discord.MessageID) (*discord.Message, error) {
|
|
|
|
var m *discord.Message
|
|
|
|
return m, c.RequestJSON(
|
|
|
|
&m, "GET",
|
|
|
|
api.EndpointWebhooks+c.ID.String()+"/"+c.Token+"/messages/"+messageID.String())
|
|
|
|
}
|
2021-06-18 06:32:11 +00:00
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params
|
2020-12-16 21:17:56 +00:00
|
|
|
type EditMessageData struct {
|
2021-06-18 06:32:11 +00:00
|
|
|
// Content is the new message contents (up to 2000 characters).
|
2020-12-16 20:11:11 +00:00
|
|
|
Content option.NullableString `json:"content,omitempty"`
|
2021-06-18 06:32:11 +00:00
|
|
|
// Embeds contains embedded rich content.
|
2020-12-16 20:11:11 +00:00
|
|
|
Embeds *[]discord.Embed `json:"embeds,omitempty"`
|
2021-06-18 06:32:11 +00:00
|
|
|
// Components contains the new components to attach.
|
|
|
|
Components *[]discord.Component `json:"components,omitempty"`
|
|
|
|
// AllowedMentions are the allowed mentions for a message.
|
2020-12-16 20:11:11 +00:00
|
|
|
AllowedMentions *api.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"`
|
|
|
|
|
|
|
|
Files []sendpart.File `json:"-"`
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:11:11 +00:00
|
|
|
// EditMessage edits a previously-sent webhook message from the same webhook.
|
2021-06-18 06:32:11 +00:00
|
|
|
func (c *Client) EditMessage(messageID discord.MessageID, data EditMessageData) (*discord.Message, error) {
|
|
|
|
if data.AllowedMentions != nil {
|
|
|
|
if err := data.AllowedMentions.Verify(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "allowedMentions error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if data.Embeds != nil {
|
|
|
|
sum := 0
|
|
|
|
for _, e := range *data.Embeds {
|
|
|
|
if err := e.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "embed error")
|
|
|
|
}
|
|
|
|
sum += e.Length()
|
|
|
|
if sum > 6000 {
|
|
|
|
return nil, &discord.OverboundError{sum, 6000, "sum of text in embeds"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var msg *discord.Message
|
|
|
|
return msg, sendpart.PATCH(c.Client, data, &msg,
|
|
|
|
api.EndpointWebhooks+c.ID.String()+"/"+c.Token+"/messages/"+messageID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07-29 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:11:11 +00:00
|
|
|
// DeleteMessage deletes a message that was previously created by the same
|
|
|
|
// webhook.
|
|
|
|
func (c *Client) DeleteMessage(messageID discord.MessageID) error {
|
|
|
|
return c.FastRequest("DELETE",
|
|
|
|
api.EndpointWebhooks+c.ID.String()+"/"+c.Token+"/messages/"+messageID.String())
|
2020-07-29 19:29:30 +00:00
|
|
|
}
|