class Discorb::Webhook
Represents a webhook. @abstract
Attributes
@return [Discorb::Snowflake] The application ID of the webhook. @return [nil] If the webhook is not an application webhook.
@return [Discorb::Asset] The avatar of the webhook.
@return [Discorb::Snowflake] The ID of the channel this webhook belongs to.
@return [Discorb::Snowflake] The ID of the guild this webhook belongs to.
@return [String] The name of the webhook.
@return [String] The URL of the webhook.
@return [Discorb::User] The user that created this webhook.
Public Class Methods
# File lib/discorb/webhook.rb, line 425 def from_url(url) URLWebhook.new(url) end
@!visibility private
# File lib/discorb/webhook.rb, line 26 def initialize(client, data) @name = data[:name] @guild_id = data[:guild_id] && Snowflake.new(data[:guild_id]) @channel_id = Snowflake.new(data[:channel_id]) @id = Snowflake.new(data[:id]) @user = data[:user] @name = data[:name] @avatar = Asset.new(self, data[:avatar]) @token = "" @application_id = data[:application_id] @client = client @http = Discorb::HTTP.new(client) end
Creates URLWebhook
.
@param [String] url The URL of the webhook.
@return [Discorb::Webhook::URLWebhook] The URLWebhook
.
# File lib/discorb/webhook.rb, line 405 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
Public Instance Methods
Deletes the webhook. @macro async @macro http
# File lib/discorb/webhook.rb, line 121 def delete! Async do @http.delete(url).wait self end end
Deletes the webhook's message.
@param [Discorb::Webhook::Message] message The message to delete.
# File lib/discorb/webhook.rb, line 179 def delete_message!(message) Async do @http.delete("#{url}/messages/#{Utils.try(message, :id)}").wait message end end
Edits the webhook. @macro async @macro http @macro edit
@param [String] name The new name of the webhook. @param [Discorb::Image] avatar The new avatar of the webhook. @param [Discorb::GuildChannel] channel The new channel of the webhook.
# 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
Edits the webhook's message. @macro async @macro http @macro edit
@param [Discorb::Webhook::Message] message The message to edit. @param [String] content The new content of the message. @param [Discorb::Embed] embed The new embed of the message. @param [Array<Discorb::Embed>] embeds The new embeds of the message. @param [Array<Discorb::Attachment>] attachments The attachments to remain. @param [Discorb::File] file The file to send. @param [Array<Discorb::File>] files The files to send. @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
# File lib/discorb/webhook.rb, line 145 def edit_message( message, 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] = embed ? [embed.to_hash] : [] if embed != :unset payload[:embeds] = embeds.map(&:to_hash) if embeds != :unset attachments = [attachment] if attachment != :unset payload[:attachments] = attachments.map(&:to_hash) if attachments != :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(message, :id)}", payload, headers: headers).wait message.send(:_set_data, data) message end end
# File lib/discorb/webhook.rb, line 40 def inspect "#<#{self.class} #{@name.inspect} id=#{@id}>" end
Posts a message to the webhook. @macro async @macro http
@param [String] content The content of the message. @param [Boolean] tts Whether the message should be sent as text-to-speech. @param [Discorb::Embed] embed The embed to send. @param [Array<Discorb::Embed>] embeds The embeds to send. @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send. @param [Discorb::File] file The file to send. @param [Array<Discorb::File>] files The files to send. @param [String] username The username of the message. @param [String] avatar_url The avatar URL of the message. @param [Boolean] wait Whether to wait for the message to be sent.
@return [Discorb::Webhook::Message] The message that was sent. @return [nil] If `wait` is false.
# 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 tmp_embed = if embed [embed] elsif embeds embeds end payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed 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