Class: Discordrb::Server
- Inherits:
-
Object
- Object
- Discordrb::Server
- Defined in:
- lib/discordrb/data.rb
Overview
A server on Discord
Instance Attribute Summary collapse
-
#afk_channel_id ⇒ Object
Returns the value of attribute afk_channel_id.
-
#afk_timeout ⇒ Object
Returns the value of attribute afk_timeout.
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#icon ⇒ Object
Returns the value of attribute icon.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#name ⇒ Object
Returns the value of attribute name.
-
#owner_id ⇒ Object
readonly
Returns the value of attribute owner_id.
-
#region ⇒ Object
Returns the value of attribute region.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
-
#==(other) ⇒ Object
ID based comparison.
- #add_role(role) ⇒ Object
- #add_user(user) ⇒ Object
- #afk_channel=(afk_channel) ⇒ Object
- #ban(user, message_days = 0) ⇒ Object
- #create_channel(name) ⇒ Object
- #create_role ⇒ Object
- #delete ⇒ Object (also: #leave)
- #delete_role(role_id) ⇒ Object
- #delete_user(user_id) ⇒ Object
-
#initialize(data, bot) ⇒ Server
constructor
A new instance of Server.
- #kick(user) ⇒ Object
- #process_channels(channels) ⇒ Object
- #process_members(members) ⇒ Object
- #process_presences(presences) ⇒ Object
- #process_roles(roles) ⇒ Object
- #process_voice_states(voice_states) ⇒ Object
- #role(id) ⇒ Object
- #unban(user) ⇒ Object
- #update_data(new_data) ⇒ Object
Constructor Details
#initialize(data, bot) ⇒ Server
Returns a new instance of Server.
507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/discordrb/data.rb', line 507 def initialize(data, bot) @bot = bot @owner_id = data['owner_id'].to_i @id = data['id'].to_i update_data(data) process_roles(data['roles']) process_members(data['members']) process_presences(data['presences']) process_channels(data['channels']) process_voice_states(data['voice_states']) end |
Instance Attribute Details
#afk_channel_id ⇒ Object
Returns the value of attribute afk_channel_id.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def afk_channel_id @afk_channel_id end |
#afk_timeout ⇒ Object
Returns the value of attribute afk_timeout.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def afk_timeout @afk_timeout end |
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def channels @channels end |
#icon ⇒ Object
Returns the value of attribute icon.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def icon @icon end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def id @id end |
#members ⇒ Object (readonly)
Returns the value of attribute members.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def members @members end |
#name ⇒ Object
Returns the value of attribute name.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def name @name end |
#owner_id ⇒ Object (readonly)
Returns the value of attribute owner_id.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def owner_id @owner_id end |
#region ⇒ Object
Returns the value of attribute region.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def region @region end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
505 506 507 |
# File 'lib/discordrb/data.rb', line 505 def roles @roles end |
Instance Method Details
#==(other) ⇒ Object
ID based comparison
521 522 523 |
# File 'lib/discordrb/data.rb', line 521 def ==(other) Discordrb.id_compare(@id, other) end |
#add_role(role) ⇒ Object
600 601 602 |
# File 'lib/discordrb/data.rb', line 600 def add_role(role) @roles << role end |
#add_user(user) ⇒ Object
616 617 618 |
# File 'lib/discordrb/data.rb', line 616 def add_user(user) @members << user end |
#afk_channel=(afk_channel) ⇒ Object
666 667 668 |
# File 'lib/discordrb/data.rb', line 666 def afk_channel=(afk_channel) update_server_data(afk_channel_id: afk_channel.id) end |
#ban(user, message_days = 0) ⇒ Object
636 637 638 |
# File 'lib/discordrb/data.rb', line 636 def ban(user, = 0) API.ban_user(@bot.token, @id, user.id, ) end |
#create_channel(name) ⇒ Object
624 625 626 627 |
# File 'lib/discordrb/data.rb', line 624 def create_channel(name) response = API.create_channel(@bot.token, @id, name, 'text') Channel.new(JSON.parse(response), @bot) end |
#create_role ⇒ Object
629 630 631 632 633 634 |
# File 'lib/discordrb/data.rb', line 629 def create_role response = API.create_role(@bot.token, @id) role = Role.new(JSON.parse(response), @bot) @roles << role role end |
#delete ⇒ Object Also known as: leave
648 649 650 |
# File 'lib/discordrb/data.rb', line 648 def delete API.delete_server(@bot.token, @id) end |
#delete_role(role_id) ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/discordrb/data.rb', line 604 def delete_role(role_id) @roles.reject! { |r| r.id == role_id } @members.each do |user| new_roles = user.roles[@id].reject { |r| r.id == role_id } user.update_roles(self, new_roles) end @channels.each do |channel| overwrites = channel..reject { |id, _| id == role_id } channel.update_overwrites(overwrites) end end |
#delete_user(user_id) ⇒ Object
620 621 622 |
# File 'lib/discordrb/data.rb', line 620 def delete_user(user_id) @members.reject! { |member| member.id == user_id } end |
#kick(user) ⇒ Object
644 645 646 |
# File 'lib/discordrb/data.rb', line 644 def kick(user) API.kick_user(@bot.token, @id, user.id) end |
#process_channels(channels) ⇒ Object
568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/discordrb/data.rb', line 568 def process_channels(channels) @channels = [] @channels_by_id = {} return unless channels channels.each do |element| channel = Channel.new(element, @bot, self) @channels << channel @channels_by_id[channel.id] = channel end end |
#process_members(members) ⇒ Object
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/discordrb/data.rb', line 536 def process_members(members) @members = [] @members_by_id = {} return unless members members.each do |element| user = User.new(element['user'], @bot) @members << user @members_by_id[user.id] = user user_roles = [] element['roles'].each do |e| role_id = e.to_i user_roles << @roles_by_id[role_id] end user.update_roles(self, user_roles) end end |
#process_presences(presences) ⇒ Object
554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/discordrb/data.rb', line 554 def process_presences(presences) # Update user statuses with presence info return unless presences presences.each do |element| next unless element['user'] user_id = element['user']['id'].to_i user = @members_by_id[user_id] if user user.status = element['status'].to_sym user.game = element['game'] ? element['game']['name'] : nil end end end |
#process_roles(roles) ⇒ Object
525 526 527 528 529 530 531 532 533 534 |
# File 'lib/discordrb/data.rb', line 525 def process_roles(roles) # Create roles @roles = [] @roles_by_id = {} roles.each do |element| role = Role.new(element, @bot, self) @roles << role @roles_by_id[role.id] = role end end |
#process_voice_states(voice_states) ⇒ Object
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/discordrb/data.rb', line 580 def process_voice_states(voice_states) return unless voice_states voice_states.each do |element| user_id = element['user_id'].to_i user = @members_by_id[user_id] next unless user user.server_mute = element['mute'] user.server_deaf = element['deaf'] user.self_mute = element['self_mute'] user.self_mute = element['self_mute'] channel_id = element['channel_id'].to_i channel = channel_id ? @channels_by_id[channel_id] : nil user.move(channel) end end |
#role(id) ⇒ Object
596 597 598 |
# File 'lib/discordrb/data.rb', line 596 def role(id) @roles.find { |e| e.id == id } end |
#unban(user) ⇒ Object
640 641 642 |
# File 'lib/discordrb/data.rb', line 640 def unban(user) API.unban_user(@bot.token, @id, user.id) end |
#update_data(new_data) ⇒ Object
678 679 680 681 682 683 684 685 686 |
# File 'lib/discordrb/data.rb', line 678 def update_data(new_data) @name = new_data[:name] || new_data['name'] || @name @region = new_data[:region] || new_data['region'] || @region @icon = new_data[:icon] || new_data['icon'] || @icon @afk_timeout = new_data[:afk_timeout] || new_data['afk_timeout'].to_i || @afk_timeout afk_channel_id = new_data[:afk_channel_id] || new_data['afk_channel_id'].to_i || @afk_channel.id @afk_channel = @bot.channel(afk_channel_id) if afk_channel_id != 0 && (!@afk_channel || afk_channel_id != @afk_channel.id) end |