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.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/discordrb/data.rb', line 20

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.



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

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)

Returns the value of attribute roles.



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

def roles
  @roles
end

#self_deafObject

Returns the value of attribute self_deaf.



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

def self_deaf
  @self_deaf
end

#self_muteObject

Returns the value of attribute self_mute.



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

def self_mute
  @self_mute
end

#server_deafObject

Returns the value of attribute server_deaf.



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

def server_deaf
  @server_deaf
end

#server_muteObject

Returns the value of attribute server_mute.



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

def server_mute
  @server_mute
end

#statusObject

Returns the value of attribute status.



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

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.



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

def voice_channel
  @voice_channel
end

Instance Method Details

#await(key, attributes = {}, &block) ⇒ Object

Add an await for a message from this user



75
76
77
# File 'lib/discordrb/data.rb', line 75

def await(key, attributes = {}, &block)
  @bot.add_await(key, MessageEvent, { from: @id }.merge(attributes), &block)
end

#delete_roles(server_id) ⇒ Object

Delete a specific server from the roles (in case a user leaves a server)



70
71
72
# File 'lib/discordrb/data.rb', line 70

def delete_roles(server_id)
  @roles.delete(server_id)
end

#mentionObject

Utility function to mention users in messages



33
34
35
# File 'lib/discordrb/data.rb', line 33

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)



61
62
63
64
65
66
67
# File 'lib/discordrb/data.rb', line 61

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



50
51
52
53
# File 'lib/discordrb/data.rb', line 50

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)


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
108
109
# File 'lib/discordrb/data.rb', line 82

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/discordrb/data.rb', line 38

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



56
57
58
# File 'lib/discordrb/data.rb', line 56

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