Class: Discordrb::Webhooks::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/webhooks/client.rb

Overview

A client for a particular webhook added to a Discord channel.

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, id: nil, token: nil) ⇒ Client

Create a new webhook

Parameters:

  • url (String) (defaults to: nil)

    The URL to post messages to.

  • id (Integer) (defaults to: nil)

    The webhook's ID. Will only be used if url is not set.

  • token (String) (defaults to: nil)

    The webhook's authorisation token. Will only be used if url is not set.



17
18
19
# File 'lib/discordrb/webhooks/client.rb', line 17

def initialize(url: nil, id: nil, token: nil)
  @url = url || generate_url(id, token)
end

Instance Method Details

#delete(reason: nil) ⇒ RestClient::Response

Note:

This is permanent and cannot be undone.

Delete this webhook.

Parameters:

  • reason (String, nil) (defaults to: nil)

    The reason this webhook was deleted.

Returns:

  • (RestClient::Response)

    the response returned by Discord.



69
70
71
# File 'lib/discordrb/webhooks/client.rb', line 69

def delete(reason: nil)
  RestClient.delete(@url, 'X-Audit-Log-Reason': reason)
end

#delete_message(message_id) ⇒ RestClient::Response

Delete a message created by this webhook.

Parameters:

  • message_id (String, Integer)

    The ID of the message to delete.

Returns:

  • (RestClient::Response)

    the response returned by Discord.



101
102
103
# File 'lib/discordrb/webhooks/client.rb', line 101

def delete_message(message_id)
  RestClient.delete("#{@url}/messages/#{message_id}")
end

#edit_message(message_id, builder: nil, content: nil, embeds: nil, allowed_mentions: nil) {|builder| ... } ⇒ RestClient::Response

Note:

Not all builder options are available when editing.

Edit a message from this webhook.

Examples:

Edit message content

client.edit_message(message_id, content: 'goodbye world!')

Edit a message via builder

client.edit_message(message_id) do |builder|
  builder.add_embed do |e|
    e.description = 'Hello World!'
  end
end

Parameters:

  • message_id (String, Integer)

    The ID of the message to edit.

  • builder (Builder, nil) (defaults to: nil)

    The builder to start out with, or nil if one should be created anew.

  • content (String) (defaults to: nil)

    The message content.

  • embeds (Array<Embed, Hash>) (defaults to: nil)
  • allowed_mentions (Hash) (defaults to: nil)

Yields:

  • (builder)

Returns:

  • (RestClient::Response)

    the response returned by Discord.



89
90
91
92
93
94
95
96
# File 'lib/discordrb/webhooks/client.rb', line 89

def edit_message(message_id, builder: nil, content: nil, embeds: nil, allowed_mentions: nil)
  builder ||= Builder.new

  yield builder if block_given?

  data = builder.to_json_hash.merge({ content: content, embeds: embeds, allowed_mentions: allowed_mentions }.compact)
  RestClient.patch("#{@url}/messages/#{message_id}", data.compact.to_json, content_type: :json)
end

#execute(builder = nil, wait = false) {|builder| ... } ⇒ RestClient::Response

Executes the webhook this client points to with the given data.

Examples:

Execute the webhook with an already existing builder

builder = Discordrb::Webhooks::Builder.new # ...
client.execute(builder)

Execute the webhook by building a new message

client.execute do |builder|
  builder.content = 'Testing'
  builder.username = 'discordrb'
  builder.add_embed do |embed|
    embed.timestamp = Time.now
    embed.title = 'Testing'
    embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
  end
end

Parameters:

  • builder (Builder, nil) (defaults to: nil)

    The builder to start out with, or nil if one should be created anew.

  • wait (true, false) (defaults to: false)

    Whether Discord should wait for the message to be successfully received by clients, or whether it should return immediately after sending the message.

Yields:

  • (builder)

    Gives the builder to the block to add additional steps, or to do the entire building process.

Yield Parameters:

  • builder (Builder)

    The builder given as a parameter which is used as the initial step to start from.

Returns:

  • (RestClient::Response)

    the response returned by Discord.

Raises:

  • (TypeError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/discordrb/webhooks/client.rb', line 41

def execute(builder = nil, wait = false)
  raise TypeError, 'builder needs to be nil or like a Discordrb::Webhooks::Builder!' unless
    (builder.respond_to?(:file) && builder.respond_to?(:to_multipart_hash)) || builder.respond_to?(:to_json_hash) || builder.nil?

  builder ||= Builder.new

  yield builder if block_given?

  if builder.file
    post_multipart(builder, wait)
  else
    post_json(builder, wait)
  end
end

#modify(name: nil, avatar: nil, channel_id: nil) ⇒ RestClient::Response

Modify this webhook's properties.

Parameters:

  • name (String, nil) (defaults to: nil)

    The default name.

  • avatar (String, #read, nil) (defaults to: nil)

    The new avatar, in base64-encoded JPG format.

  • channel_id (String, Integer, nil) (defaults to: nil)

    The channel to move the webhook to.

Returns:

  • (RestClient::Response)

    the response returned by Discord.



61
62
63
# File 'lib/discordrb/webhooks/client.rb', line 61

def modify(name: nil, avatar: nil, channel_id: nil)
  RestClient.patch(@url, { name: name, avatar: avatarise(avatar), channel_id: channel_id }.compact.to_json, content_type: :json)
end