Class: OnyxCord::Webhook
- Inherits:
-
Object
- Object
- OnyxCord::Webhook
- Includes:
- IDObject
- Defined in:
- lib/onyxcord/models/webhook.rb
Overview
A webhook on a server channel
Instance Attribute Summary collapse
-
#avatar ⇒ String
The webhook's avatar id.
-
#channel ⇒ Channel
The channel that the webhook is currently connected to.
-
#name ⇒ String
The webhook name.
-
#owner ⇒ Member, ...
readonly
Gets the user object of the creator of the webhook.
-
#server ⇒ Server
readonly
The server that the webhook is currently connected to.
-
#token ⇒ String?
readonly
The webhook's token, if this is an Incoming Webhook.
-
#type ⇒ Integer
readonly
The webhook's type (1: Incoming, 2: Channel Follower).
Attributes included from IDObject
Instance Method Summary collapse
-
#avatar_url ⇒ String
Utility function to get a webhook's avatar URL.
-
#delete(reason = nil) ⇒ Object
Deletes the webhook.
-
#delete_avatar ⇒ Object
Deletes the webhook's avatar.
-
#delete_message(message) ⇒ Object
Delete a message created by this webhook.
-
#edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, builder: nil, components: nil, flags: 0, has_components: false, components_v2: false) {|builder| ... } ⇒ Message
Edit a message created by this webhook.
-
#execute(content: nil, username: nil, avatar_url: nil, tts: nil, file: nil, embeds: nil, allowed_mentions: nil, wait: true, builder: nil, components: nil, flags: 0, has_components: false, components_v2: false) {|builder| ... } ⇒ Message?
Execute a webhook.
-
#inspect ⇒ Object
The
inspectmethod is overwritten to give more useful output. -
#token? ⇒ true, false
Utility function to know if the webhook was requested through a webhook token, rather than auth.
-
#update(data) ⇒ Object
Updates the webhook if you need to edit more than 1 attribute.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#avatar ⇒ String
Returns the webhook's avatar id.
25 26 27 |
# File 'lib/onyxcord/models/webhook.rb', line 25 def avatar @avatar end |
#channel ⇒ Channel
Returns the channel that the webhook is currently connected to.
16 17 18 |
# File 'lib/onyxcord/models/webhook.rb', line 16 def channel @channel end |
#name ⇒ String
Returns the webhook name.
13 14 15 |
# File 'lib/onyxcord/models/webhook.rb', line 13 def name @name end |
#owner ⇒ Member, ... (readonly)
Gets the user object of the creator of the webhook. May be limited to username, discriminator, ID and avatar if the bot cannot reach the owner
33 34 35 |
# File 'lib/onyxcord/models/webhook.rb', line 33 def owner @owner end |
#server ⇒ Server (readonly)
Returns the server that the webhook is currently connected to.
19 20 21 |
# File 'lib/onyxcord/models/webhook.rb', line 19 def server @server end |
#token ⇒ String? (readonly)
Returns the webhook's token, if this is an Incoming Webhook.
22 23 24 |
# File 'lib/onyxcord/models/webhook.rb', line 22 def token @token end |
#type ⇒ Integer (readonly)
Returns the webhook's type (1: Incoming, 2: Channel Follower).
28 29 30 |
# File 'lib/onyxcord/models/webhook.rb', line 28 def type @type end |
Instance Method Details
#avatar_url ⇒ String
Utility function to get a webhook's avatar URL.
200 201 202 203 204 |
# File 'lib/onyxcord/models/webhook.rb', line 200 def avatar_url return REST::User.default_avatar(@id) unless @avatar REST::User.avatar_url(@id, @avatar) end |
#delete(reason = nil) ⇒ Object
Deletes the webhook.
96 97 98 99 100 101 102 |
# File 'lib/onyxcord/models/webhook.rb', line 96 def delete(reason = nil) if token? REST::Webhook.token_delete_webhook(@token, @id, reason) else REST::Webhook.delete_webhook(@bot.token, @id, reason) end end |
#delete_avatar ⇒ Object
Deletes the webhook's avatar.
64 65 66 |
# File 'lib/onyxcord/models/webhook.rb', line 64 def delete_avatar update_webhook(avatar: nil) end |
#delete_message(message) ⇒ Object
Delete a message created by this webhook.
161 162 163 164 165 |
# File 'lib/onyxcord/models/webhook.rb', line 161 def () raise OnyxCord::Errors::UnauthorizedWebhook unless @token REST::Webhook.(@token, @id, .resolve_id) end |
#edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, builder: nil, components: nil, flags: 0, has_components: false, components_v2: false) {|builder| ... } ⇒ Message
When editing allowed_mentions, it will update visually in the client but not alert the user with a notification.
Edit a message created by this webhook.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/onyxcord/models/webhook.rb', line 178 def (, content: nil, embeds: nil, allowed_mentions: nil, builder: nil, components: nil, flags: 0, has_components: false, components_v2: false) raise OnyxCord::Errors::UnauthorizedWebhook unless @token params = { allowed_mentions: allowed_mentions }.compact builder ||= Webhooks::Builder.new view ||= Webhooks::View.new yield(builder, view) if block_given? data = builder.to_json_hash.merge(params.compact) components ||= view flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: has_components || components_v2) edit_content = content == OnyxCord::MessagePayload::KEEP || !content.nil? ? content : data[:content] = == OnyxCord::MessagePayload::KEEP || !.nil? ? : data[:embeds] resp = REST::Webhook.(@token, @id, .resolve_id, edit_content, , data[:allowed_mentions], components.to_a, nil, flags) Message.new(OnyxCord::Internal::JSON.parse(resp), @bot) end |
#execute(content: nil, username: nil, avatar_url: nil, tts: nil, file: nil, embeds: nil, allowed_mentions: nil, wait: true, builder: nil, components: nil, flags: 0, has_components: false, components_v2: false) {|builder| ... } ⇒ Message?
This is only available to webhooks with publically exposed tokens. This excludes channel follow webhooks and webhooks retrieved via the audit log.
Execute a webhook.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/onyxcord/models/webhook.rb', line 140 def execute(content: nil, username: nil, avatar_url: nil, tts: nil, file: nil, embeds: nil, allowed_mentions: nil, wait: true, builder: nil, components: nil, flags: 0, has_components: false, components_v2: false) raise OnyxCord::Errors::UnauthorizedWebhook unless @token params = { content: content, username: username, avatar_url: avatar_url, tts: tts, file: file, embeds: , allowed_mentions: allowed_mentions } builder ||= Webhooks::Builder.new view = Webhooks::View.new yield(builder, view) if block_given? data = builder.to_json_hash.merge(params.compact) components ||= view flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: has_components || components_v2) resp = REST::Webhook.token_execute_webhook(@token, @id, wait, data[:content], data[:username], data[:avatar_url], data[:tts], data[:file], data[:embeds], data[:allowed_mentions], flags, components.to_a) Message.new(OnyxCord::Internal::JSON.parse(resp), @bot) if wait end |
#inspect ⇒ Object
The inspect method is overwritten to give more useful output.
207 208 209 |
# File 'lib/onyxcord/models/webhook.rb', line 207 def inspect "<Webhook name=#{@name} id=#{@id}>" end |
#token? ⇒ true, false
Utility function to know if the webhook was requested through a webhook token, rather than auth.
213 214 215 |
# File 'lib/onyxcord/models/webhook.rb', line 213 def token? @owner.nil? end |
#update(data) ⇒ Object
Updates the webhook if you need to edit more than 1 attribute.
86 87 88 89 90 91 92 |
# File 'lib/onyxcord/models/webhook.rb', line 86 def update(data) # Only pass a value for avatar if the key is defined as sending nil will delete the data[:avatar] = avatarise(data[:avatar]) if data.key?(:avatar) data[:channel_id] = data[:channel]&.resolve_id data.delete(:channel) update_webhook(**data) end |