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 <datutbrus@gmail.com>
This commit is contained in:
parent
9a820c3749
commit
f887060983
|
@ -185,6 +185,7 @@ func (c *Client) SendMessageComplex(
|
||||||
return msg, json.DecodeStream(body, &msg)
|
return msg, json.DecodeStream(body, &msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params
|
||||||
type ExecuteWebhookData struct {
|
type ExecuteWebhookData struct {
|
||||||
// Content are the message contents (up to 2000 characters).
|
// Content are the message contents (up to 2000 characters).
|
||||||
//
|
//
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
package webhook
|
package webhook
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"mime/multipart"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -13,11 +12,9 @@ import (
|
||||||
"github.com/diamondburned/arikawa/v2/discord"
|
"github.com/diamondburned/arikawa/v2/discord"
|
||||||
"github.com/diamondburned/arikawa/v2/utils/httputil"
|
"github.com/diamondburned/arikawa/v2/utils/httputil"
|
||||||
"github.com/diamondburned/arikawa/v2/utils/json"
|
"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.
|
// Client is the client used to interact with a webhook.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
// Client is the httputil.Client used to call Discord's API.
|
// 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))
|
httputil.WithJSONBody(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
writer := func(mw *multipart.Writer) error {
|
writer := data.WriteMultipart
|
||||||
return data.WriteMultipart(mw)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := c.MeanwhileMultipart(writer, "POST", URL)
|
resp, err := c.MeanwhileMultipart(writer, "POST", URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -129,35 +124,27 @@ func (c *Client) execute(data api.ExecuteWebhookData, wait bool) (*discord.Messa
|
||||||
return msg, json.DecodeStream(body, &msg)
|
return msg, json.DecodeStream(body, &msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get is a shortcut for NewCustomClient(token, id, DefaultHTTPClient).Get().
|
// https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params
|
||||||
func Get(id discord.WebhookID, token string) (*discord.Webhook, error) {
|
type EditWebhookMessageData struct {
|
||||||
return NewCustomClient(id, token, DefaultHTTPClient).Get()
|
// 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
|
// EditMessage edits a previously-sent webhook message from the same webhook.
|
||||||
// NewCustomClient(token, id, DefaultHTTPClient).Modify(data).
|
func (c *Client) EditMessage(messageID discord.MessageID, data EditWebhookMessageData) error {
|
||||||
func Modify(
|
return c.FastRequest("PATCH",
|
||||||
id discord.WebhookID, token string, data api.ModifyWebhookData) (*discord.Webhook, error) {
|
api.EndpointWebhooks+c.ID.String()+"/"+c.Token+"/messages/"+messageID.String(),
|
||||||
|
httputil.WithJSONBody(data))
|
||||||
return NewCustomClient(id, token, DefaultHTTPClient).Modify(data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete is a shortcut for
|
// DeleteMessage deletes a message that was previously created by the same
|
||||||
// NewCustomClient(token, id, DefaultHTTPClient).Delete().
|
// webhook.
|
||||||
func Delete(id discord.WebhookID, token string) error {
|
func (c *Client) DeleteMessage(messageID discord.MessageID) error {
|
||||||
return NewCustomClient(id, token, DefaultHTTPClient).Delete()
|
return c.FastRequest("DELETE",
|
||||||
}
|
api.EndpointWebhooks+c.ID.String()+"/"+c.Token+"/messages/"+messageID.String())
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue