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 ⇒ Object
readonly
Returns the value of attribute avatar.
-
#discriminator ⇒ Object
readonly
Returns the value of attribute discriminator.
-
#game ⇒ Object
Returns the value of attribute game.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
-
#self_deaf ⇒ Object
Returns the value of attribute self_deaf.
-
#self_mute ⇒ Object
Returns the value of attribute self_mute.
-
#server_deaf ⇒ Object
Returns the value of attribute server_deaf.
-
#server_mute ⇒ Object
Returns the value of attribute server_mute.
-
#status ⇒ Object
Returns the value of attribute status.
-
#username ⇒ Object
(also: #name)
readonly
Returns the value of attribute username.
-
#voice_channel ⇒ Object
readonly
Returns the value of attribute voice_channel.
Instance Method Summary collapse
-
#==(other) ⇒ Object
ID based comparison.
- #add_role(server, role) ⇒ Object
-
#await(key, attributes = {}, &block) ⇒ Object
Add an await for a message from this user.
-
#bot? ⇒ Boolean
Is the user the bot?.
-
#delete_roles(server_id) ⇒ Object
Delete a specific server from the roles (in case a user leaves a server).
-
#initialize(data, bot) ⇒ User
constructor
A new instance of User.
-
#mention ⇒ Object
Utility function to mention users in messages.
-
#merge_roles(server, roles) ⇒ Object
Merge this user's roles with the roles from another instance of this user (from another server).
-
#move(to_channel) ⇒ Object
Move a user into a voice channel.
-
#permission?(action, server, channel = nil) ⇒ Boolean
Determine if the user has permission to do an action action is a permission from Permissions::Flags.
-
#pm(content = nil) ⇒ Object
Utility function to send a PM.
- #remove_role(server, role) ⇒ Object
-
#update_roles(server, roles) ⇒ Object
Set this user's roles.
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
#avatar ⇒ Object (readonly)
Returns the value of attribute avatar.
19 20 21 |
# File 'lib/discordrb/data.rb', line 19 def avatar @avatar end |
#discriminator ⇒ Object (readonly)
Returns the value of attribute discriminator.
19 20 21 |
# File 'lib/discordrb/data.rb', line 19 def discriminator @discriminator end |
#game ⇒ Object
Returns the value of attribute game.
20 21 22 |
# File 'lib/discordrb/data.rb', line 20 def game @game end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
19 20 21 |
# File 'lib/discordrb/data.rb', line 19 def id @id end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
19 20 21 |
# File 'lib/discordrb/data.rb', line 19 def roles @roles end |
#self_deaf ⇒ Object
Returns the value of attribute self_deaf.
20 21 22 |
# File 'lib/discordrb/data.rb', line 20 def self_deaf @self_deaf end |
#self_mute ⇒ Object
Returns the value of attribute self_mute.
20 21 22 |
# File 'lib/discordrb/data.rb', line 20 def self_mute @self_mute end |
#server_deaf ⇒ Object
Returns the value of attribute server_deaf.
20 21 22 |
# File 'lib/discordrb/data.rb', line 20 def server_deaf @server_deaf end |
#server_mute ⇒ Object
Returns the value of attribute server_mute.
20 21 22 |
# File 'lib/discordrb/data.rb', line 20 def server_mute @server_mute end |
#status ⇒ Object
Returns the value of attribute status.
20 21 22 |
# File 'lib/discordrb/data.rb', line 20 def status @status end |
#username ⇒ Object (readonly) Also known as: name
Returns the value of attribute username.
19 20 21 |
# File 'lib/discordrb/data.rb', line 19 def username @username end |
#voice_channel ⇒ Object (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
#==(other) ⇒ Object
ID based comparison
41 42 43 |
# File 'lib/discordrb/data.rb', line 41 def ==(other) Discordrb.id_compare(@id, other) end |
#add_role(server, role) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/discordrb/data.rb', line 68 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 |
#await(key, attributes = {}, &block) ⇒ Object
Add an await for a message from this user
105 106 107 |
# File 'lib/discordrb/data.rb', line 105 def await(key, attributes = {}, &block) @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block) end |
#bot? ⇒ Boolean
Is the user the bot?
110 111 112 |
# File 'lib/discordrb/data.rb', line 110 def bot? @bot.bot_user.id == @id end |
#delete_roles(server_id) ⇒ Object
Delete a specific server from the roles (in case a user leaves a server)
100 101 102 |
# File 'lib/discordrb/data.rb', line 100 def delete_roles(server_id) @roles.delete(server_id) end |
#mention ⇒ Object
Utility function to mention users in messages
46 47 48 |
# File 'lib/discordrb/data.rb', line 46 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)
91 92 93 94 95 96 97 |
# File 'lib/discordrb/data.rb', line 91 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
63 64 65 66 |
# File 'lib/discordrb/data.rb', line 63 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).
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/discordrb/data.rb', line 117 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 if channel_allow == false can_act = false elsif channel_allow == true can_act = true else # channel_allow == nil can_act = role..instance_variable_get("@#{action}") || can_act end can_act end end |
#pm(content = nil) ⇒ Object
Utility function to send a PM
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/discordrb/data.rb', line 51 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
75 76 77 78 79 80 81 82 |
# File 'lib/discordrb/data.rb', line 75 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 |
#update_roles(server, roles) ⇒ Object
Set this user's roles
85 86 87 88 |
# File 'lib/discordrb/data.rb', line 85 def update_roles(server, roles) @roles ||= {} @roles[server.id] = roles end |