1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-18 04:42:54 +00:00
arikawa/api/webhook.go

125 lines
4 KiB
Go
Raw Normal View History

2020-01-19 02:27:30 +00:00
package api
import (
"github.com/diamondburned/arikawa/discord"
"github.com/diamondburned/arikawa/utils/httputil"
2020-05-11 22:06:19 +00:00
"github.com/diamondburned/arikawa/utils/json/option"
2020-01-19 02:27:30 +00:00
)
var EndpointWebhooks = Endpoint + "webhooks/"
2020-01-19 02:27:30 +00:00
2020-05-11 22:06:19 +00:00
// https://discord.com/developers/docs/resources/webhook#create-webhook-json-params
type CreateWebhookData struct {
// Name is the name of the webhook (1-80 characters).
Name string `json:"name"`
// Avatar is the image for the default webhook avatar.
Avatar *Image `json:"avatar"`
}
// CreateWebhook creates a new webhook.
//
// Webhooks cannot be named "clyde".
//
// Requires the MANAGE_WEBHOOKS permission.
2020-01-19 02:27:30 +00:00
func (c *Client) CreateWebhook(
channelID discord.Snowflake,
name string, avatar discord.Hash) (*discord.Webhook, error) {
var param struct {
Name string `json:"name"`
Avatar discord.Hash `json:"avatar"`
}
param.Name = name
param.Avatar = avatar
var w *discord.Webhook
return w, c.RequestJSON(
&w, "POST",
EndpointChannels+channelID.String()+"/webhooks",
httputil.WithJSONBody(param),
2020-01-19 02:27:30 +00:00
)
}
2020-05-12 03:26:09 +00:00
// ChannelWebhooks returns the webhooks of the channel with the given ID.
2020-05-11 22:06:19 +00:00
//
// Requires the MANAGE_WEBHOOKS permission.
2020-05-12 03:26:09 +00:00
func (c *Client) ChannelWebhooks(channelID discord.Snowflake) ([]discord.Webhook, error) {
var ws []discord.Webhook
return ws, c.RequestJSON(&ws, "GET", EndpointChannels+channelID.String()+"/webhooks")
}
// GuildWebhooks returns the webhooks of the guild with the given ID.
//
// Requires the MANAGE_WEBHOOKS permission.
func (c *Client) GuildWebhooks(guildID discord.Snowflake) ([]discord.Webhook, error) {
2020-01-19 02:27:30 +00:00
var ws []discord.Webhook
2020-04-19 21:53:53 +00:00
return ws, c.RequestJSON(&ws, "GET", EndpointGuilds+guildID.String()+"/webhooks")
2020-01-19 02:27:30 +00:00
}
2020-05-11 22:06:19 +00:00
// Webhook returns the webhook with the given id.
2020-04-19 21:53:53 +00:00
func (c *Client) Webhook(webhookID discord.Snowflake) (*discord.Webhook, error) {
2020-01-19 02:27:30 +00:00
var w *discord.Webhook
2020-04-19 21:53:53 +00:00
return w, c.RequestJSON(&w, "GET", EndpointWebhooks+webhookID.String())
2020-01-19 02:27:30 +00:00
}
2020-05-11 22:06:19 +00:00
// WebhookWithToken is the same as above, except this call does not require
// authentication and returns no user in the webhook object.
2020-01-19 02:27:30 +00:00
func (c *Client) WebhookWithToken(
webhookID discord.Snowflake, token string) (*discord.Webhook, error) {
var w *discord.Webhook
2020-04-19 21:53:53 +00:00
return w, c.RequestJSON(&w, "GET", EndpointWebhooks+webhookID.String()+"/"+token)
2020-01-19 02:27:30 +00:00
}
2020-05-11 22:06:19 +00:00
// https://discord.com/developers/docs/resources/webhook#modify-webhook-json-params
2020-01-19 02:27:30 +00:00
type ModifyWebhookData struct {
2020-05-11 22:06:19 +00:00
// Name is the default name of the webhook.
Name option.String `json:"name,omitempty"`
// Avatar is the image for the default webhook avatar.
Avatar *Image `json:"avatar,omitempty"`
// ChannelID is the new channel id this webhook should be moved to.
2020-01-19 02:27:30 +00:00
ChannelID discord.Snowflake `json:"channel_id,omitempty"`
}
2020-05-11 22:06:19 +00:00
// ModifyWebhook modifies a webhook.
//
// Requires the MANAGE_WEBHOOKS permission.
2020-01-19 02:27:30 +00:00
func (c *Client) ModifyWebhook(
2020-05-11 22:06:19 +00:00
webhookID discord.Snowflake, data ModifyWebhookData) (*discord.Webhook, error) {
2020-01-19 02:27:30 +00:00
var w *discord.Webhook
2020-05-11 22:06:19 +00:00
return w, c.RequestJSON(
&w, "PATCH",
EndpointWebhooks+webhookID.String(),
httputil.WithJSONBody(data),
)
2020-01-19 02:27:30 +00:00
}
2020-05-11 22:06:19 +00:00
// ModifyWebhookWithToken is the same as above, except this call does not
// require authentication, does not accept a channel_id parameter in the body,
// and does not return a user in the webhook object.
2020-01-19 02:27:30 +00:00
func (c *Client) ModifyWebhookWithToken(
2020-05-11 22:06:19 +00:00
webhookID discord.Snowflake, data ModifyWebhookData, token string) (*discord.Webhook, error) {
2020-01-19 02:27:30 +00:00
var w *discord.Webhook
2020-05-11 22:06:19 +00:00
return w, c.RequestJSON(
&w, "PATCH",
EndpointWebhooks+webhookID.String()+"/"+token,
2020-05-12 00:16:08 +00:00
httputil.WithJSONBody(data),
)
2020-01-19 02:27:30 +00:00
}
2020-05-11 22:06:19 +00:00
// DeleteWebhook deletes a webhook permanently.
//
// Requires the MANAGE_WEBHOOKS permission.
2020-01-19 02:27:30 +00:00
func (c *Client) DeleteWebhook(webhookID discord.Snowflake) error {
return c.FastRequest("DELETE", EndpointWebhooks+webhookID.String())
}
2020-05-11 22:06:19 +00:00
// DeleteWebhookWithToken is the same as above, except this call does not
// require authentication.
2020-04-19 21:53:53 +00:00
func (c *Client) DeleteWebhookWithToken(webhookID discord.Snowflake, token string) error {
return c.FastRequest("DELETE", EndpointWebhooks+webhookID.String()+"/"+token)
2020-01-19 02:27:30 +00:00
}