Class: Discordrb::User

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

Overview

User on Discord, including internal data like discriminators

Direct Known Subclasses

Profile

Instance Attribute Summary collapse

Instance Method Summary collapse

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_idString (readonly)

Returns the ID of this user's current avatar, can be used to generate an avatar URL.

Returns:

  • (String)

    the ID of this user's current avatar, can be used to generate an avatar URL.

See Also:



30
31
32
# File 'lib/discordrb/data.rb', line 30

def avatar_id
  @avatar_id
end

#discriminatorString (readonly)

Returns this user's discriminator which is used internally to identify users with identical usernames.

Returns:

  • (String)

    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

#gameString?

Returns the game the user is currently playing, or nil if none is being played.

Returns:

  • (String, nil)

    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

#idInteger (readonly) Also known as: resolve_id

Returns this user's ID which uniquely identifies them across Discord.

Returns:

  • (Integer)

    this user's ID which uniquely identifies them across Discord.



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

def id
  @id
end

#rolesHash<Integer => Array<Role>> (readonly)

Returns the roles this user has, grouped by server ID.

Returns:

  • (Hash<Integer => Array<Role>>)

    the roles this user has, grouped by server ID.



36
37
38
# File 'lib/discordrb/data.rb', line 36

def roles
  @roles
end

#self_mutetrue, false

Returns whether or not the user is currently muted by the bot.

Returns:

  • (true, false)

    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

#statusSymbol

Returns the current online status of the user (:online, :offline or :idle).

Returns:

  • (Symbol)

    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

#usernameString (readonly) Also known as: name

Returns this user's username.

Returns:

  • (String)

    this user's username



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

def username
  @username
end

#voice_channelChannel? (readonly)

Returns the voice channel this user is on currently.

Returns:

  • (Channel, nil)

    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.

Parameters:

  • server (Server)

    The server on which to add the role.

  • role (Role)

    The role to add.



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

#avatarObject

Deprecated.

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_urlString

Utility function to get a user's avatar URL.

Returns:

  • (String)

    the URL to the avatar image.



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.

See Also:



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?

Returns:

  • (true, false)

    whether this user is the bot



177
178
179
# File 'lib/discordrb/data.rb', line 177

def bot?
  @bot.bot_user.id == @id
end

#mentionString

Utility function to mention users in messages

Returns:

  • (String)

    the mention code in the form of <@id>



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).

Parameters:

  • action (Symbol)

    The permission that should be checked. See also Permissions::Flags for a list.

  • server (Server)

    The server on which the permission should be checked.

  • channel (Channel, nil) (defaults to: nil)

    If channel overrides should be checked too, this channel specifies where the overrides should be checked.

Returns:

  • (true, false)

    whether or not this user has the permission.



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 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
    can_act = if channel_allow.nil?
                role.permissions.instance_variable_get("@#{action}") || can_act
              else
                channel_allow
              end
    can_act
  end
end

#pmChannel #pm(content) ⇒ Message

Get a user's PM channel or send them a PM

Overloads:

  • #pmChannel

    Creates a private message channel for this user or returns an existing one if it already exists

    Returns:

    • (Channel)

      the PM channel to this user.

  • #pm(content) ⇒ Message

    Sends a private to this user.

    Parameters:

    • content (String)

      The content to send.

    Returns:

    • (Message)

      the message sent to this user.



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.send_message(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.

Parameters:

  • server (Server)

    The server on which to remove the role.

  • role (Role)

    The role to remove.



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