Class: RubyCord::Guild::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycord/guild/webhook.rb,
lib/rubycord/guild/webhook/message.rb

Defined Under Namespace

Classes: Message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#application_idRubyCord::Snowflake? (readonly)

Returns:

  • (RubyCord::Snowflake)

    The application ID of the webhook.

  • (nil)

    If the webhook is not an application webhook.



22
23
24
# File 'lib/rubycord/guild/webhook.rb', line 22

def application_id
  @application_id
end

#avatarRubyCord::Asset (readonly)

Returns The avatar of the webhook.

Returns:



19
20
21
# File 'lib/rubycord/guild/webhook.rb', line 19

def avatar
  @avatar
end

#channel_idRubyCord::Snowflake (readonly)

Returns The ID of the channel this webhook belongs to.

Returns:



15
16
17
# File 'lib/rubycord/guild/webhook.rb', line 15

def channel_id
  @channel_id
end

#guild_idRubyCord::Snowflake (readonly)

Returns The ID of the guild this webhook belongs to.

Returns:



13
14
15
# File 'lib/rubycord/guild/webhook.rb', line 13

def guild_id
  @guild_id
end

#nameString (readonly)

Returns The name of the webhook.

Returns:

  • (String)

    The name of the webhook.



11
12
13
# File 'lib/rubycord/guild/webhook.rb', line 11

def name
  @name
end

#tokenString (readonly)

Returns The URL of the webhook.

Returns:

  • (String)

    The URL of the webhook.



24
25
26
# File 'lib/rubycord/guild/webhook.rb', line 24

def token
  @token
end

#userRubyCord::User (readonly)

Returns The user that created this webhook.

Returns:



17
18
19
# File 'lib/rubycord/guild/webhook.rb', line 17

def user
  @user
end

Class Method Details

.from_url(url) ⇒ Object



250
251
252
# File 'lib/rubycord/guild/webhook.rb', line 250

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

Instance Method Details

#deleteAsync::Task<void> Also known as: destroy

Deletes the webhook.

Returns:

  • (Async::Task<void>)

    The task.



146
147
148
149
150
151
152
153
# File 'lib/rubycord/guild/webhook.rb', line 146

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

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

Deletes the webhook's message.

Parameters:

Returns:

  • (Async::Task<void>)

    The task.



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/rubycord/guild/webhook.rb', line 216

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

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

Note:

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

Edits the webhook.

Parameters:

  • name (String) (defaults to: RubyCord::Unset)

    The new name of the webhook.

  • avatar (RubyCord::Image) (defaults to: RubyCord::Unset)

    The new avatar of the webhook.

  • channel (RubyCord::Guild::Channel) (defaults to: RubyCord::Unset)

    The new channel of the webhook.

Returns:

  • (Async::Task<void>)

    The task.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rubycord/guild/webhook.rb', line 120

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

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

Note:

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

Edits the webhook's message.

Parameters:

Returns:

  • (Async::Task<void>)

    The task.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rubycord/guild/webhook.rb', line 173

def edit_message(
  message,
  content = RubyCord::Unset,
  embed: RubyCord::Unset,
  embeds: RubyCord::Unset,
  file: RubyCord::Unset,
  files: RubyCord::Unset,
  attachments: RubyCord::Unset,
  allowed_mentions: RubyCord::Unset
)
  Async do
    payload = {}
    payload[:content] = content if content != RubyCord::Unset
    payload[:embeds] = embed ? [embed.to_hash] : [] if embed !=
      RubyCord::Unset
    payload[:embeds] = embeds.map(&:to_hash) if embeds != RubyCord::Unset
    payload[:attachments] = attachments.map(&:to_hash) if attachments !=
      RubyCord::Unset
    payload[:allowed_mentions] = allowed_mentions if allowed_mentions !=
      RubyCord::Unset
    files = [file] if file != RubyCord::Unset
    _resp, data =
      @http.multipart_request(
        RubyCord::Internal::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



47
48
49
# File 'lib/rubycord/guild/webhook.rb', line 47

def inspect
  "#<#{self.class} #{@name.inspect} id=#{@id}>"
end

#post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil, attachments: nil, username: nil, avatar_url: RubyCord::Unset, wait: true) ⇒ RubyCord::Guild::Webhook::Message, Async::Task<nil> Also known as: execute

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 (RubyCord::Embed) (defaults to: nil)

    The embed to send.

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

    The embeds to send.

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

    The allowed mentions to send.

  • attachments (Array<RubyCord::Attachment>) (defaults to: nil)

    The attachments to send.

  • username (String) (defaults to: nil)

    The username of the message.

  • avatar_url (String) (defaults to: RubyCord::Unset)

    The avatar URL of the message.

  • wait (Boolean) (defaults to: true)

    Whether to wait for the message to be sent.

Returns:



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
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rubycord/guild/webhook.rb', line 68

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