Class: Discordrb::Server

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

Overview

A server on Discord

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, bot) ⇒ Server

Returns a new instance of Server.



459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/discordrb/data.rb', line 459

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_idObject

Returns the value of attribute afk_channel_id.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def afk_channel_id
  @afk_channel_id
end

#afk_timeoutObject

Returns the value of attribute afk_timeout.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def afk_timeout
  @afk_timeout
end

#channelsObject (readonly)

Returns the value of attribute channels.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def channels
  @channels
end

#iconObject

Returns the value of attribute icon.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def icon
  @icon
end

#idObject (readonly)

Returns the value of attribute id.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def id
  @id
end

#membersObject (readonly)

Returns the value of attribute members.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def members
  @members
end

#nameObject

Returns the value of attribute name.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def name
  @name
end

#owner_idObject (readonly)

Returns the value of attribute owner_id.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def owner_id
  @owner_id
end

#regionObject

Returns the value of attribute region.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def region
  @region
end

#rolesObject (readonly)

Returns the value of attribute roles.



457
458
459
# File 'lib/discordrb/data.rb', line 457

def roles
  @roles
end

Instance Method Details

#add_role(role) ⇒ Object



543
544
545
# File 'lib/discordrb/data.rb', line 543

def add_role(role)
  @roles << role
end

#add_user(user) ⇒ Object



559
560
561
# File 'lib/discordrb/data.rb', line 559

def add_user(user)
  @members << user
end

#afk_channel=(afk_channel) ⇒ Object



607
608
609
# File 'lib/discordrb/data.rb', line 607

def afk_channel=(afk_channel)
  update_server_data(afk_channel_id: afk_channel.id)
end

#ban(user, message_days = 0) ⇒ Object



579
580
581
# File 'lib/discordrb/data.rb', line 579

def ban(user, message_days = 0)
  API.ban_user(@bot.token, @id, user.id, message_days)
end

#create_channel(name) ⇒ Object



567
568
569
570
# File 'lib/discordrb/data.rb', line 567

def create_channel(name)
  response = API.create_channel(@bot.token, @id, name, 'text')
  Channel.new(JSON.parse(response), @bot)
end

#create_roleObject



572
573
574
575
576
577
# File 'lib/discordrb/data.rb', line 572

def create_role
  response = API.create_role(@bot.token, @id)
  role = Role.new(JSON.parse(response), @bot)
  @roles << role
  role
end

#deleteObject



591
592
593
# File 'lib/discordrb/data.rb', line 591

def delete
  API.delete_server(@bot.token, @id)
end

#delete_role(role_id) ⇒ Object



547
548
549
550
551
552
553
554
555
556
557
# File 'lib/discordrb/data.rb', line 547

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.permission_overwrites.reject { |id, _| id == role_id }
    channel.update_overwrites(overwrites)
  end
end

#delete_user(user_id) ⇒ Object



563
564
565
# File 'lib/discordrb/data.rb', line 563

def delete_user(user_id)
  @members.reject! { |member| member.id == user_id }
end

#kick(user) ⇒ Object



587
588
589
# File 'lib/discordrb/data.rb', line 587

def kick(user)
  API.kick_user(@bot.token, @id, user.id)
end

#process_channels(channels) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
# File 'lib/discordrb/data.rb', line 515

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



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/discordrb/data.rb', line 483

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



501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/discordrb/data.rb', line 501

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 = Discordrb::Games.find_game(element['game_id'])
    end
  end
end

#process_roles(roles) ⇒ Object



472
473
474
475
476
477
478
479
480
481
# File 'lib/discordrb/data.rb', line 472

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



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/discordrb/data.rb', line 527

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']
    channel = channel_id ? @channels_by_id[channel_id] : nil
    user.move(channel)
  end
end

#unban(user) ⇒ Object



583
584
585
# File 'lib/discordrb/data.rb', line 583

def unban(user)
  API.unban_user(@bot.token, @id, user.id)
end

#update_data(new_data) ⇒ Object



619
620
621
622
623
624
625
626
627
# File 'lib/discordrb/data.rb', line 619

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