Class: Discorb::Webhook Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/webhook.rb

Overview

This class is abstract.

Represents a webhook.

Defined Under Namespace

Classes: ApplicationWebhook, FollowerWebhook, IncomingWebhook, Message, URLWebhook

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#application_id -> Discorb::Snowflake? (readonly)

Returns:

  • (Discorb::Snowflake)

    The application ID of the webhook.

  • (nil)

    If the webhook is not an application webhook.



23
24
25
# File 'lib/discorb/webhook.rb', line 23

def application_id
  @application_id
end

#avatar -> Discorb::Asset (readonly)

Returns The avatar of the webhook.

Returns:



20
21
22
# File 'lib/discorb/webhook.rb', line 20

def avatar
  @avatar
end

#channel_id -> Discorb::Snowflake (readonly)

Returns The ID of the channel this webhook belongs to.

Returns:



16
17
18
# File 'lib/discorb/webhook.rb', line 16

def channel_id
  @channel_id
end

#guild_id -> Discorb::Snowflake (readonly)

Returns The ID of the guild this webhook belongs to.

Returns:



14
15
16
# File 'lib/discorb/webhook.rb', line 14

def guild_id
  @guild_id
end

#name -> String (readonly)

Returns The name of the webhook.

Returns:

  • (String)

    The name of the webhook.



12
13
14
# File 'lib/discorb/webhook.rb', line 12

def name
  @name
end

#token -> String (readonly)

Returns The URL of the webhook.

Returns:

  • (String)

    The URL of the webhook.



25
26
27
# File 'lib/discorb/webhook.rb', line 25

def token
  @token
end

#user -> Discorb::User (readonly)

Returns The user that created this webhook.

Returns:



18
19
20
# File 'lib/discorb/webhook.rb', line 18

def user
  @user
end

Class Method Details

.from_url(url) -> Object



416
417
418
# File 'lib/discorb/webhook.rb', line 416

def from_url(url)
  URLWebhook.new(url)
end

.new(url) -> Discorb::Webhook::URLWebhook

Creates URLWebhook.

Parameters:

  • url (String)

    The URL of the webhook.

Returns:



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/discorb/webhook.rb', line 396

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!

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Deletes the webhook.

Raises:



123
124
125
126
127
128
# File 'lib/discorb/webhook.rb', line 123

def delete!
  Async do
    @internet.delete(url).wait
    self
  end
end

#delete_message!(message) -> Object

Deletes the webhook's message.

Parameters:



181
182
183
184
185
186
# File 'lib/discorb/webhook.rb', line 181

def delete_message!(message)
  Async do
    @internet.delete("#{url}/messages/#{Utils.try(message, :id)}").wait
    message
  end
end

#edit(name: :unset, avatar: :unset, channel: :unset) -> Object Also known as: modify

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Note:

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.

Parameters:

  • name (String) (defaults to: :unset)

    The new name of the webhook.

  • avatar (Discorb::Image) (defaults to: :unset)

    The new avatar of the webhook.

  • channel (Discorb::GuildChannel) (defaults to: :unset)

    The new channel of the webhook.

Raises:



106
107
108
109
110
111
112
113
114
# File 'lib/discorb/webhook.rb', line 106

def edit(name: :unset, avatar: :unset, channel: :unset)
  Async do |_task|
    payload = {}
    payload[:name] = name if name != :unset
    payload[:avatar] = avatar if avatar != :unset
    payload[:channel_id] = Utils.try(channel, :id) if channel != :unset
    @internet.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

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Note:

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.

Parameters:

  • message (Discorb::Webhook::Message)

    The message to edit.

  • content (String) (defaults to: :unset)

    The new content of the message.

  • embed (Discorb::Embed) (defaults to: :unset)

    The new embed of the message.

  • embeds (Array<Discorb::Embed>) (defaults to: :unset)

    The new embeds of the message.

  • attachments (Array<Discorb::Attachment>) (defaults to: :unset)

    The attachments to remain.

  • file (Discorb::File) (defaults to: :unset)

    The file to send.

  • files (Array<Discorb::File>) (defaults to: :unset)

    The files to send.

  • allowed_mentions (Discorb::AllowedMentions) (defaults to: :unset)

    The allowed mentions to send.

Raises:



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
173
174
# File 'lib/discorb/webhook.rb', line 147

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 = Internet.multipart(payload, files)
    end
    _resp, data = @internet.patch("#{url}/messages/#{Utils.try(message, :id)}", payload, headers: headers).wait
    message.send(:_set_data, data)
    message
  end
end

#inspect -> Object



42
43
44
# File 'lib/discorb/webhook.rb', line 42

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

Note:

This is an asynchronous method, it will return a Async::Task object. Use Async::Task#wait to get the result.

Note:

This method calls HTTP request.

Posts a message to the webhook.

Parameters:

  • content (String) (defaults to: nil)

    The content of the message.

  • tts (Boolean) (defaults to: false)

    Whether the message should be sent as text-to-speech.

  • embed (Discorb::Embed) (defaults to: nil)

    The embed to send.

  • embeds (Array<Discorb::Embed>) (defaults to: nil)

    The embeds to send.

  • allowed_mentions (Discorb::AllowedMentions) (defaults to: nil)

    The allowed mentions to send.

  • file (Discorb::File) (defaults to: nil)

    The file to send.

  • files (Array<Discorb::File>) (defaults to: nil)

    The files to send.

  • username (String) (defaults to: nil)

    The username of the message.

  • avatar_url (String) (defaults to: :unset)

    The avatar URL of the message.

  • wait (Boolean) (defaults to: true)

    Whether to wait for the message to be sent.

Returns:

Raises:



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
91
92
# File 'lib/discorb/webhook.rb', line 65

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 |_task|
    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 = Internet.multipart(payload, files)
    else
      headers = {
        "Content-Type" => "application/json",
      }
    end
    _resp, data = @internet.post("#{url}?wait=#{wait}", payload, headers: headers).wait

    data && Webhook::Message.new(self, data)
  end
end