Module: Vox::HTTP::Routes::Webhook

Defined in:
lib/vox/http/routes/webhook.rb

Overview

Mixin for webhook routes.

Instance Method Summary collapse

Instance Method Details

#create_webhook(channel_id, name: :undef, avatar: :undef) ⇒ Hash<Symbol, Object>

Create a new webhook.

Parameters:

  • channel_id (String, Integer)

    The ID of the channel to create this webhook in.

  • name (String) (defaults to: :undef)

    The name for the webhook.

  • avatar (UploadIO) (defaults to: :undef)

    Image for the default webhook avatar.

Returns:

Required Permissions:

  • MANAGE_WEBHOOKS

View On Discord's Docs:



21
22
23
24
25
26
27
28
29
30
# File 'lib/vox/http/routes/webhook.rb', line 21

def create_webhook(channel_id, name: :undef, avatar: :undef)
  avatar_data = if avatar != :undef && !avatar.nil?
                  "data:#{avatar.content_type};base64,#{Base64.encode64(avatar.io.read)}"
                else
                  :undef
                end
  json = filter_undef({ name: name, avatar: avatar_data })
  route = Route.new(:POST, '/channels/%{channel_id}/webhooks', channel_id: channel_id)
  request(route, json: json)
end

#delete_webhook(webhook_id) ⇒ nil

Delete a webhook.

Parameters:

  • webhook_id (String, Integer)

    The ID of the webhook to be deleted.

Returns:

  • (nil)

    Returns ‘nil` on success.

Required Permissions:

  • MANAGE_WEBHOOKS

View On Discord's Docs:



120
121
122
# File 'lib/vox/http/routes/webhook.rb', line 120

def delete_webhook(webhook_id)
  request(Route.new(:DELETE, '/webhooks/%{webhook_id}', webhook_id: webhook_id))
end

#delete_webhook_with_token(webhook_id, webhook_token) ⇒ nil

Delete a webhook with a token. This endpoint does not require authorization.

Parameters:

  • webhook_id (String, Integer)

    The ID of the webhook to be deleted.

  • webhook_token (String)

    The token for the target webhook/

Returns:

  • (nil)

    Returns ‘nil` on success.

View On Discord's Docs:



129
130
131
132
133
# File 'lib/vox/http/routes/webhook.rb', line 129

def delete_webhook_with_token(webhook_id, webhook_token)
  route = Route.new(:DELETE, '/webhooks/%{webhook_id}/%{webhook_token}',
                    webhook_id: webhook_id, webhook_token: webhook_token)
  request(route)
end

#execute_webhook(webhook_id, webhook_token, wait: :undef, content: :undef, username: :undef, avatar_url: :undef, tts: :undef, file: :undef, embeds: :undef, allowed_mentions: :undef, attachments: :undef) ⇒ Object

Post content to a webhook.

Parameters:

  • webhook_id (String, Integer)

    The ID of the webhook to post to.

  • webhook_token (String)

    The token for the target webhook.

  • wait (true, false) (defaults to: :undef)

    Waits for server confirmation of message send before response, and returns the created message body.

  • content (String) (defaults to: :undef)

    The message contents.

  • username (String) (defaults to: :undef)

    Override the default avatar of the webhook.

  • avatar_url (String) (defaults to: :undef)

    Override the default avatar of the webhook.

  • tts (true, false) (defaults to: :undef)

    If this message is TTS.

  • file (UploadIO) (defaults to: :undef)

    The file being sent.

  • embeds (Array<Hash<Symbol, Object>>) (defaults to: :undef)

    An array of up to 10 [embed](discord.com/developers/docs/resources/channel#embed-object) objects.

  • allowed_mentions (Hash<Symbol, Object>) (defaults to: :undef)

    [Allowed mentions](discord.com/developers/docs/resources/channel#allowed-mentions-object) object for this message.

  • attachments (Hash<String, UploadIO>, Array<UploadIO>) (defaults to: :undef)

    A hash in the form of ‘filename => upload_io` to be referenced in embeds via the `attachment://` URI, or an array of UploadIO who’s filenames are derived from the existing UploadIO object. See [attachment:// docs](discord.com/developers/docs/resources/channel#create-message-using-attachments-within-embeds).

View On Discord's Docs:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/vox/http/routes/webhook.rb', line 153

def execute_webhook(webhook_id, webhook_token, wait: :undef, content: :undef, username: :undef,
                    avatar_url: :undef, tts: :undef, file: :undef, embeds: :undef,
                    allowed_mentions: :undef, attachments: :undef)
  params = filter_undef({ wait: wait })
  json = filter_undef({ content: content, username: username, avatar_url: avatar_url, tts: tts, embeds: embeds,
                        allowed_mentions: allowed_mentions })
  route = Route.new(:POST, '/webhooks/%{webhook_id}/%{webhook_token}',
                    webhook_id: webhook_id, webhook_token: webhook_token)

  if file != :undef
    data = { file: file, payload_json: MultiJson.dump(json) }
    request(route, data: data, query: params)
  elsif attachments != :undef
    attachments = attachments.collect { |k, v| UploadIO.new(v.io, v.content_type, k) } if attachments.is_a? Hash
    attach_hash = Array(0...attachments.size).zip(attachments).to_h
    data = attach_hash.merge(payload_json: MultiJson.dump(json))
    request(route, data: data, query: params)
  else
    request(route, json: json, query: params)
  end
end

#get_channel_webhooks(channel_id) ⇒ Array<Hash<Symbol, Object>>

Get webhooks associated with a channel.

Parameters:

  • channel_id (String, Integer)

    The channel to list webhooks for.

Returns:

Required Permissions:

  • MANAGE_WEBHOOKS

View On Discord's Docs:



38
39
40
# File 'lib/vox/http/routes/webhook.rb', line 38

def get_channel_webhooks(channel_id)
  request(Route.new(:GET, '/channels/%{channel_id}/webhooks', channel_id: channel_id))
end

#get_guild_webhooks(guild_id) ⇒ Array<Hash<Symbol, Object>>

Get webhooks associated with a guild.

Parameters:

  • guild_id (String, Integer)

    The guild to list webhooks for.

Returns:

Required Permissions:

  • MANAGE_WEBHOOKS

View On Discord's Docs:



48
49
50
# File 'lib/vox/http/routes/webhook.rb', line 48

def get_guild_webhooks(guild_id)
  request(Route.new(:GET, '/guilds/%{guild_id}/webhooks', guild_id: guild_id))
end

#get_webhook(webhook_id) ⇒ Hash<Symbol, Object>

Get a webhook by ID.

Parameters:

  • webhook_id (String, Integer)

    The ID of the desired webhook.

Returns:

Required Permissions:

  • MANAGE_WEBHOOKS

View On Discord's Docs:



58
59
60
# File 'lib/vox/http/routes/webhook.rb', line 58

def get_webhook(webhook_id)
  request(Route.new(:GET, '/webhooks/%{webhook_id}', webhook_id: webhook_id))
end

#get_webhook_with_token(webhook_id, webhook_token) ⇒ Hash<Symbol, Object>

Get a webhook by ID and token. This does not require authentication.

Parameters:

  • webhook_id (String, Integer)
  • webhook_token (String)

Returns:

View On Discord's Docs:



68
69
70
71
72
# File 'lib/vox/http/routes/webhook.rb', line 68

def get_webhook_with_token(webhook_id, webhook_token)
  route = Route.new(:GET, '/webhooks/%{webhook_id}/%{webhook_token}',
                    webhook_id: webhook_id, webhook_token: webhook_token)
  request(route)
end

#modify_webhook(webhook_id, name: :undef, avatar: :undef, channel_id: :undef) ⇒ Hash<Symbol, Object>

Modify a webhook’s properties.

Parameters:

  • webhook_id (String, Integer)

    The ID of the target webhook.

  • name (String) (defaults to: :undef)

    The new name for the webhook.

  • avatar (UploadIO) (defaults to: :undef)

    The new avatar for the webhook.

  • channel_id (String, Integer) (defaults to: :undef)

    The new channel for this webhook to use.

Returns:

Required Permissions:

  • MANAGE_WEBHOOKS

View On Discord's Docs:



83
84
85
86
87
88
89
90
91
92
# File 'lib/vox/http/routes/webhook.rb', line 83

def modify_webhook(webhook_id, name: :undef, avatar: :undef, channel_id: :undef)
  avatar_data = if avatar != :undef && !avatar.nil?
                  "data:#{avatar.content_type};base64,#{Base64.encode64(avatar.io.read)}"
                else
                  :undef
                end
  json = filter_undef({ name: name, avatar: avatar_data, channel_id: channel_id })
  route = Route.new(:PATCH, '/webhooks/%{webhook_id}', webhook_id: webhook_id)
  request(route, json: json)
end

#modify_webhook_with_token(webhook_id, webhook_token, name: :undef, avatar: :undef, channel_id: :undef) ⇒ Hash<Symbol, Object>

Modify a webhook’s properties with a token. This endpoint does not require authorization.

Parameters:

  • webhook_id (String, Integer)

    The ID of the target webhook.

  • webhook_token (String)

    The token of the target webhook.

  • name (String) (defaults to: :undef)

    The new name for the webhook.

  • avatar (UploadIO) (defaults to: :undef)

    The new avatar for the webhook.

  • channel_id (String, Integer) (defaults to: :undef)

    The new channel for this webhook to use.

Returns:

View On Discord's Docs:



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vox/http/routes/webhook.rb', line 103

def modify_webhook_with_token(webhook_id, webhook_token, name: :undef, avatar: :undef, channel_id: :undef)
  avatar_data = if avatar != :undef && !avatar.nil?
                  "data:#{avatar.content_type};base64,#{Base64.encode64(avatar.io.read)}"
                else
                  :undef
                end
  json = filter_undef({ name: name, avatar: avatar_data, channel_id: channel_id })
  route = Route.new(:PATCH, '/webhooks/%{webhook_id}/%{webhook_token}',
                    webhook_id: webhook_id, webhook_token: webhook_token)
  request(route, json: json)
end