Class: Discordrb::User
- Inherits:
-
Object
- Object
- Discordrb::User
- Defined in:
- lib/discordrb/data.rb
Overview
User on Discord, including internal data like discriminators
Direct Known Subclasses
Instance Attribute Summary collapse
-
#avatar_id ⇒ String
readonly
The ID of this user's current avatar, can be used to generate an avatar URL.
-
#discriminator ⇒ String
readonly
This user's discriminator which is used internally to identify users with identical usernames.
-
#game ⇒ String?
The game the user is currently playing, or
nil
if none is being played. -
#id ⇒ Integer
(also: #resolve_id)
readonly
This user's ID which uniquely identifies them across Discord.
-
#roles ⇒ Hash<Integer => Array<Role>>
readonly
The roles this user has, grouped by server ID.
-
#self_mute ⇒ true, false
Whether or not the user is currently muted by the bot.
-
#status ⇒ Symbol
The current online status of the user (
:online
,:offline
or:idle
). -
#username ⇒ String
(also: #name)
readonly
This user's username.
-
#voice_channel ⇒ Channel?
readonly
The voice channel this user is on currently.
Instance Method Summary collapse
-
#==(other) ⇒ Object
ID based comparison.
-
#add_role(server, role) ⇒ Object
Adds a role to this user on the specified server.
-
#avatar ⇒ Object
deprecated
Deprecated.
Use #avatar_id instead.
-
#avatar_url ⇒ String
Utility function to get a user's avatar URL.
-
#await(key, attributes = {}, &block) ⇒ Object
Add an await for a message from this user.
-
#bot? ⇒ true, false
Is the user the bot?.
-
#initialize(data, bot) ⇒ User
constructor
A new instance of User.
-
#mention ⇒ String
Utility function to mention users in messages.
-
#permission?(action, server, channel = nil) ⇒ true, false
Determines whether this user has a specific permission on a server (and channel).
-
#pm(content = nil) ⇒ Object
Get a user's PM channel or send them a PM.
-
#remove_role(server, role) ⇒ Object
Removes a role from this user on the specified server.
Constructor Details
#initialize(data, bot) ⇒ User
Returns a new instance of User.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/discordrb/data.rb', line 57 def initialize(data, bot) @bot = bot @username = data['username'] @id = data['id'].to_i @discriminator = data['discriminator'] @avatar_id = data['avatar'] @roles = {} @status = :offline end |
Instance Attribute Details
#avatar_id ⇒ String (readonly)
Returns the ID of this user's current avatar, can be used to generate an avatar URL.
30 31 32 |
# File 'lib/discordrb/data.rb', line 30 def avatar_id @avatar_id end |
#discriminator ⇒ String (readonly)
Returns this user's discriminator which is used internally to identify users with identical usernames.
26 27 28 |
# File 'lib/discordrb/data.rb', line 26 def discriminator @discriminator end |
#game ⇒ String?
Returns the game the user is currently playing, or nil
if none is being played.
44 45 46 |
# File 'lib/discordrb/data.rb', line 44 def game @game end |
#id ⇒ Integer (readonly) Also known as: resolve_id
Returns this user's ID which uniquely identifies them across Discord.
23 24 25 |
# File 'lib/discordrb/data.rb', line 23 def id @id end |
#roles ⇒ Hash<Integer => Array<Role>> (readonly)
Returns the roles this user has, grouped by server ID.
36 37 38 |
# File 'lib/discordrb/data.rb', line 36 def roles @roles end |
#self_mute ⇒ true, false
Returns whether or not the user is currently muted by the bot.
48 49 50 |
# File 'lib/discordrb/data.rb', line 48 def self_mute @self_mute end |
#status ⇒ Symbol
Returns the current online status of the user (:online
, :offline
or :idle
).
40 41 42 |
# File 'lib/discordrb/data.rb', line 40 def status @status end |
#username ⇒ String (readonly) Also known as: name
Returns this user's username.
20 21 22 |
# File 'lib/discordrb/data.rb', line 20 def username @username end |
#voice_channel ⇒ Channel? (readonly)
Returns the voice channel this user is on currently.
33 34 35 |
# File 'lib/discordrb/data.rb', line 33 def voice_channel @voice_channel end |
Instance Method Details
#==(other) ⇒ Object
ID based comparison
70 71 72 |
# File 'lib/discordrb/data.rb', line 70 def ==(other) Discordrb.id_compare(@id, other) end |
#add_role(server, role) ⇒ Object
Adds a role to this user on the specified server.
123 124 125 126 127 128 |
# File 'lib/discordrb/data.rb', line 123 def add_role(server, role) user_roles = @roles[server.id] || [] user_roles << role ids = user_roles.map(&:id) API.update_user_roles(@bot.token, server.id, @id, ids) end |
#avatar ⇒ Object
Use #avatar_id instead.
Gets the user's avatar ID.
76 77 78 79 |
# File 'lib/discordrb/data.rb', line 76 def avatar LOGGER.debug('Warning: Deprecated reader User.avatar was used! Use User.avatar_id (or User.avatar_url if you just want the URL) instead.', true) @avatar_id end |
#avatar_url ⇒ String
Utility function to get a user's avatar URL.
89 90 91 |
# File 'lib/discordrb/data.rb', line 89 def avatar_url API.avatar_url(@id, @avatar_id) end |
#await(key, attributes = {}, &block) ⇒ Object
Add an await for a message from this user. Specifically, this adds a global await for a MessageEvent with this user's ID as a :from attribute.
171 172 173 |
# File 'lib/discordrb/data.rb', line 171 def await(key, attributes = {}, &block) @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block) end |
#bot? ⇒ true, false
Is the user the bot?
177 178 179 |
# File 'lib/discordrb/data.rb', line 177 def bot? @bot.bot_user.id == @id end |
#mention ⇒ String
Utility function to mention users in messages
83 84 85 |
# File 'lib/discordrb/data.rb', line 83 def mention "<@#{@id}>" end |
#permission?(action, server, channel = nil) ⇒ true, false
Determines whether this user has a specific permission on a server (and channel).
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/discordrb/data.rb', line 186 def (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.[role.id] allow = channel.[role.id].allow deny = channel.[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 can_act = if channel_allow.nil? role..instance_variable_get("@#{action}") || can_act else channel_allow end can_act end end |
#pm ⇒ Channel #pm(content) ⇒ Message
Get a user's PM channel or send them a PM
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/discordrb/data.rb', line 101 def pm(content = nil) if content # Recursively call pm to get the channel, then send a message to it channel = pm channel.(content) else # If no message was specified, return the PM channel @bot.private_channel(@id) end end |
#remove_role(server, role) ⇒ Object
Removes a role from this user on the specified server.
133 134 135 136 137 138 139 140 |
# File 'lib/discordrb/data.rb', line 133 def remove_role(server, role) user_roles = @roles[server.id] || [] # If the given role has an ID (i.e. is a Role object), then check whether its ID is equal, otherwise check whether it's equal directly user_roles.delete_if { |e| e == role } ids = user_roles.map(&:id) API.update_user_roles(@bot.token, server.id, @id, ids) end |