Class: Discordrb::Webhook
- Inherits:
-
Object
- Object
- Discordrb::Webhook
- Includes:
- IDObject
- Defined in:
- lib/discordrb/data/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) {|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) {|builder| ... } ⇒ Message?
Execute a webhook.
-
#initialize(data, bot) ⇒ Webhook
constructor
A new instance of Webhook.
-
#inspect ⇒ Object
The
inspect
method 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
Constructor Details
#initialize(data, bot) ⇒ Webhook
Returns a new instance of Webhook.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/discordrb/data/webhook.rb', line 34 def initialize(data, bot) @bot = bot @name = data['name'] @id = data['id'].to_i @channel = bot.channel(data['channel_id']) @server = @channel.server @token = data['token'] @avatar = data['avatar'] @type = data['type'] # Will not exist if the data was requested through a webhook token return unless data['user'] @owner = @server.member(data['user']['id'].to_i) return if @owner Discordrb::LOGGER.debug("Member with ID #{data['user']['id']} not cached (possibly left the server).") @owner = @bot.ensure_user(data['user']) end |
Instance Attribute Details
#avatar ⇒ String
Returns the webhook's avatar id.
24 25 26 |
# File 'lib/discordrb/data/webhook.rb', line 24 def avatar @avatar end |
#channel ⇒ Channel
Returns the channel that the webhook is currently connected to.
15 16 17 |
# File 'lib/discordrb/data/webhook.rb', line 15 def channel @channel end |
#name ⇒ String
Returns the webhook name.
12 13 14 |
# File 'lib/discordrb/data/webhook.rb', line 12 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
32 33 34 |
# File 'lib/discordrb/data/webhook.rb', line 32 def owner @owner end |
#server ⇒ Server (readonly)
Returns the server that the webhook is currently connected to.
18 19 20 |
# File 'lib/discordrb/data/webhook.rb', line 18 def server @server end |
#token ⇒ String? (readonly)
Returns the webhook's token, if this is an Incoming Webhook.
21 22 23 |
# File 'lib/discordrb/data/webhook.rb', line 21 def token @token end |
#type ⇒ Integer (readonly)
Returns the webhook's type (1: Incoming, 2: Channel Follower).
27 28 29 |
# File 'lib/discordrb/data/webhook.rb', line 27 def type @type end |
Instance Method Details
#avatar_url ⇒ String
Utility function to get a webhook's avatar URL.
194 195 196 197 198 |
# File 'lib/discordrb/data/webhook.rb', line 194 def avatar_url return API::User.default_avatar(@id) unless @avatar API::User.avatar_url(@id, @avatar) end |
#delete(reason = nil) ⇒ Object
Deletes the webhook.
94 95 96 97 98 99 100 |
# File 'lib/discordrb/data/webhook.rb', line 94 def delete(reason = nil) if token? API::Webhook.token_delete_webhook(@token, @id, reason) else API::Webhook.delete_webhook(@bot.token, @id, reason) end end |
#delete_avatar ⇒ Object
Deletes the webhook's avatar.
62 63 64 |
# File 'lib/discordrb/data/webhook.rb', line 62 def delete_avatar update_webhook(avatar: nil) end |
#delete_message(message) ⇒ Object
Delete a message created by this webhook.
158 159 160 161 162 |
# File 'lib/discordrb/data/webhook.rb', line 158 def () raise Discordrb::Errors::UnauthorizedWebhook unless @token API::Webhook.(@token, @id, .resolve_id) end |
#edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, builder: nil, components: nil) {|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.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/discordrb/data/webhook.rb', line 175 def (, content: nil, embeds: nil, allowed_mentions: nil, builder: nil, components: nil) raise Discordrb::Errors::UnauthorizedWebhook unless @token params = { content: content, embeds: , 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 resp = API::Webhook.(@token, @id, .resolve_id, data[:content], data[:embeds], data[:allowed_mentions], components.to_a) Message.new(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) {|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.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/discordrb/data/webhook.rb', line 138 def execute(content: nil, username: nil, avatar_url: nil, tts: nil, file: nil, embeds: nil, allowed_mentions: nil, wait: true, builder: nil, components: nil) raise Discordrb::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 resp = API::Webhook.token_execute_webhook(@token, @id, wait, data[:content], data[:username], data[:avatar_url], data[:tts], data[:file], data[:embeds], data[:allowed_mentions], nil, components.to_a) Message.new(JSON.parse(resp), @bot) if wait end |
#inspect ⇒ Object
The inspect
method is overwritten to give more useful output.
201 202 203 |
# File 'lib/discordrb/data/webhook.rb', line 201 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.
207 208 209 |
# File 'lib/discordrb/data/webhook.rb', line 207 def token? @owner.nil? end |
#update(data) ⇒ Object
Updates the webhook if you need to edit more than 1 attribute.
84 85 86 87 88 89 90 |
# File 'lib/discordrb/data/webhook.rb', line 84 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 |