webhook

package
v3.3.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: ISC Imports: 21 Imported by: 16

Documentation

Overview

Package webhook provides means to interact with webhooks directly and not through the bot API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseURL added in v3.3.3

func ParseURL(webhookURL string) (id discord.WebhookID, token string, err error)

ParseURL parses the given Discord webhook URL.

Types

type Client

type Client struct {
	// Client is the httputil.Client used to call Discord's API.
	*httputil.Client
	*Session
}

Client is the client used to interact with a webhook.

func FromAPI

func FromAPI(id discord.WebhookID, token string, c *api.Client) *Client

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 New

func New(id discord.WebhookID, token string) *Client

New creates a new Client using the passed webhook token and ID. It uses its own rate limiter.

func NewCustom

func NewCustom(id discord.WebhookID, token string, hcl *httputil.Client) *Client

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 NewFromURL added in v3.3.3

func NewFromURL(url string) (*Client, error)

NewFromURL creates a new webhook client using the passed webhook URL. It uses its own rate limiter.

func (*Client) Delete

func (c *Client) Delete() error

Delete deletes a webhook permanently.

func (*Client) DeleteMessage

func (c *Client) DeleteMessage(messageID discord.MessageID) error

DeleteMessage deletes a message that was previously created by the same webhook.

func (*Client) EditMessage

func (c *Client) EditMessage(messageID discord.MessageID, data EditMessageData) (*discord.Message, error)

EditMessage edits a previously-sent webhook message from the same webhook.

func (*Client) Execute

func (c *Client) Execute(data ExecuteData) (err error)

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.

func (*Client) ExecuteAndWait

func (c *Client) ExecuteAndWait(data ExecuteData) (*discord.Message, error)

ExecuteAndWait executes the webhook, and waits for the generated discord.Message to be returned.

func (*Client) Get

func (c *Client) Get() (*discord.Webhook, error)

Get gets the webhook.

func (*Client) Message

func (c *Client) Message(messageID discord.MessageID) (*discord.Message, error)

Message returns a previously-sent webhook message from the same token.

func (*Client) Modify

func (c *Client) Modify(data api.ModifyWebhookData) (*discord.Webhook, error)

Modify modifies the webhook.

func (*Client) WithContext

func (c *Client) WithContext(ctx context.Context) *Client

WithContext returns a shallow copy of Client with the given context. It's used for method timeouts and such. This method is thread-safe.

type EditMessageData

type EditMessageData struct {
	// Content is the new message contents (up to 2000 characters).
	Content option.NullableString `json:"content,omitempty"`
	// Embeds contains embedded rich content.
	Embeds *[]discord.Embed `json:"embeds,omitempty"`
	// Components contains the new components to attach.
	Components *discord.ContainerComponents `json:"components,omitempty"`
	// AllowedMentions are the allowed mentions for a message.
	AllowedMentions *api.AllowedMentions `json:"allowed_mentions,omitempty"`
	// Attachments are the attached files to keep
	Attachments *[]discord.Attachment `json:"attachments,omitempty"`

	Files []sendpart.File `json:"-"`
}

https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params

func (EditMessageData) NeedsMultipart

func (data EditMessageData) NeedsMultipart() bool

NeedsMultipart returns true if the SendMessageData has files.

func (EditMessageData) WriteMultipart

func (data EditMessageData) WriteMultipart(body *multipart.Writer) error

type ExecuteData

type ExecuteData struct {
	// Content are the message contents (up to 2000 characters).
	//
	// Required: one of content, file, embeds
	Content string `json:"content,omitempty"`

	// 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:"-"`

	// 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"`

	// Components is the list of components (such as buttons) to be attached to
	// the message.
	Components discord.ContainerComponents `json:"components,omitempty"`

	// Files represents a list of files to upload. This will not be
	// JSON-encoded and will only be available through WriteMultipart.
	Files []sendpart.File `json:"-"`

	// AllowedMentions are the allowed mentions for the message.
	AllowedMentions *api.AllowedMentions `json:"allowed_mentions,omitempty"`
}

https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params

func (ExecuteData) NeedsMultipart

func (data ExecuteData) NeedsMultipart() bool

NeedsMultipart returns true if the ExecuteWebhookData has files.

func (ExecuteData) WriteMultipart

func (data ExecuteData) WriteMultipart(body *multipart.Writer) error

WriteMultipart writes the webhook data into the given multipart body. It does not close body.

type InteractionErrorFunc added in v3.1.0

type InteractionErrorFunc func(w http.ResponseWriter, r *http.Request, code int, err error)

InteractionErrorFunc is called to write an error. err may be nil with a non-2xx code.

type InteractionHandler added in v3.1.0

type InteractionHandler interface {
	// HandleInteraction is expected to return a response synchronously, either
	// to be followed-up later by deferring the response or to be responded
	// immediately.
	HandleInteraction(*discord.InteractionEvent) *api.InteractionResponse
}

InteractionHandler is a type whose method is called on every incoming event.

func AlwaysDeferInteraction added in v3.1.0

func AlwaysDeferInteraction(flags discord.MessageFlags, f func(*discord.InteractionEvent)) InteractionHandler

AlwaysDeferInteraction always returns a DeferredMessageInteractionWithSource then invokes f in the background. This allows f to always use the follow-up functions.

type InteractionHandlerFunc added in v3.2.0

type InteractionHandlerFunc func(*discord.InteractionEvent) *api.InteractionResponse

InteractionHandlerFunc is a function type that implements the interface.

func (InteractionHandlerFunc) HandleInteraction added in v3.2.0

type InteractionServer added in v3.1.0

type InteractionServer struct {
	ErrorFunc InteractionErrorFunc
	// contains filtered or unexported fields
}

InteractionServer provides a HTTP handler to verify and handle Interaction Create events sent by Discord into a HTTP endpoint..

func NewInteractionServer added in v3.1.0

func NewInteractionServer(pubkey string, handler InteractionHandler) (*InteractionServer, error)

NewInteractionServer creates a new InteractionServer instance. pubkey should be hex-encoded.

func (*InteractionServer) ServeHTTP added in v3.1.0

func (s *InteractionServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type 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
}

Session keeps a single webhook session. It is referenced by other webhook clients using the same session.

func (*Session) Client added in v3.3.3

func (s *Session) Client() *Client

Client creates a new Webhook API client from the session.

func (*Session) OnRequest

func (s *Session) OnRequest(r httpdriver.Request) error

OnRequest should be called on each client request to inject itself.

func (*Session) OnResponse

func (s *Session) OnResponse(r httpdriver.Request, resp httpdriver.Response) error

OnResponse should be called after each client request to clean itself up.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL