webhook

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

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.NewClient(snowflake.Snowflake("webhookID"), "webhookToken")

webhook.NewClient 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.NewClient(snowflake.Snowflake("webhookID"), "webhookToken",
	webhook.WithLogger(logrus.New()),
	webhook.WithDefaultAllowedMentions(discord.AllowedMentions{
		RepliedUser: false,
	}),
)
Send Message

You can send a message as following

client := webhook.NewClient(snowflake.Snowflake("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(),
)
Edit Message

Messages can also be edited

client := webhook.NewClient(snowflake.Snowflake("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", webhook.NewWebhookMessageUpdateBuilder().
	SetContent("hello world!").
	Build(),
)
Delete Message

or deleted

client := webhook.NewClient(snowflake.Snowflake("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 DefaultConfig = Config{
	RestClientConfig:       &rest.DefaultConfig,
	DefaultAllowedMentions: &discord.DefaultAllowedMentions,
}

DefaultConfig is the default configuration for the webhook client

Functions

This section is empty.

Types

type Client

type Client struct {
	ID    snowflake.Snowflake
	Token string
	Config
}

Client is used to interact with the discord webhook api

func NewClient

func NewClient(id snowflake.Snowflake, token string, opts ...ConfigOpt) *Client

NewClient returns a new Client

func (*Client) Close

func (h *Client) Close(ctx context.Context)

Close closes all connections the webhook client has open

func (*Client) CreateContent

func (h *Client) CreateContent(content string, opts ...rest.RequestOpt) (*Message, error)

CreateContent creates a new message from the provided content

func (*Client) CreateEmbeds

func (h *Client) CreateEmbeds(embeds []discord.Embed, opts ...rest.RequestOpt) (*Message, error)

CreateEmbeds creates a new message from the provided embeds

func (*Client) CreateMessage

func (h *Client) CreateMessage(messageCreate discord.WebhookMessageCreate, opts ...rest.RequestOpt) (*Message, error)

CreateMessage creates a new message from the discord.WebhookMessageCreate

func (*Client) CreateMessageInThread

func (h *Client) CreateMessageInThread(messageCreate discord.WebhookMessageCreate, threadID snowflake.Snowflake, opts ...rest.RequestOpt) (*Message, error)

CreateMessageInThread creates a new Message in the provided thread

func (*Client) DeleteMessage

func (h *Client) DeleteMessage(messageID snowflake.Snowflake, opts ...rest.RequestOpt) error

DeleteMessage deletes an already sent webhook message

func (*Client) DeleteMessageInThread added in v0.6.8

func (h *Client) DeleteMessageInThread(messageID snowflake.Snowflake, threadID snowflake.Snowflake, opts ...rest.RequestOpt) error

DeleteMessageInThread deletes an already sent webhook message in a thread

func (*Client) DeleteWebhook

func (h *Client) DeleteWebhook(opts ...rest.RequestOpt) error

DeleteWebhook deletes the current webhook

func (*Client) GetWebhook

func (h *Client) GetWebhook(opts ...rest.RequestOpt) (*Webhook, error)

GetWebhook fetches the current webhook from discord

func (*Client) URL

func (h *Client) URL() string

URL returns the full webhook URL

func (*Client) UpdateContent

func (h *Client) UpdateContent(messageID snowflake.Snowflake, content string, opts ...rest.RequestOpt) (*Message, error)

UpdateContent updates an already sent webhook message with the content

func (*Client) UpdateEmbeds

func (h *Client) UpdateEmbeds(messageID snowflake.Snowflake, embeds []discord.Embed, opts ...rest.RequestOpt) (*Message, error)

UpdateEmbeds updates an already sent webhook message with the embeds

func (*Client) UpdateMessage

func (h *Client) UpdateMessage(messageID snowflake.Snowflake, messageUpdate discord.WebhookMessageUpdate, opts ...rest.RequestOpt) (*Message, error)

UpdateMessage updates an already sent webhook message with the discord.WebhookMessageUpdate

func (*Client) UpdateMessageInThread added in v0.6.8

func (h *Client) UpdateMessageInThread(messageID snowflake.Snowflake, messageUpdate discord.WebhookMessageUpdate, threadID snowflake.Snowflake, opts ...rest.RequestOpt) (*Message, error)

UpdateMessageInThread updates an already sent webhook message with the discord.WebhookMessageUpdate in a thread

func (*Client) UpdateWebhook

func (h *Client) UpdateWebhook(webhookUpdate discord.WebhookUpdateWithToken, opts ...rest.RequestOpt) (*Webhook, error)

UpdateWebhook updates the current webhook

type Config

type Config struct {
	Logger                 log.Logger
	RestClient             rest.Client
	RestClientConfig       *rest.Config
	WebhookService         rest.WebhookService
	EntityBuilder          EntityBuilder
	DefaultAllowedMentions *discord.AllowedMentions
}

Config is the 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 WithEntityBuilder

func WithEntityBuilder(entityBuilder EntityBuilder) ConfigOpt

WithEntityBuilder sets the entity builder for the webhook client

func WithLogger

func WithLogger(logger log.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 WithRestClientConfig

func WithRestClientConfig(restConfig rest.Config) ConfigOpt

WithRestClientConfig sets the rest client configuration for the webhook client

func WithRestClientConfigOpts

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

WithRestClientConfigOpts sets the rest client configuration for the webhook client

func WithWebhookService

func WithWebhookService(webhookService rest.WebhookService) ConfigOpt

WithWebhookService sets the webhook service for the webhook client

type EntityBuilder

type EntityBuilder interface {
	// WebhookClient returns the underlying webhook client used by this EntityBuilder
	WebhookClient() *Client

	CreateMessage(message discord.Message) *Message

	// CreateWebhook returns a new webhook.Webhook from the discord.Webhook
	CreateWebhook(webhook discord.Webhook) *Webhook
}

EntityBuilder is used to transform discord package entities into webhook package entities which hold a reference to the webhook client

func NewEntityBuilder

func NewEntityBuilder(webhookClient *Client) EntityBuilder

NewEntityBuilder returns a new default EntityBuilder

type Message

type Message struct {
	discord.Message
	WebhookClient *Client
}

Message represents a discord.Message which can be directly edited by the Client

func (*Message) ActionRows

func (m *Message) ActionRows() []discord.ActionRowComponent

ActionRows returns all discord.ActionRowComponent(s) from this Message

func (*Message) ButtonByID

func (m *Message) ButtonByID(customID discord.CustomID) *discord.ButtonComponent

ButtonByID returns a ButtonComponent with the specific customID from this Message

func (*Message) Buttons

func (m *Message) Buttons() []discord.ButtonComponent

Buttons returns all ButtonComponent(s) from this Message

func (*Message) ComponentByID

func (m *Message) ComponentByID(customID discord.CustomID) discord.InteractiveComponent

ComponentByID returns the discord.Component with the specific discord.CustomID

func (*Message) Delete

func (m *Message) Delete(opts ...rest.RequestOpt) error

Delete allows you to edit an existing Message sent by you

func (*Message) InteractiveComponents

func (m *Message) InteractiveComponents() []discord.InteractiveComponent

InteractiveComponents returns the discord.InteractiveComponent(s) from this Message

func (*Message) SelectMenuByID

func (m *Message) SelectMenuByID(customID discord.CustomID) *discord.SelectMenuComponent

SelectMenuByID returns a SelectMenuComponent with the specific customID from this Message

func (*Message) SelectMenus

func (m *Message) SelectMenus() []discord.SelectMenuComponent

SelectMenus returns all SelectMenuComponent(s) from this Message

func (*Message) Update

func (m *Message) Update(messageUpdate discord.WebhookMessageUpdate, opts ...rest.RequestOpt) (*Message, error)

Update allows you to edit an existing Message sent by you

type Webhook

type Webhook struct {
	discord.IncomingWebhook
	WebhookClient *Client
}

Webhook can be used to update or delete the Webhook

func (*Webhook) Delete

func (h *Webhook) Delete(opts ...rest.RequestOpt) error

Delete is used to delete the Webhook

func (*Webhook) Update

func (h *Webhook) Update(webhookUpdate discord.WebhookUpdateWithToken, opts ...rest.RequestOpt) (*Webhook, error)

Update is used to update the Webhook

Jump to

Keyboard shortcuts

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