class Discordrb::Webhook

A webhook on a server channel

Attributes

avatar[R]

@return [String] the webhook's avatar id.

channel[R]

@return [Channel] the channel that the webhook is currently connected to.

name[R]

@return [String] the webhook name.

owner[R]

Gets the user object of the creator of the webhook. May be limited to username, discriminator, ID and avatar if the bot cannot reach the owner @return [Member, User, nil] the user object of the owner or nil if the webhook was requested using the token.

server[R]

@return [Server] the server that the webhook is currently connected to.

token[R]

@return [String] the webhook's token.

Public Class Methods

new(data, bot) click to toggle source
# File lib/discordrb/data.rb, line 3766
def initialize(data, bot)
  @bot = bot

  @name = data['name']
  @id = data['id'].to_i
  @channel = bot.channel(data['channel_id'])
  @server = @channel.server
  @token = data['token']
  @avatar = data['avatar']

  # Will not exist if the data was requested through a webhook token
  return unless data['user']

  @owner = @server.member(data['user']['id'].to_i)
  return if @owner

  Discordrb::LOGGER.debug("Member with ID #{data['user']['id']} not cached (possibly left the server).")
  @owner = @bot.ensure_user(data['user'])
end

Public Instance Methods

avatar=(avatar) click to toggle source

Sets the webhook's avatar. @param avatar [String, read] The new avatar, in base64-encoded JPG format.

# File lib/discordrb/data.rb, line 3788
def avatar=(avatar)
  update_webhook(avatar: avatarise(avatar))
end
avatar_url() click to toggle source

Utility function to get a webhook's avatar URL. @return [String] the URL to the avatar image

# File lib/discordrb/data.rb, line 3835
def avatar_url
  return API::User.default_avatar unless @avatar

  API::User.avatar_url(@id, @avatar)
end
channel=(channel) click to toggle source

Sets the webhook's channel @param channel [Channel, String, Integer, resolve_id] The channel the webhook should use.

# File lib/discordrb/data.rb, line 3799
def channel=(channel)
  update_webhook(channel_id: channel.resolve_id)
end
delete(reason = nil) click to toggle source

Deletes the webhook. @param reason [String] The reason the invite is being deleted.

# File lib/discordrb/data.rb, line 3825
def delete(reason = nil)
  if token?
    API::Webhook.token_delete_webhook(@token, @id, reason)
  else
    API::Webhook.delete_webhook(@bot.token, @id, reason)
  end
end
delete_avatar() click to toggle source

Deletes the webhook's avatar.

# File lib/discordrb/data.rb, line 3793
def delete_avatar
  update_webhook(avatar: nil)
end
inspect() click to toggle source

The `inspect` method is overwritten to give more useful output.

# File lib/discordrb/data.rb, line 3842
def inspect
  "<Webhook name=#{@name} id=#{@id}>"
end
name=(name) click to toggle source

Sets the webhook's name. @param name [String] The webhook's new name.

# File lib/discordrb/data.rb, line 3805
def name=(name)
  update_webhook(name: name)
end
token?() click to toggle source

Utility function to know if the webhook was requested through a webhook token, rather than auth. @return [true, false] whether the webhook was requested by token or not.

# File lib/discordrb/data.rb, line 3848
def token?
  @owner.nil?
end
update(data) click to toggle source

Updates the webhook if you need to edit more than 1 attribute. @param data [Hash] the data to update. @option data [String, read, nil] :avatar The new avatar, in base64-encoded JPG format, or nil to delete the avatar. @option data [Channel, String, Integer, resolve_id] :channel The channel the webhook should use. @option data [String] :name The webhook's new name. @option data [String] :reason The reason for the webhook changes.

# File lib/discordrb/data.rb, line 3815
def update(data)
  # Only pass a value for avatar if the key is defined as sending nil will delete the
  data[:avatar] = avatarise(data[:avatar]) if data.key?(:avatar)
  data[:channel_id] = data[:channel].resolve_id
  data.delete(:channel)
  update_webhook(data)
end

Private Instance Methods

avatarise(avatar) click to toggle source
# File lib/discordrb/data.rb, line 3854
def avatarise(avatar)
  if avatar.respond_to? :read
    "data:image/jpg;base64,#{Base64.strict_encode64(avatar.read)}"
  else
    avatar
  end
end
update_internal(data) click to toggle source
# File lib/discordrb/data.rb, line 3862
def update_internal(data)
  @name = data['name']
  @avatar_id = data['avatar']
  @channel = @bot.channel(data['channel_id'])
end
update_webhook(new_data) click to toggle source
# File lib/discordrb/data.rb, line 3868
def update_webhook(new_data)
  reason = new_data.delete(:reason)
  data = JSON.parse(if token?
                      API::Webhook.token_update_webhook(@token, @id, new_data, reason)
                    else
                      API::Webhook.update_webhook(@bot.token, @id, new_data, reason)
                    end)
  # Only update cache if API call worked
  update_internal(data) if data['name']
end