From f887060983d83950de07cde61fd61b943a805278 Mon Sep 17 00:00:00 2001 From: Maximilian von Lindern <48887425+mavolin@users.noreply.github.com> Date: Wed, 16 Dec 2020 21:11:11 +0100 Subject: [PATCH] Webhook: Add support for message edit and delete (#180) * Webhook: Add support for message edit and delete * Webhook: remove global functions * Webhook: move package Webhook into api/ * Webhook: Moved (unused in API) data struct to package webhook Co-authored-by: diamondburned --- api/send.go | 1 + {webhook => api/webhook}/webhook.go | 55 +++++++++++------------------ 2 files changed, 22 insertions(+), 34 deletions(-) rename {webhook => api/webhook}/webhook.go (70%) diff --git a/api/send.go b/api/send.go index 1b2f986..a7688af 100644 --- a/api/send.go +++ b/api/send.go @@ -185,6 +185,7 @@ func (c *Client) SendMessageComplex( return msg, json.DecodeStream(body, &msg) } +// https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params type ExecuteWebhookData struct { // Content are the message contents (up to 2000 characters). // diff --git a/webhook/webhook.go b/api/webhook/webhook.go similarity index 70% rename from webhook/webhook.go rename to api/webhook/webhook.go index 058c531..4b91b18 100644 --- a/webhook/webhook.go +++ b/api/webhook/webhook.go @@ -3,7 +3,6 @@ package webhook import ( - "mime/multipart" "net/url" "strconv" @@ -13,11 +12,9 @@ import ( "github.com/diamondburned/arikawa/v2/discord" "github.com/diamondburned/arikawa/v2/utils/httputil" "github.com/diamondburned/arikawa/v2/utils/json" + "github.com/diamondburned/arikawa/v2/utils/json/option" ) -// DefaultHTTPClient is the httputil.Client used in the helper methods. -var DefaultHTTPClient = httputil.NewClient() - // Client is the client used to interact with a webhook. type Client struct { // Client is the httputil.Client used to call Discord's API. @@ -109,9 +106,7 @@ func (c *Client) execute(data api.ExecuteWebhookData, wait bool) (*discord.Messa httputil.WithJSONBody(data)) } - writer := func(mw *multipart.Writer) error { - return data.WriteMultipart(mw) - } + writer := data.WriteMultipart resp, err := c.MeanwhileMultipart(writer, "POST", URL) if err != nil { @@ -129,35 +124,27 @@ func (c *Client) execute(data api.ExecuteWebhookData, wait bool) (*discord.Messa return msg, json.DecodeStream(body, &msg) } -// Get is a shortcut for NewCustomClient(token, id, DefaultHTTPClient).Get(). -func Get(id discord.WebhookID, token string) (*discord.Webhook, error) { - return NewCustomClient(id, token, DefaultHTTPClient).Get() +// https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params +type EditWebhookMessageData struct { + // Content are the message contents. They may be up to 2000 characters + // characters long. + Content option.NullableString `json:"content,omitempty"` + // Embeds is an array of up to 10 discord.Embeds. + Embeds *[]discord.Embed `json:"embeds,omitempty"` + // AllowedMentions are the AllowedMentions for the message. + AllowedMentions *api.AllowedMentions `json:"allowed_mentions,omitempty"` } -// Modify is a shortcut for -// NewCustomClient(token, id, DefaultHTTPClient).Modify(data). -func Modify( - id discord.WebhookID, token string, data api.ModifyWebhookData) (*discord.Webhook, error) { - - return NewCustomClient(id, token, DefaultHTTPClient).Modify(data) +// EditMessage edits a previously-sent webhook message from the same webhook. +func (c *Client) EditMessage(messageID discord.MessageID, data EditWebhookMessageData) error { + return c.FastRequest("PATCH", + api.EndpointWebhooks+c.ID.String()+"/"+c.Token+"/messages/"+messageID.String(), + httputil.WithJSONBody(data)) } -// Delete is a shortcut for -// NewCustomClient(token, id, DefaultHTTPClient).Delete(). -func Delete(id discord.WebhookID, token string) error { - return NewCustomClient(id, token, DefaultHTTPClient).Delete() -} - -// Execute is a shortcut for -// NewCustomClient(token, id, DefaultHTTPClient).Execute(data). -func Execute(id discord.WebhookID, token string, data api.ExecuteWebhookData) error { - return NewCustomClient(id, token, DefaultHTTPClient).Execute(data) -} - -// ExecuteAndWait is a shortcut for -// NewCustomClient(token, id, DefaultHTTPClient).ExecuteAndWait(data). -func ExecuteAndWait( - id discord.WebhookID, token string, data api.ExecuteWebhookData) (*discord.Message, error) { - - return NewCustomClient(id, token, DefaultHTTPClient).ExecuteAndWait(data) +// 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()) }