Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
feat!: adjust webhook methods to match latest discord api docs (#475)
Browse files Browse the repository at this point in the history
* feat/fix!: adjust webhook methods to match latest discord api docs

* docs: uppercase struct names to match struct names

* style: format by project standards

* fix: validate before sending discord request

* fix: DeleteMessage only returns err
  • Loading branch information
DevYukine authored Mar 1, 2022
1 parent 2c210e2 commit 7aaf8fb
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 74 deletions.
34 changes: 21 additions & 13 deletions disgordtest/query_builders_nop_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions internal/endpoint/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func WebhookToken(id fmt.Stringer, token string) string {
return Webhook(id) + "/" + token
}

// WebhookMessage /webhooks/{webhook.id}/{webhook.token}/messages/{message.id}
func WebhookMessage(id fmt.Stringer, token string, messageId fmt.Stringer) string {
return WebhookToken(id, token) + "/messages/" + messageId.String()
}

// ChannelWebhooks /channels/{channel.id}/webhooks
func ChannelWebhooks(id fmt.Stringer) string {
return Channel(id) + webhooks
Expand Down
34 changes: 21 additions & 13 deletions query_builders_nop_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

191 changes: 143 additions & 48 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ type WebhookQueryBuilder interface {
// Delete Deletes a webhook permanently. User must be owner. Returns a 204 NO CONTENT response on success.
Delete() error

// Execute Trigger a webhook in Discord.
Execute(params *ExecuteWebhook, wait bool, threadID Snowflake, URLSuffix string) (*Message, error)

// ExecuteSlackWebhook Trigger a webhook in Discord from the Slack app.
ExecuteSlackWebhook(params *ExecuteWebhook, wait bool, threadID Snowflake) (*Message, error)

// ExecuteGitHubWebhook Trigger a webhook in Discord from the GitHub app.
ExecuteGitHubWebhook(params *ExecuteWebhook, wait bool, threadID Snowflake) (*Message, error)

WithToken(token string) WebhookWithTokenQueryBuilder
}

Expand Down Expand Up @@ -162,41 +153,12 @@ type ExecuteWebhook struct {
}

type execWebhook struct {
Wait bool `urlparam:"wait"`
ThreadID Snowflake `urlparam:"thread_id"`
Wait *bool `urlparam:"wait"`
ThreadID *Snowflake `urlparam:"thread_id"`
}

var _ URLQueryStringer = (*execWebhook)(nil)

// Execute Trigger a webhook in Discord.
func (w webhookQueryBuilder) Execute(params *ExecuteWebhook, wait bool, threadID Snowflake, URLSuffix string) (message *Message, err error) {
return w.WithToken("").WithFlags(w.flags).WithContext(w.ctx).Execute(params, wait, threadID, URLSuffix)
}

// ExecuteSlackWebhook [REST] Trigger a webhook in Discord from the Slack app.
// Method POST
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-slackcompatible-webhook
// Reviewed 2020-05-21
// Comment Refer to Slack's documentation for more information. We do not support Slack's channel,
// icon_emoji, mrkdwn, or mrkdwn_in properties.
func (w webhookQueryBuilder) ExecuteSlackWebhook(params *ExecuteWebhook, wait bool, threadID Snowflake) (message *Message, err error) {
return w.WithToken("").WithFlags(w.flags).WithContext(w.ctx).Execute(params, wait, threadID, endpoint.Slack())
}

// ExecuteGitHubWebhook [REST] Trigger a webhook in Discord from the GitHub app.
// Method POST
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-githubcompatible-webhook
// Reviewed 2020-05-21
// Comment Add a new webhook to your GitHub repo (in the repo's settings), and use this endpoint.
// as the "Payload URL." You can choose what events your Discord channel receives by
// choosing the "Let me select individual events" option and selecting individual
// events for the new webhook you're configuring.
func (w webhookQueryBuilder) ExecuteGitHubWebhook(params *ExecuteWebhook, wait bool, threadID Snowflake) (message *Message, err error) {
return w.WithToken("").WithFlags(w.flags).WithContext(w.ctx).Execute(params, wait, threadID, endpoint.GitHub())
}

type WebhookWithTokenQueryBuilder interface {
WithContext(ctx context.Context) WebhookWithTokenQueryBuilder
WithFlags(flags ...Flag) WebhookWithTokenQueryBuilder
Expand All @@ -215,7 +177,23 @@ type WebhookWithTokenQueryBuilder interface {
// Delete Same as DeleteWebhook, except this call does not require authentication.
Delete() error

Execute(params *ExecuteWebhook, wait bool, threadID Snowflake, URLSuffix string) (*Message, error)
// Execute Trigger a webhook in Discord.
Execute(params *ExecuteWebhook, wait *bool, threadID *Snowflake, URLSuffix string) (*Message, error)

// ExecuteSlackWebhook Trigger a webhook in Discord from the Slack app.
ExecuteSlackWebhook(params *ExecuteWebhook, wait *bool, threadID *Snowflake) (*Message, error)

// ExecuteGitHubWebhook Trigger a webhook in Discord from the GitHub app.
ExecuteGitHubWebhook(params *ExecuteWebhook, wait *bool, threadID *Snowflake) (*Message, error)

// GetMessage gets a previously sent message from this webhook.
GetMessage(messageId Snowflake, threadID *Snowflake) (*Message, error)

// EditMessage edits a previously sent message from this webhook.
EditMessage(params *ExecuteWebhook, messageId Snowflake, threadID *Snowflake) (*Message, error)

// DeleteMessage Deletes a previously sent message from this webhook.
DeleteMessage(messageId Snowflake, threadID *Snowflake) error
}

func (w webhookQueryBuilder) WithToken(token string) WebhookWithTokenQueryBuilder {
Expand Down Expand Up @@ -348,16 +326,13 @@ func (w webhookWithTokenQueryBuilder) Delete() error {
// Comment#2 For the webhook embed objects, you can set every field except type (it will be
// rich regardless of if you try to set it), provider, video, and any height, width,
// or proxy_url values for images.
func (w webhookWithTokenQueryBuilder) Execute(params *ExecuteWebhook, wait bool, threadID Snowflake, URLSuffix string) (message *Message, err error) {
func (w webhookWithTokenQueryBuilder) Execute(params *ExecuteWebhook, wait *bool, threadID *Snowflake, URLSuffix string) (message *Message, err error) {
if params == nil {
return nil, errors.New("params can not be nil")
}

if w.webhookID.IsZero() {
return nil, MissingWebhookIDErr
}
if w.token == "" {
return nil, MissingWebhookTokenErr
if err := w.validate(); err != nil {
return nil, err
}

var contentType string
Expand All @@ -379,10 +354,130 @@ func (w webhookWithTokenQueryBuilder) Execute(params *ExecuteWebhook, wait bool,
ContentType: contentType,
}, w.flags)
// Discord only returns the message when wait=true.
if wait {
if wait != nil && *wait == true {
r.pool = w.client.pool.message
return getMessage(r.Execute)
}
_, err = r.Execute()
return nil, err
}

// ExecuteSlackWebhook [REST] Trigger a webhook in Discord from the Slack app.
// Method POST
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-slackcompatible-webhook
// Reviewed 2020-05-21
// Comment Refer to Slack's documentation for more information. We do not support Slack's channel,
// icon_emoji, mrkdwn, or mrkdwn_in properties.
func (w *webhookWithTokenQueryBuilder) ExecuteSlackWebhook(params *ExecuteWebhook, wait *bool, threadID *Snowflake) (*Message, error) {
return w.Execute(params, wait, threadID, endpoint.Slack())
}

// ExecuteGitHubWebhook [REST] Trigger a webhook in Discord from the GitHub app.
// Method POST
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-githubcompatible-webhook
// Reviewed 2020-05-21
// Comment Add a new webhook to your GitHub repo (in the repo's settings), and use this endpoint.
// as the "Payload URL." You can choose what events your Discord channel receives by
// choosing the "Let me select individual events" option and selecting individual
// events for the new webhook you're configuring.
func (w *webhookWithTokenQueryBuilder) ExecuteGitHubWebhook(params *ExecuteWebhook, wait *bool, threadID *Snowflake) (*Message, error) {
return w.Execute(params, wait, threadID, endpoint.GitHub())
}

// GetMessage [REST] Gets a Message sent from this Webhook.
// Method GET
// Endpoint /webhooks/{webhook.id}/{webhook.token}/messages/{message.id}
// Discord documentation https://discord.com/developers/docs/resources/webhook#get-webhook-message
// Reviewed 2021-02-16
// Comment Returns a previously-sent webhook message from the same token. Returns a message object on success.
func (w *webhookWithTokenQueryBuilder) GetMessage(messageId Snowflake, threadID *Snowflake) (*Message, error) {
urlparams := &execWebhook{
ThreadID: threadID,
}

if err := w.validate(); err != nil {
return nil, err
}

r := w.client.newRESTRequest(&httd.Request{
Method: http.MethodGet,
Ctx: w.ctx,
Endpoint: endpoint.WebhookMessage(w.webhookID, w.token, messageId) + urlparams.URLQueryString(),
}, w.flags)
r.pool = w.client.pool.message
return getMessage(r.Execute)
}

// EditMessage [REST] Edits a previously-sent Webhook Message from the same token. Returns a Message object on success.
// Method PATCH
// Endpoint /webhooks/{webhook.id}/{webhook.token}/messages/{message.id}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-webhook
// Reviewed 2020-05-21
// Comment When the content field is edited, the mentions array in the message object will be reconstructed from scratch based on the new content.
// The allowed_mentions field of the edit request controls how this happens.
// If there is no explicit allowed_mentions in the edit request, the content will be parsed with default allowances, that is, without regard to whether or not an allowed_mentions was present in the request that originally created the message.
// Refer to Uploading Files for details on attachments and multipart/form-data requests.
// Any provided files will be appended to the message.
// To remove or replace files you will have to supply the attachments field which specifies the files to retain on the message after edit.
// Comment #2 All parameters to this endpoint are optional and nullable.
func (w *webhookWithTokenQueryBuilder) EditMessage(params *ExecuteWebhook, messageId Snowflake, threadID *Snowflake) (*Message, error) {
if params == nil {
return nil, errors.New("params can not be nil")
}

if err := w.validate(); err != nil {
return nil, err
}

var contentType string
if params.File == nil {
contentType = httd.ContentTypeJSON
} else {
contentType = "multipart/form-data"
}

urlparams := &execWebhook{
ThreadID: threadID,
}

r := w.client.newRESTRequest(&httd.Request{
Method: http.MethodPatch,
Ctx: w.ctx,
Endpoint: endpoint.WebhookMessage(w.webhookID, w.token, messageId) + urlparams.URLQueryString(),
Body: params,
ContentType: contentType,
}, w.flags)

r.pool = w.client.pool.message
return getMessage(r.Execute)
}

// DeleteMessage [REST] Deletes a Message sent from this Webhook.
// Method DELETE
// Endpoint /webhooks/{webhook.id}/{webhook.token}/messages/{message.id}
// Discord documentation https://discord.com/developers/docs/resources/webhook#delete-webhook-message
// Reviewed 2021-02-16
// Comment Deletes a message that was created by the webhook. Returns a 204 No Content response on success.
func (w *webhookWithTokenQueryBuilder) DeleteMessage(messageId Snowflake, threadID *Snowflake) error {
if err := w.validate(); err != nil {
return nil
}

urlparams := &execWebhook{
ThreadID: threadID,
}

r := w.client.newRESTRequest(&httd.Request{
Method: http.MethodDelete,
Ctx: w.ctx,
Endpoint: endpoint.WebhookMessage(w.webhookID, w.token, messageId) + urlparams.URLQueryString(),
}, w.flags)
r.pool = w.client.pool.message
_, err := r.Execute()
if err != nil {
return err
}
return nil
}

0 comments on commit 7aaf8fb

Please sign in to comment.