Class: Discorb::Webhook Abstract
- Inherits:
-
Object
- Object
- Discorb::Webhook
- Defined in:
- lib/discorb/webhook.rb
Overview
Represents a webhook.
Direct Known Subclasses
ApplicationWebhook, FollowerWebhook, IncomingWebhook, URLWebhook
Defined Under Namespace
Classes: ApplicationWebhook, FollowerWebhook, IncomingWebhook, Message, URLWebhook
Instance Attribute Summary collapse
- #application_id -> Discorb::Snowflake? readonly
-
#avatar -> Discorb::Asset
readonly
The avatar of the webhook.
-
#channel_id -> Discorb::Snowflake
readonly
The ID of the channel this webhook belongs to.
-
#guild_id -> Discorb::Snowflake
readonly
The ID of the guild this webhook belongs to.
-
#name -> String
readonly
The name of the webhook.
-
#token -> String
readonly
The URL of the webhook.
-
#user -> Discorb::User
readonly
The user that created this webhook.
Class Method Summary collapse
- .from_url(url) -> Object
-
.new(url) -> Discorb::Webhook::URLWebhook
Creates URLWebhook.
Instance Method Summary collapse
-
#delete! -> Object
(also: #destroy!)
Deletes the webhook.
-
#delete_message!(message) -> Object
Deletes the webhook's message.
-
#edit(name: :unset, avatar: :unset, channel: :unset) -> Object
(also: #modify)
Edits the webhook.
-
#edit_message(message, content = :unset, embed: :unset, embeds: :unset, file: :unset, files: :unset, attachments: :unset, allowed_mentions: :unset) -> Object
Edits the webhook's message.
- #inspect -> Object
-
#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, file: nil, files: nil, username: nil, avatar_url: :unset, wait: true) -> Discorb::Webhook::Message?
(also: #execute)
Posts a message to the webhook.
Instance Attribute Details
#application_id -> Discorb::Snowflake? (readonly)
21 22 23 |
# File 'lib/discorb/webhook.rb', line 21 def application_id @application_id end |
#avatar -> Discorb::Asset (readonly)
Returns The avatar of the webhook.
18 19 20 |
# File 'lib/discorb/webhook.rb', line 18 def avatar @avatar end |
#channel_id -> Discorb::Snowflake (readonly)
Returns The ID of the channel this webhook belongs to.
14 15 16 |
# File 'lib/discorb/webhook.rb', line 14 def channel_id @channel_id end |
#guild_id -> Discorb::Snowflake (readonly)
Returns The ID of the guild this webhook belongs to.
12 13 14 |
# File 'lib/discorb/webhook.rb', line 12 def guild_id @guild_id end |
#name -> String (readonly)
Returns The name of the webhook.
10 11 12 |
# File 'lib/discorb/webhook.rb', line 10 def name @name end |
#token -> String (readonly)
Returns The URL of the webhook.
23 24 25 |
# File 'lib/discorb/webhook.rb', line 23 def token @token end |
#user -> Discorb::User (readonly)
Returns The user that created this webhook.
16 17 18 |
# File 'lib/discorb/webhook.rb', line 16 def user @user end |
Class Method Details
.from_url(url) -> Object
414 415 416 |
# File 'lib/discorb/webhook.rb', line 414 def from_url(url) URLWebhook.new(url) end |
.new(url) -> Discorb::Webhook::URLWebhook
Creates URLWebhook.
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/discorb/webhook.rb', line 394 def new(url) if self != Webhook return super(*url) if url.is_a?(Array) return super end if url.is_a?(String) URLWebhook.new(url) else case url[1][:type] when 1 IncomingWebhook when 2 FollowerWebhook when 3 ApplicationWebhook end.new(url) end end |
Instance Method Details
#delete! -> Object Also known as: destroy!
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Deletes the webhook.
121 122 123 124 125 126 |
# File 'lib/discorb/webhook.rb', line 121 def delete! Async do @http.delete(url).wait self end end |
#delete_message!(message) -> Object
Deletes the webhook's message.
179 180 181 182 183 184 |
# File 'lib/discorb/webhook.rb', line 179 def () Async do @http.delete("#{url}/messages/#{Utils.try(, :id)}").wait end end |
#edit(name: :unset, avatar: :unset, channel: :unset) -> Object Also known as: modify
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
The arguments of this method are defaultly set to :unset
. Specify value to set the value, if not don't specify or specify :unset
.
Edits the webhook.
104 105 106 107 108 109 110 111 112 |
# File 'lib/discorb/webhook.rb', line 104 def edit(name: :unset, avatar: :unset, channel: :unset) Async do payload = {} payload[:name] = name if name != :unset payload[:avatar] = avatar if avatar != :unset payload[:channel_id] = Utils.try(channel, :id) if channel != :unset @http.patch(url.to_s, payload).wait end end |
#edit_message(message, content = :unset, embed: :unset, embeds: :unset, file: :unset, files: :unset, attachments: :unset, allowed_mentions: :unset) -> Object
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
The arguments of this method are defaultly set to :unset
. Specify value to set the value, if not don't specify or specify :unset
.
Edits the webhook's message.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/discorb/webhook.rb', line 145 def ( , content = :unset, embed: :unset, embeds: :unset, file: :unset, files: :unset, attachments: :unset, allowed_mentions: :unset ) Async do payload = {} payload[:content] = content if content != :unset payload[:embeds] = ? [.to_hash] : [] if != :unset payload[:embeds] = .map(&:to_hash) if != :unset = [] if != :unset payload[:attachments] = .map(&:to_hash) if != :unset payload[:allowed_mentions] = allowed_mentions if allowed_mentions != :unset files = [file] if file != :unset if files == :unset headers = { "Content-Type" => "application/json", } else headers, payload = HTTP.multipart(payload, files) end _resp, data = @http.patch("#{url}/messages/#{Utils.try(, :id)}", payload, headers: headers).wait .send(:_set_data, data) end end |
#inspect -> Object
40 41 42 |
# File 'lib/discorb/webhook.rb', line 40 def inspect "#<#{self.class} #{@name.inspect} id=#{@id}>" end |
#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, file: nil, files: nil, username: nil, avatar_url: :unset, wait: true) -> Discorb::Webhook::Message? Also known as: execute
This is an asynchronous method, it will return a Async::Task
object. Use Async::Task#wait
to get the result.
This method calls HTTP request.
Posts a message to the webhook.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/discorb/webhook.rb', line 63 def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, file: nil, files: nil, username: nil, avatar_url: :unset, wait: true) Async do payload = {} payload[:content] = content if content payload[:tts] = tts = if [] elsif end payload[:embeds] = .map(&:to_hash) if payload[:allowed_mentions] = allowed_mentions&.to_hash payload[:username] = username if username payload[:avatar_url] = avatar_url if avatar_url != :unset files = [file] if file if files headers, payload = HTTP.multipart(payload, files) else headers = { "Content-Type" => "application/json", } end _resp, data = @http.post("#{url}?wait=#{wait}", payload, headers: headers).wait data && Webhook::Message.new(self, data) end end |