Class: Rubord::Guild
- Inherits:
-
Object
- Object
- Rubord::Guild
- Defined in:
- lib/rubord/models/guild.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
- #add_member(data) ⇒ Object
- #fetch_member(user_id) ⇒ Object
- #fetch_owner ⇒ Object
-
#initialize(data, client) ⇒ Guild
constructor
A new instance of Guild.
- #inspect ⇒ Object
- #is_owner?(user_id) ⇒ Boolean
- #load_roles(raw_roles) ⇒ Object
- #members_count ⇒ Object
- #owner_id ⇒ Object
Constructor Details
#initialize(data, client) ⇒ Guild
Returns a new instance of Guild.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rubord/models/guild.rb', line 8 def initialize(data, client) @client = client @id = data["id"] @members = Rubord::Collection.new @roles = Rubord::Collection.new @owner = data["owner"] || false @owner_id = data["owner_id"] client.guilds.set(@id, self) load_roles(data["roles"]) if data["roles"] end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
3 4 5 |
# File 'lib/rubord/models/guild.rb', line 3 def client @client end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/rubord/models/guild.rb', line 3 def id @id end |
#members ⇒ Object (readonly)
Returns the value of attribute members.
3 4 5 |
# File 'lib/rubord/models/guild.rb', line 3 def members @members end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
3 4 5 |
# File 'lib/rubord/models/guild.rb', line 3 def roles @roles end |
Instance Method Details
#add_member(data) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/rubord/models/guild.rb', line 52 def add_member(data) member = Rubord::Member.new( data.merge("guild_id" => @id), @client ) @members.set(member.id, member) member end |
#fetch_member(user_id) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rubord/models/guild.rb', line 62 def fetch_member(user_id) cached = @members.get(user_id) return cached if cached data = @client.rest.get_guild_member(@id, user_id) return nil unless data add_member(data) rescue Rubord::HTTPError nil end |
#fetch_owner ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rubord/models/guild.rb', line 28 def fetch_owner return nil unless @owner_id @client.users.get(@owner_id) || begin data = @client.rest.get_user(@owner_id) user = Rubord::User.new(data) @client.users.set(user.id, user) user rescue Rubord::HTTPError nil end end |
#inspect ⇒ Object
74 75 76 |
# File 'lib/rubord/models/guild.rb', line 74 def inspect "#<Rubord::Guild id=#{@id} members=#{@members.size} roles=#{@roles.size}>" end |
#is_owner?(user_id) ⇒ Boolean
20 21 22 |
# File 'lib/rubord/models/guild.rb', line 20 def is_owner?(user_id) @owner_id.to_i == user_id.to_i end |
#load_roles(raw_roles) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/rubord/models/guild.rb', line 45 def load_roles(raw_roles) raw_roles.each do |role_data| role = Rubord::Role.new(role_data) @roles.set(role.id, role) end end |
#members_count ⇒ Object
41 42 43 |
# File 'lib/rubord/models/guild.rb', line 41 def members_count @members.size end |
#owner_id ⇒ Object
24 25 26 |
# File 'lib/rubord/models/guild.rb', line 24 def owner_id @owner_id end |