Class: MijDiscord::Data::Webhook

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/mij-discord/data/webhook.rb

Instance Attribute Summary collapse

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, #hash, synthesize, timestamp

Constructor Details

#initialize(data, bot) ⇒ Webhook

Returns a new instance of Webhook.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mij-discord/data/webhook.rb', line 19

def initialize(data, bot)
  @bot = bot

  @id = data['id'].to_id
  @token = data['token']
  update_data(data)

  if (user = data['user'])
    unless (@owner = server.member(user['id']))
      @owner = @bot.cache.put_user(user)
    end
  end
end

Instance Attribute Details

#avatar_idObject (readonly)

Returns the value of attribute avatar_id.



15
16
17
# File 'lib/mij-discord/data/webhook.rb', line 15

def avatar_id
  @avatar_id
end

#botObject (readonly)

Returns the value of attribute bot.



7
8
9
# File 'lib/mij-discord/data/webhook.rb', line 7

def bot
  @bot
end

#channelObject (readonly)

Returns the value of attribute channel.



11
12
13
# File 'lib/mij-discord/data/webhook.rb', line 11

def channel
  @channel
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/mij-discord/data/webhook.rb', line 9

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



17
18
19
# File 'lib/mij-discord/data/webhook.rb', line 17

def owner
  @owner
end

#tokenObject (readonly)

Returns the value of attribute token.



13
14
15
# File 'lib/mij-discord/data/webhook.rb', line 13

def token
  @token
end

Instance Method Details

#avatar_url(format = nil) ⇒ Object Also known as: avatar



47
48
49
50
# File 'lib/mij-discord/data/webhook.rb', line 47

def avatar_url(format = nil)
  return MijDiscord::Core::API::User.default_avatar(0) unless @avatar_id
  MijDiscord::Core::API::User.avatar_url(@id, @avatar_id, format)
end

#delete(reason = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/mij-discord/data/webhook.rb', line 79

def delete(reason = nil)
  if via_token?
    MijDiscord::Core::API::Webhook.token_delete_webhook(@token, @id, reason)
  else
    MijDiscord::Core::API::Webhook.delete_webhook(@bot.auth, @id, reason)
  end

  nil
end

#execute(text: '', name: nil, avatar: nil, tts: false, embeds: [], file: nil, wait: true) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mij-discord/data/webhook.rb', line 89

def execute(text: '', name: nil, avatar: nil, tts: false, embeds: [], file: nil, wait: true)
  raise 'Not yet implemented' unless file.nil? # TODO: Implement

  params = {
    content: text,
    username: name,
    avatar_url: avatar,
    tts: tts,
    embeds: embeds.map(&:to_hash),
  }.delete_if {|_,v| v.nil? }

  response = MijDiscord::Core::API::Webhook.execute_json(@token, @id, params, wait)
  wait ? Message.new(JSON.parse(response), @bot) : nil
end

#inspectObject



104
105
106
# File 'lib/mij-discord/data/webhook.rb', line 104

def inspect
  MijDiscord.make_inspect(self, :id, :name, :channel, :owner)
end

#serverObject



43
44
45
# File 'lib/mij-discord/data/webhook.rb', line 43

def server
  @channel.server
end

#set_avatar(data, format = :png, reason = nil) ⇒ Object Also known as: avatar=



72
73
74
75
# File 'lib/mij-discord/data/webhook.rb', line 72

def set_avatar(data, format = :png, reason = nil)
  data = User.process_avatar(data, format, true)
  update_webhook(reason, avatar: data)
end

#set_channel(channel, reason = nil) ⇒ Object Also known as: channel=



66
67
68
# File 'lib/mij-discord/data/webhook.rb', line 66

def set_channel(channel, reason = nil)
  update_webhook(reason, channel_id: channel.to_id)
end

#set_name(name, reason = nil) ⇒ Object Also known as: name=



60
61
62
# File 'lib/mij-discord/data/webhook.rb', line 60

def set_name(name, reason = nil)
  update_webhook(reason, name: name)
end

#set_options(reason = nil, name: nil, channel: nil, avatar: nil, format: :png) ⇒ Object



54
55
56
57
58
# File 'lib/mij-discord/data/webhook.rb', line 54

def set_options(reason = nil, name: nil, channel: nil, avatar: nil, format: :png)
  data = {name: name, channel_id: channel&.to_id}
  data[:avatar] = User.process_avatar(avatar, format, true) unless avatar.nil?
  update_webhook(reason, **data.delete_if {|_,v| v.nil? })
end

#update_data(data) ⇒ Object



33
34
35
36
37
# File 'lib/mij-discord/data/webhook.rb', line 33

def update_data(data)
  @name = data['name']
  @channel = @bot.channel(data['channel_id'])
  @avatar_id = data['avatar']
end

#via_token?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/mij-discord/data/webhook.rb', line 39

def via_token?
  @owner.nil?
end