Class: DiscordRDA::User

Inherits:
Entity
  • Object
show all
Defined in:
lib/discord_rda/entity/user.rb

Overview

Represents a Discord user. Users are account-wide and not guild-specific.

Class Attribute Summary collapse

Attributes inherited from Entity

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#==, attribute, #created_at, from_hash, #hash, #initialize, #inspect, #to_h, #to_json

Constructor Details

This class inherits a constructor from DiscordRDA::Entity

Class Attribute Details

.apiObject

Returns the value of attribute api.



13
14
15
# File 'lib/discord_rda/entity/user.rb', line 13

def api
  @api
end

Class Method Details

.get_application_role_connection(application_id) ⇒ Hash?

Get user’s application role connection

Parameters:

  • application_id (String, Snowflake)

    Application ID

Returns:

  • (Hash, nil)

    Role connection metadata



205
206
207
208
209
210
211
# File 'lib/discord_rda/entity/user.rb', line 205

def self.get_application_role_connection(application_id)
  return nil unless api

  api.get("/users/@me/applications/#{application_id}/role-connection")
rescue RestClient::NotFoundError
  nil
end

.get_connectionsArray<Hash>

Get user connections (for current user only)

Returns:

  • (Array<Hash>)

    Connected accounts



196
197
198
199
200
# File 'lib/discord_rda/entity/user.rb', line 196

def self.get_connections
  return [] unless api

  api.get('/users/@me/connections')
end

.get_current_user_guild_member(guild_id) ⇒ Hash?

Get current user’s guild member information

Parameters:

Returns:

  • (Hash, nil)

    Guild member object



160
161
162
163
164
165
166
# File 'lib/discord_rda/entity/user.rb', line 160

def self.get_current_user_guild_member(guild_id)
  return nil unless api

  api.get("/users/@me/guilds/#{guild_id}/member")
rescue RestClient::NotFoundError
  nil
end

.get_current_user_guilds(limit: 200, after: nil, before: nil, with_counts: false) ⇒ Array<Hash>

Get guilds the current user is in

Parameters:

  • limit (Integer) (defaults to: 200)

    Max number of guilds (1-200, default 200)

  • after (String) (defaults to: nil)

    Get guilds after this guild ID

  • before (String) (defaults to: nil)

    Get guilds before this guild ID

  • with_counts (Boolean) (defaults to: false)

    Include approximate member and presence counts

Returns:

  • (Array<Hash>)

    Guild objects (partial, not full Guild entities)



138
139
140
141
142
143
144
145
146
# File 'lib/discord_rda/entity/user.rb', line 138

def self.get_current_user_guilds(limit: 200, after: nil, before: nil, with_counts: false)
  return [] unless api

  params = { limit: limit, with_counts: with_counts }
  params[:after] = after if after
  params[:before] = before if before

  api.get('/users/@me/guilds', params: params)
end

.leave_guild(guild_id) ⇒ void

This method returns an undefined value.

Leave a guild

Parameters:

  • guild_id (String, Snowflake)

    Guild ID to leave



151
152
153
154
155
# File 'lib/discord_rda/entity/user.rb', line 151

def self.leave_guild(guild_id)
  return unless api

  api.delete("/users/@me/guilds/#{guild_id}")
end

.modify_current_user(username: nil, avatar: nil) ⇒ User

Modify the current user

Parameters:

  • username (String) (defaults to: nil)

    New username

  • avatar (File, String) (defaults to: nil)

    New avatar (file or base64 data URI)

Returns:

  • (User)

    Updated user



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/discord_rda/entity/user.rb', line 172

def self.modify_current_user(username: nil, avatar: nil)
  return nil unless api

  body = {}
  body[:username] = username if username

  if avatar
    body[:avatar] = if avatar.respond_to?(:read)
                      # Convert file to base64 data URI
                      data = avatar.read
                      base64 = Base64.strict_encode64(data)
                      ext = File.extname(avatar.respond_to?(:path) ? avatar.path : 'png').delete('.')
                      "data:image/#{ext};base64,#{base64}"
                    else
                      avatar
                    end
  end

  data = api.patch('/users/@me', body: body)
  User.new(data)
end

.update_application_role_connection(application_id, platform_name: nil, platform_username: nil, metadata: {}) ⇒ Hash

Update user’s application role connection

Parameters:

  • application_id (String, Snowflake)

    Application ID

  • platform_name (String) (defaults to: nil)

    Platform name

  • platform_username (String) (defaults to: nil)

    Platform username

  • metadata (Hash) (defaults to: {})

    Role connection metadata

Returns:

  • (Hash)

    Updated role connection



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/discord_rda/entity/user.rb', line 219

def self.update_application_role_connection(application_id, platform_name: nil, platform_username: nil, metadata: {})
  return nil unless api

  body = {
    platform_name: platform_name,
    platform_username: platform_username,
    metadata: 
  }.compact

  api.put("/users/@me/applications/#{application_id}/role-connection", body: body)
end

Instance Method Details

#animated_avatar?Boolean

Check if the avatar is animated (GIF)

Returns:

  • (Boolean)

    True if animated



73
74
75
# File 'lib/discord_rda/entity/user.rb', line 73

def animated_avatar?
  avatar&.start_with?('a_')
end

#avatar_url(format: nil, size: nil) ⇒ String

Get the user’s avatar URL

Parameters:

  • format (String) (defaults to: nil)

    Image format (png, jpg, webp, gif)

  • size (Integer) (defaults to: nil)

    Image size (power of 2, 16-4096)

Returns:

  • (String)

    Avatar URL



54
55
56
57
58
59
60
61
# File 'lib/discord_rda/entity/user.rb', line 54

def avatar_url(format: nil, size: nil)
  return default_avatar_url unless avatar

  ext = format || (animated_avatar? ? 'gif' : 'png')
  url = "https://cdn.discordapp.com/avatars/#{id}/#{avatar}.#{ext}"
  url += "?size=#{size}" if size
  url
end

Get the user’s banner URL

Parameters:

  • format (String) (defaults to: 'png')

    Image format

  • size (Integer) (defaults to: nil)

    Image size

Returns:

  • (String, nil)

    Banner URL or nil if no banner



87
88
89
90
91
92
93
# File 'lib/discord_rda/entity/user.rb', line 87

def banner_url(format: 'png', size: nil)
  return nil unless @raw_data['banner']

  url = "https://cdn.discordapp.com/banners/#{id}/#{@raw_data['banner']}.#{format}"
  url += "?size=#{size}" if size
  url
end

#bot?Boolean

Check if user is a bot account

Returns:

  • (Boolean)

    True if bot



40
41
42
# File 'lib/discord_rda/entity/user.rb', line 40

def bot?
  @raw_data['bot'] || false
end

#create_dm_channelChannel?

Create a DM channel with this user

Returns:



125
126
127
128
129
130
# File 'lib/discord_rda/entity/user.rb', line 125

def create_dm_channel
  return nil unless self.class.api

  data = self.class.api.post('/users/@me/channels', body: { recipient_id: id.to_s })
  Channel.new(data)
end

#default_avatar_urlString

Get the default avatar URL based on discriminator

Returns:

  • (String)

    Default avatar URL



65
66
67
68
69
# File 'lib/discord_rda/entity/user.rb', line 65

def default_avatar_url
  discrim = discriminator.to_i
  index = discrim % 5
  "https://cdn.discordapp.com/embed/avatars/#{index}.png"
end

#display_nameString

Get the user’s effective name (global_name or username)

Returns:

  • (String)

    The display name



34
35
36
# File 'lib/discord_rda/entity/user.rb', line 34

def display_name
  global_name || username
end

#mentionString

Get mention string for the user

Returns:

  • (String)

    Mention string



79
80
81
# File 'lib/discord_rda/entity/user.rb', line 79

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

#public_flag?(flag) ⇒ Boolean

Check if user has a specific public flag

Parameters:

  • flag (Symbol)

    Flag name

Returns:

  • (Boolean)

    True if flag is set



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/discord_rda/entity/user.rb', line 98

def public_flag?(flag)
  flags = {
    staff: 1 << 0,
    partner: 1 << 1,
    hypesquad: 1 << 2,
    bug_hunter_level_1: 1 << 3,
    hypesquad_bravery: 1 << 6,
    hypesquad_brilliance: 1 << 7,
    hypesquad_balance: 1 << 8,
    early_supporter: 1 << 9,
    team_user: 1 << 10,
    bug_hunter_level_2: 1 << 14,
    verified_bot: 1 << 16,
    verified_developer: 1 << 17,
    certified_moderator: 1 << 18,
    bot_http_interactions: 1 << 19,
    active_developer: 1 << 22
  }

  flag_value = flags[flag.to_sym]
  return false unless flag_value

  (public_flags & flag_value) == flag_value
end

#system?Boolean

Check if this is the system user

Returns:

  • (Boolean)

    True if system user



46
47
48
# File 'lib/discord_rda/entity/user.rb', line 46

def system?
  @raw_data['system'] || false
end