webhook

package
v0.18.6 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: Apache-2.0 Imports: 8 Imported by: 38

README

webhook

Webhook module of disgo

Usage

Import the package into your project.

import "github.com/disgoorg/disgo/webhook"

Create a new Webhook by webhook_id and webhook_token. (This WebhookClient should be created once as it holds important state)

client := webhook.New(snowflake.ID("webhookID"), "webhookToken")

client, err := webhook.NewWithURL("webhookURL")

webhook.New takes a vararg of type webhook.ConfigOpt as third argument which lets you pass additional optional parameter like a custom logger, rest client, etc

Optional Arguments
client := webhook.New(snowflake.ID("webhookID"), "webhookToken",
	webhook.WithLogger(logrus.New()),
	webhook.WithDefaultAllowedMentions(discord.AllowedMentions{
		RepliedUser: false,
	}),
)
Send Message

You can send a message as following

client := webhook.New(snowflake.ID("webhookID"), "webhookToken")

message, err := client.CreateContent("hello world!")

message, err := client.CreateEmbeds(discord.NewEmbedBuilder().
	SetDescription("hello world!").
	Build(),
)

message, err := client.CreateMessage(webhook.NewWebhookMessageCreateBuilder().
	SetContent("hello world!").
	Build(),
)

message, err := client.CreateMessage(discord.WebhookMessageCreate{
	Content: "hello world!",
})
Edit Message

Messages can also be edited

client := webhook.New(snowflake.ID("webhookID"), "webhookToken")

message, err := client.UpdateContent("870741249114652722", "hello world!")

message, err := client.UpdateEmbeds("870741249114652722", discord.NewEmbedBuilder().
	SetDescription("hello world!").
	Build(),
)

message, err := client.UpdateMessage("870741249114652722", discord.NewWebhookMessageUpdateBuilder().
	SetContent("hello world!").
	Build(),
)

message, err := client.UpdateMessage("870741249114652722", discord.WebhookMessageUpdate{
	Content: json.Ptr("hello world!"),
})
Delete Message

or deleted

client := webhook.New(snowflake.ID("webhookID"), "webhookToken")

err := client.DeleteMessage("message_id")
Full Example

a full example can be found here

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidWebhookURL = errors.New("invalid webhook URL")

Functions

This section is empty.

Types

type Client

type Client interface {
	// ID returns the configured Webhook id
	ID() snowflake.ID
	// Token returns the configured Webhook token
	Token() string
	// URL returns the full Webhook URL
	URL() string
	// Close closes all connections the Webhook Client has open
	Close(ctx context.Context)
	// Rest returns the underlying rest.Webhooks
	Rest() rest.Webhooks

	// GetWebhook fetches the current Webhook from discord
	GetWebhook(opts ...rest.RequestOpt) (*discord.IncomingWebhook, error)
	// UpdateWebhook updates the current Webhook
	UpdateWebhook(webhookUpdate discord.WebhookUpdateWithToken, opts ...rest.RequestOpt) (*discord.IncomingWebhook, error)
	// DeleteWebhook deletes the current Webhook
	DeleteWebhook(opts ...rest.RequestOpt) error

	// CreateMessage creates a new Message from the discord.WebhookMessageCreate
	CreateMessage(messageCreate discord.WebhookMessageCreate, opts ...rest.RequestOpt) (*discord.Message, error)
	// CreateMessageInThread creates a new Message from the discord.WebhookMessageCreate in the provided thread
	CreateMessageInThread(messageCreate discord.WebhookMessageCreate, threadID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error)
	// CreateContent creates a new Message from the provided content
	CreateContent(content string, opts ...rest.RequestOpt) (*discord.Message, error)
	// CreateEmbeds creates a new Message from the provided discord.Embed(s)
	CreateEmbeds(embeds []discord.Embed, opts ...rest.RequestOpt) (*discord.Message, error)

	// UpdateMessage updates an already sent Webhook Message with the discord.WebhookMessageUpdate
	UpdateMessage(messageID snowflake.ID, messageUpdate discord.WebhookMessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)
	// UpdateMessageInThread updates an already sent Webhook Message with the discord.WebhookMessageUpdate in the provided thread
	UpdateMessageInThread(messageID snowflake.ID, messageUpdate discord.WebhookMessageUpdate, threadID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error)
	// UpdateContent updates an already sent Webhook Message with the content
	UpdateContent(messageID snowflake.ID, content string, opts ...rest.RequestOpt) (*discord.Message, error)
	// UpdateEmbeds updates an already sent Webhook Message with the discord.Embed(s)
	UpdateEmbeds(messageID snowflake.ID, embeds []discord.Embed, opts ...rest.RequestOpt) (*discord.Message, error)

	// DeleteMessage deletes an already sent Webhook Message
	DeleteMessage(messageID snowflake.ID, opts ...rest.RequestOpt) error
	// DeleteMessageInThread deletes an already sent Webhook Message in the provided thread
	DeleteMessageInThread(messageID snowflake.ID, threadID snowflake.ID, opts ...rest.RequestOpt) error
}

Client is a high level interface for interacting with Discord's Webhooks API.

func New added in v0.12.0

func New(id snowflake.ID, token string, opts ...ConfigOpt) Client

New creates a new Client with the given ID, token and ConfigOpt(s).

func NewWithURL added in v0.14.1

func NewWithURL(webhookURL string, opts ...ConfigOpt) (Client, error)

NewWithURL creates a new Client by parsing the given webhookURL for the ID and token.

type Config

type Config struct {
	Logger                 *slog.Logger
	RestClient             rest.Client
	RestClientConfigOpts   []rest.ConfigOpt
	Webhooks               rest.Webhooks
	DefaultAllowedMentions *discord.AllowedMentions
}

Config is the configuration for the webhook client

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig is the default configuration for the webhook client

func (*Config) Apply

func (c *Config) Apply(opts []ConfigOpt)

Apply applies all options to the config

type ConfigOpt

type ConfigOpt func(config *Config)

ConfigOpt is used to provide optional parameters to the webhook client

func WithDefaultAllowedMentions

func WithDefaultAllowedMentions(allowedMentions discord.AllowedMentions) ConfigOpt

WithDefaultAllowedMentions sets the default allowed mentions for the webhook client

func WithLogger

func WithLogger(logger *slog.Logger) ConfigOpt

WithLogger sets the logger for the webhook client

func WithRestClient

func WithRestClient(restClient rest.Client) ConfigOpt

WithRestClient sets the rest client for the webhook client

func WithRestClientConfigOpts

func WithRestClientConfigOpts(opts ...rest.ConfigOpt) ConfigOpt

WithRestClientConfigOpts sets the rest client configuration for the webhook client

func WithWebhooks

func WithWebhooks(webhooks rest.Webhooks) ConfigOpt

WithWebhooks sets the webhook service for the webhook client

Jump to

Keyboard shortcuts

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