Class: OnyxCord::Webhooks::Client
- Inherits:
-
Object
- Object
- OnyxCord::Webhooks::Client
- Defined in:
- lib/onyxcord/webhooks/client.rb
Overview
A client for a particular webhook added to a Discord channel.
Instance Method Summary collapse
-
#delete(reason: nil) ⇒ RestClient::Response
Delete this webhook.
-
#delete_message(message_id) ⇒ RestClient::Response
Delete a message created by this webhook.
-
#edit_message(message_id, builder: nil, content: nil, embeds: nil, allowed_mentions: nil, components: nil, flags: nil, thread_id: nil, has_components: false, components_v2: false) {|builder| ... } ⇒ RestClient::Response
Edit a message from this webhook.
-
#execute(builder = nil, wait = false, components = nil, thread_id: nil, flags: nil, has_components: false, components_v2: false) {|builder| ... } ⇒ RestClient::Response
Executes the webhook this client points to with the given data.
-
#initialize(url: nil, id: nil, token: nil) ⇒ Client
constructor
Create a new webhook.
-
#modify(name: nil, avatar: nil, channel_id: nil) ⇒ RestClient::Response
Modify this webhook’s properties.
Constructor Details
#initialize(url: nil, id: nil, token: nil) ⇒ Client
Create a new webhook
17 18 19 |
# File 'lib/onyxcord/webhooks/client.rb', line 17 def initialize(url: nil, id: nil, token: nil) @url = url || generate_url(id, token) end |
Instance Method Details
#delete(reason: nil) ⇒ RestClient::Response
Note:
This is permanent and cannot be undone.
Delete this webhook.
73 74 75 |
# File 'lib/onyxcord/webhooks/client.rb', line 73 def delete(reason: nil) RestClient.delete(@url, 'X-Audit-Log-Reason': reason) end |
#delete_message(message_id) ⇒ RestClient::Response
Delete a message created by this webhook.
114 115 116 |
# File 'lib/onyxcord/webhooks/client.rb', line 114 def () RestClient.delete("#{@url}/messages/#{}") end |
#edit_message(message_id, builder: nil, content: nil, embeds: nil, allowed_mentions: nil, components: nil, flags: nil, thread_id: nil, has_components: false, components_v2: false) {|builder| ... } ⇒ RestClient::Response
Note:
Not all builder options are available when editing.
Edit a message from this webhook.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/onyxcord/webhooks/client.rb', line 94 def (, builder: nil, content: nil, embeds: nil, allowed_mentions: nil, components: nil, flags: nil, thread_id: nil, has_components: false, components_v2: false) builder ||= Builder.new yield builder if block_given? query = URI.encode_www_form({ thread_id: }.compact) components = View.component_payload(components) unless components.nil? data = builder.to_json_hash builder_flags = data[:flags] if data.is_a?(Hash) flags = View.apply_v2_flag(flags || builder_flags, components, force: has_components || components_v2) data = data.merge({ content: content, embeds: , allowed_mentions: allowed_mentions, components: components, flags: flags }.compact) RestClient.patch( "#{@url}/messages/#{}#{(query.empty? ? '' : "?#{query}")}", data.compact.to_json, content_type: :json ) end |
#execute(builder = nil, wait = false, components = nil, thread_id: nil, flags: nil, has_components: false, components_v2: false) {|builder| ... } ⇒ RestClient::Response
Executes the webhook this client points to with the given data.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/onyxcord/webhooks/client.rb', line 42 def execute(builder = nil, wait = false, components = nil, thread_id: nil, flags: nil, has_components: false, components_v2: false) raise TypeError, 'builder needs to be nil or like a OnyxCord::Webhooks::Builder!' unless (builder.respond_to?(:file) && builder.respond_to?(:to_multipart_hash)) || builder.respond_to?(:to_json_hash) || builder.nil? builder ||= Builder.new view = View.new yield(builder, view) if block_given? components ||= view if builder.file post_multipart(builder, components, wait, thread_id, flags: flags, has_components: has_components || components_v2) else post_json(builder, components, wait, thread_id, flags: flags, has_components: has_components || components_v2) end end |
#modify(name: nil, avatar: nil, channel_id: nil) ⇒ RestClient::Response
Modify this webhook’s properties.
65 66 67 |
# File 'lib/onyxcord/webhooks/client.rb', line 65 def modify(name: nil, avatar: nil, channel_id: nil) RestClient.patch(@url, { name: name, avatar: avatarise(avatar), channel_id: channel_id }.compact.to_json, content_type: :json) end |