Class: Discordrb::Webhook

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/discordrb/data.rb

Overview

A webhook on a server channel

Instance Attribute Summary collapse

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, synthesise

Constructor Details

#initialize(data, bot) ⇒ Webhook

Returns a new instance of Webhook.


3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
# File 'lib/discordrb/data.rb', line 3350

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

Instance Attribute Details

#avatarString

Returns the webhook's avatar id.

Returns:

  • (String)

    the webhook's avatar id.


3343
3344
3345
# File 'lib/discordrb/data.rb', line 3343

def avatar
  @avatar
end

#channelChannel

Returns the channel that the webhook is currently connected to.

Returns:

  • (Channel)

    the channel that the webhook is currently connected to.


3334
3335
3336
# File 'lib/discordrb/data.rb', line 3334

def channel
  @channel
end

#nameString

Returns the webhook name.

Returns:

  • (String)

    the webhook name.


3331
3332
3333
# File 'lib/discordrb/data.rb', line 3331

def name
  @name
end

#ownerMember, ... (readonly)

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

Returns:

  • (Member, User, nil)

    the user object of the owner or nil if the webhook was requested using the token.


3348
3349
3350
# File 'lib/discordrb/data.rb', line 3348

def owner
  @owner
end

#serverServer (readonly)

Returns the server that the webhook is currently connected to.

Returns:

  • (Server)

    the server that the webhook is currently connected to.


3337
3338
3339
# File 'lib/discordrb/data.rb', line 3337

def server
  @server
end

#tokenString (readonly)

Returns the webhook's token.

Returns:

  • (String)

    the webhook's token.


3340
3341
3342
# File 'lib/discordrb/data.rb', line 3340

def token
  @token
end

Instance Method Details

#avatar_urlString

Utility function to get a webhook's avatar URL

Returns:

  • (String)

    the URL to the avatar image


3417
3418
3419
3420
# File 'lib/discordrb/data.rb', line 3417

def avatar_url
  return API::User.default_avatar unless @avatar
  API::User.avatar_url(@id, @avatar)
end

#delete(reason = nil) ⇒ Object

Deletes the webhook

Parameters:

  • reason (String) (defaults to: nil)

    The reason the invite is being deleted.


3407
3408
3409
3410
3411
3412
3413
# File 'lib/discordrb/data.rb', line 3407

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_avatarObject

Deletes the webhook's avatar


3375
3376
3377
# File 'lib/discordrb/data.rb', line 3375

def delete_avatar
  update_webhook(avatar: nil)
end

#inspectObject

The inspect method is overwritten to give more useful output


3423
3424
3425
# File 'lib/discordrb/data.rb', line 3423

def inspect
  "<Webhook name=#{@name} id=#{@id}>"
end

#token?true, false

Utility function to know if the webhook was requested through a webhook token, rather than auth.

Returns:

  • (true, false)

    whether the webhook was requested by token or not.


3429
3430
3431
# File 'lib/discordrb/data.rb', line 3429

def token?
  @owner.nil?
end

#update(data) ⇒ Object

Updates the webhook if you need to edit more than 1 attribute

Parameters:

  • data (Hash)

    the data to update.

Options Hash (data):

  • :avatar (String, #read, nil)

    The new avatar, in base64-encoded JPG format, or nil to delete the avatar.

  • :channel (Channel, String, Integer, #resolve_id)

    The channel the webhook should use.

  • :name (String)

    The webhook's new name.

  • :reason (String)

    The reason for the webhook changes.


3397
3398
3399
3400
3401
3402
3403
# File 'lib/discordrb/data.rb', line 3397

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