Class: Discordrb::User

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, bot, server = nil) ⇒ User

Returns a new instance of User.



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

def initialize(data, bot, server = nil)
  @bot = bot

  @username = data['username']
  @id = data['id'].to_i
  @discriminator = data['discriminator']
  @avatar = data['avatar']
  @server = server
  @roles = []
  
  @status = :offline
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/discordrb/data.rb', line 91

def method_missing(method_name, *args, &block)
  if /\Acan_(?<action>\w+)\?\Z/ =~ method_name
    action = action.to_sym
    if Permissions::Flags.has_value? action
      has_permission? action, args.first
    else
      super
    end
  else
    super
  end
end

Instance Attribute Details

#avatarObject (readonly)

Returns the value of attribute avatar.



7
8
9
# File 'lib/discordrb/data.rb', line 7

def avatar
  @avatar
end

#discriminatorObject (readonly)

Returns the value of attribute discriminator.



7
8
9
# File 'lib/discordrb/data.rb', line 7

def discriminator
  @discriminator
end

#game_idObject

Returns the value of attribute game_id.



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

def game_id
  @game_id
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/discordrb/data.rb', line 7

def id
  @id
end

#rolesObject (readonly)

Returns the value of attribute roles.



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

def roles
  @roles
end

#self_deafObject

Returns the value of attribute self_deaf.



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

def self_deaf
  @self_deaf
end

#self_muteObject

Returns the value of attribute self_mute.



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

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.



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

def server_mute
  @server_mute
end

#statusObject

Returns the value of attribute status.



9
10
11
# File 'lib/discordrb/data.rb', line 9

def status
  @status
end

#usernameObject (readonly) Also known as: name

Returns the value of attribute username.



7
8
9
# File 'lib/discordrb/data.rb', line 7

def username
  @username
end

#voice_channelObject (readonly)

Returns the value of attribute voice_channel.



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

def voice_channel
  @voice_channel
end

Instance Method Details

#has_permission?(action, 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)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/discordrb/data.rb', line 64

def has_permission?(action, 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
  @roles.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
      # else
      #   If the channel has nothing to say on the matter, we can defer to the role itself
      end
    end
    if channel_allow == false
      can_act = 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
  end
end

#mentionObject

Utility function to mention users in messages



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

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

#move(to_channel) ⇒ Object

Move a user into a voice channel



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

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

#pm(content = nil) ⇒ Object

Utility function to send a PM



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

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

#respond_to?(method_name, include_private = false) ⇒ Boolean

Respond to can_*? methods

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/discordrb/data.rb', line 105

def respond_to?(method_name, include_private = false)
  if /\Acan_(?<action>\w+)\?\Z/ =~ method_name
    action = action.to_sym
    if Permissions::Flags.has_value? action
      true
    else
      super
    end
  else
    super
  end
end

#update_roles(roles) ⇒ Object

Set this user’s roles



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

def update_roles(roles)
  @roles = roles
end