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_idDiscorb::Snowflake? (readonly)

Returns:

  • The application ID of the webhook.

  • If the webhook is not an application webhook.



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

def application_id
  @application_id
end

#avatarDiscorb::Asset (readonly)

Returns The avatar of the webhook.

Returns:

  • The avatar of the webhook.



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

def avatar
  @avatar
end

#channel_idDiscorb::Snowflake (readonly)

Returns The ID of the channel this webhook belongs to.

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_idDiscorb::Snowflake (readonly)

Returns The ID of the guild this webhook belongs to.

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

#nameString (readonly)

Returns The name of the webhook.

Returns:

  • The name of the webhook.



10
11
12
# File 'lib/discorb/webhook.rb', line 10

def name
  @name
end

#tokenString (readonly)

Returns The URL of the webhook.

Returns:

  • The URL of the webhook.



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

def token
  @token
end

#userDiscorb::User (readonly)

Returns The user that created this webhook.

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



469
470
471
# File 'lib/discorb/webhook.rb', line 469

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

.new(url) ⇒ Discorb::Webhook::URLWebhook

Creates URLWebhook.

Parameters:

  • The URL of the webhook.

Returns:

  • The URLWebhook.



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/discorb/webhook.rb', line 449

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!Async::Task<void> Also known as: destroy!

Deletes the webhook.

Returns:

  • The task.



120
121
122
123
124
125
# File 'lib/discorb/webhook.rb', line 120

def delete!
  Async do
    @http.request(Route.new(url, "//webhooks/:webhook_id/:token", :delete)).wait
    self
  end
end

#delete_message!(message) ⇒ Async::Task<void>

Deletes the webhook's message.

Parameters:

  • The message to delete.

Returns:

  • The task.



173
174
175
176
177
178
179
180
181
# File 'lib/discorb/webhook.rb', line 173

def delete_message!(message)
  Async do
    @http.request(Route.new(
      "#{url}/messages/#{Utils.try(message, :id)}",
      "//webhooks/:webhook_id/:token/messages/:message_id", :delete
    )).wait
    message
  end
end

#edit(name: Discorb::Unset, avatar: Discorb::Unset, channel: Discorb::Unset) ⇒ Async::Task<void> Also known as: modify

Note:

The arguments of this method are defaultly set to Discorb::Unset. Specify value to set the value, if not don't specify or specify Discorb::Unset.

Edits the webhook.

Parameters:

  • (defaults to: Discorb::Unset)

    The new name of the webhook.

  • (defaults to: Discorb::Unset)

    The new avatar of the webhook.

  • (defaults to: Discorb::Unset)

    The new channel of the webhook.

Returns:

  • The task.



102
103
104
105
106
107
108
109
110
# File 'lib/discorb/webhook.rb', line 102

def edit(name: Discorb::Unset, avatar: Discorb::Unset, channel: Discorb::Unset)
  Async do
    payload = {}
    payload[:name] = name if name != Discorb::Unset
    payload[:avatar] = avatar if avatar != Discorb::Unset
    payload[:channel_id] = Utils.try(channel, :id) if channel != Discorb::Unset
    @http.request(Route.new(url, "//webhooks/:webhook_id/:token", :patch), payload).wait
  end
end

#edit_message(message, content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, file: Discorb::Unset, files: Discorb::Unset, attachments: Discorb::Unset, allowed_mentions: Discorb::Unset) ⇒ Async::Task<void>

Note:

The arguments of this method are defaultly set to Discorb::Unset. Specify value to set the value, if not don't specify or specify Discorb::Unset.

Edits the webhook's message.

Parameters:

  • The message to edit.

  • (defaults to: Discorb::Unset)

    The new content of the message.

  • (defaults to: Discorb::Unset)

    The new embed of the message.

  • (defaults to: Discorb::Unset)

    The new embeds of the message.

  • (defaults to: Discorb::Unset)

    The attachments to remain.

  • (defaults to: Discorb::Unset)

    The file to send.

  • (defaults to: Discorb::Unset)

    The files to send.

  • (defaults to: Discorb::Unset)

    The allowed mentions to send.

Returns:

  • The task.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/discorb/webhook.rb', line 145

def edit_message(
  message, content = Discorb::Unset,
  embed: Discorb::Unset, embeds: Discorb::Unset,
  file: Discorb::Unset, files: Discorb::Unset,
  attachments: Discorb::Unset,
  allowed_mentions: Discorb::Unset
)
  Async do
    payload = {}
    payload[:content] = content if content != Discorb::Unset
    payload[:embeds] = embed ? [embed.to_hash] : [] if embed != Discorb::Unset
    payload[:embeds] = embeds.map(&:to_hash) if embeds != Discorb::Unset
    payload[:attachments] = attachments.map(&:to_hash) if attachments != Discorb::Unset
    payload[:allowed_mentions] = allowed_mentions if allowed_mentions != Discorb::Unset
    files = [file] if file != Discorb::Unset
    _resp, data = @http.multipart_request(Route.new("#{url}/messages/#{Utils.try(message, :id)}", "//webhooks/:webhook_id/:token/messages/:message_id", :patch), payload, files).wait
    message.send(:_set_data, data)
    message
  end
end

#inspectObject



46
47
48
# File 'lib/discorb/webhook.rb', line 46

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: Discorb::Unset, wait: true) ⇒ Discorb::Webhook::Message, Async::Task<nil> Also known as: execute

Posts a message to the webhook.

Parameters:

  • (defaults to: nil)

    The content of the message.

  • (defaults to: false)

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

  • (defaults to: nil)

    The embed to send.

  • (defaults to: nil)

    The embeds to send.

  • (defaults to: nil)

    The allowed mentions to send.

  • (defaults to: nil)

    The file to send.

  • (defaults to: nil)

    The files to send.

  • (defaults to: nil)

    The username of the message.

  • (defaults to: Discorb::Unset)

    The avatar URL of the message.

  • (defaults to: true)

    Whether to wait for the message to be sent.

Returns:

  • The message that was sent.

  • If wait is false.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/discorb/webhook.rb', line 68

def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil,
                        file: nil, files: nil, username: nil, avatar_url: Discorb::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 != Discorb::Unset
    files = [file]
    _resp, data = @http.multipart_request(Route.new("#{url}?wait=#{wait}", "//webhooks/:webhook_id/:token", :post), files, payload, headers: headers).wait
    data && Webhook::Message.new(self, data)
  end
end