Class: MijDiscord::Data::Role

Inherits:
Object
  • Object
show all
Includes:
IDObject
Defined in:
lib/mij-discord/data/role.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, server, bot) ⇒ Role

Returns a new instance of Role.



23
24
25
26
27
28
29
30
31
# File 'lib/mij-discord/data/role.rb', line 23

def initialize(data, server, bot)
  @bot, @server = bot, server

  @permissions = Permissions.new
  @color = ColorRGB.new

  @id = data['id'].to_i
  update_data(data)
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



19
20
21
# File 'lib/mij-discord/data/role.rb', line 19

def color
  @color
end

#hoistObject (readonly)

Returns the value of attribute hoist.



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

def hoist
  @hoist
end

#managedObject (readonly) Also known as: managed?

Returns the value of attribute managed.



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

def managed
  @managed
end

#mentionableObject (readonly) Also known as: mentionable?

Returns the value of attribute mentionable.



16
17
18
# File 'lib/mij-discord/data/role.rb', line 16

def mentionable
  @mentionable
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



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

def permissions
  @permissions
end

#positionObject (readonly)

Returns the value of attribute position.



21
22
23
# File 'lib/mij-discord/data/role.rb', line 21

def position
  @position
end

Instance Method Details

#delete(reason = nil) ⇒ Object



87
88
89
# File 'lib/mij-discord/data/role.rb', line 87

def delete(reason = nil)
  MijDiscord::Core::API::Server.delete_role(@bot.auth, @server.id, @id, reason)
end

#inspectObject



97
98
99
100
# File 'lib/mij-discord/data/role.rb', line 97

def inspect
  MijDiscord.make_inspect(self,
    :id, :name, :color, :position, :hoist, :mentionable, :managed, :permissions)
end

#membersObject Also known as: users



53
54
55
# File 'lib/mij-discord/data/role.rb', line 53

def members
  @server.members.select {|x| x.role?(self) }
end

#mentionObject Also known as: to_s



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

def mention
  "<@&#{@id}>"
end

#set_color(color) ⇒ Object Also known as: color=



80
81
82
83
# File 'lib/mij-discord/data/role.rb', line 80

def set_color(color)
  set_options(color: color)
  nil
end

#set_hoist(flag) ⇒ Object Also known as: hoist=



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

def set_hoist(flag)
  set_options(hoist: flag)
  nil
end

#set_mentionable(flag) ⇒ Object Also known as: mentionable=



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

def set_mentionable(flag)
  set_options(mentionable: flag)
  nil
end

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



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

def set_name(name)
  set_options(name: name)
  nil
end

#set_options(name: nil, color: nil, hoist: nil, mentionable: nil, permissions: nil) ⇒ Object



91
92
93
94
95
# File 'lib/mij-discord/data/role.rb', line 91

def set_options(name: nil, color: nil, hoist: nil, mentionable: nil, permissions: nil)
  response = MijDiscord::Core::API::Server.update_role(@bot.auth, @server.id, @id,
    name, color&.to_i, hoist, mentionable, permissions&.to_i)
  @server.cache.put_role(JSON.parse(response), update: true)
end

#update_data(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mij-discord/data/role.rb', line 33

def update_data(data)
  @name = data.fetch('name', @name)
  @position = data.fetch('position', @position)
  @hoist = data.fetch('hoist', @hoist)
  @mentionable = data.fetch('mentionable', @mentionable)
  @managed = data.fetch('managed', @managed)

  @color.value = data.fetch('color', @color.value)

  if (bits = data['permissions'])
    @permissions.bits = bits.to_i
  end
end