Class: Discordrb::User

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

Overview

User on Discord, including internal data like discriminators

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, bot) ⇒ User

Returns a new instance of User.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/discordrb/data.rb', line 28

def initialize(data, bot)
  @bot = bot

  @username = data['username']
  @id = data['id'].to_i
  @discriminator = data['discriminator']
  @avatar = data['avatar']
  @roles = {}

  @status = :offline
end

Instance Attribute Details

#avatarObject (readonly)

Returns the value of attribute avatar.



11
12
13
# File 'lib/discordrb/data.rb', line 11

def avatar
  @avatar
end

#discriminatorObject (readonly)

Returns the value of attribute discriminator.



11
12
13
# File 'lib/discordrb/data.rb', line 11

def discriminator
  @discriminator
end

#gameObject

Returns the value of attribute game.



14
15
16
# File 'lib/discordrb/data.rb', line 14

def game
  @game
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/discordrb/data.rb', line 11

def id
  @id
end

#rolesObject (readonly)

Hash of user roles. Key: Server ID Value: Array of roles.



24
25
26
# File 'lib/discordrb/data.rb', line 24

def roles
  @roles
end

#self_deafObject

Returns the value of attribute self_deaf.



18
19
20
# File 'lib/discordrb/data.rb', line 18

def self_deaf
  @self_deaf
end

#self_muteObject

Returns the value of attribute self_mute.



17
18
19
# File 'lib/discordrb/data.rb', line 17

def self_mute
  @self_mute
end

#server_deafObject

Returns the value of attribute server_deaf.



16
17
18
# File 'lib/discordrb/data.rb', line 16

def server_deaf
  @server_deaf
end

#server_muteObject

Returns the value of attribute server_mute.



15
16
17
# File 'lib/discordrb/data.rb', line 15

def server_mute
  @server_mute
end

#statusObject

Returns the value of attribute status.



13
14
15
# File 'lib/discordrb/data.rb', line 13

def status
  @status
end

#usernameObject (readonly) Also known as: name

Returns the value of attribute username.



11
12
13
# File 'lib/discordrb/data.rb', line 11

def username
  @username
end

#voice_channelObject (readonly)

Returns the value of attribute voice_channel.



19
20
21
# File 'lib/discordrb/data.rb', line 19

def voice_channel
  @voice_channel
end

Instance Method Details

#mentionObject

Utility function to mention users in messages



41
42
43
# File 'lib/discordrb/data.rb', line 41

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

#merge_roles(server, roles) ⇒ Object

Merge this user’s roles with the roles from another instance of this user (from another server)



69
70
71
72
73
74
75
# File 'lib/discordrb/data.rb', line 69

def merge_roles(server, roles)
  if @roles[server.id]
    @roles[server.id] = (@roles[server.id] + roles).uniq
  else
    @roles[server.id] = roles
  end
end

#move(to_channel) ⇒ Object

Move a user into a voice channel



58
59
60
61
# File 'lib/discordrb/data.rb', line 58

def move(to_channel)
  return if to_channel && to_channel.type != 'voice'
  @voice_channel = to_channel
end

#permission?(action, server, channel = nil) ⇒ Boolean

Determine if the user has permission to do an action action is a permission from Permissions::Flags. channel is the channel in which the action takes place (not applicable for server-wide actions).

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/discordrb/data.rb', line 80

def permission?(action, server, channel = nil)
  # For each role, check if
  #   (1) the channel explicitly allows or permits an action for the role and
  #   (2) if the user is allowed to do the action if the channel doesn't specify
  return false unless @roles[server.id]

  @roles[server.id].reduce(false) do |can_act, role|
    channel_allow = nil
    if channel && channel.permission_overwrites[role.id]
      allow = channel.permission_overwrites[role.id].allow
      deny = channel.permission_overwrites[role.id].deny
      if allow.instance_variable_get("@#{action}")
        channel_allow = true
      elsif deny.instance_variable_get("@#{action}")
        channel_allow = false
      end
      # If the channel has nothing to say on the matter, we can defer to the role itself
    end
    if channel_allow == false
      can_act = false
    elsif channel_allow == true
      can_act = true
    else # channel_allow == nil
      can_act = role.permissions.instance_variable_get("@#{action}") || can_act
    end
    can_act
  end
end

#pm(content = nil) ⇒ Object

Utility function to send a PM



46
47
48
49
50
51
52
53
54
55
# File 'lib/discordrb/data.rb', line 46

def pm(content = nil)
  if content
    # Recursively call pm to get the channel, then send a message to it
    channel = pm
    channel.send_message(content)
  else
    # If no message was specified, return the PM channel
    @bot.private_channel(@id)
  end
end

#update_roles(server, roles) ⇒ Object

Set this user’s roles



64
65
66
# File 'lib/discordrb/data.rb', line 64

def update_roles(server, roles)
  @roles[server.id] = roles
end