Class: DiscordRDA::User
Overview
Represents a Discord user. Users are account-wide and not guild-specific.
Class Attribute Summary collapse
-
.api ⇒ Object
Returns the value of attribute api.
Attributes inherited from Entity
Class Method Summary collapse
-
.get_application_role_connection(application_id) ⇒ Hash?
Get user’s application role connection.
-
.get_connections ⇒ Array<Hash>
Get user connections (for current user only).
-
.get_current_user_guild_member(guild_id) ⇒ Hash?
Get current user’s guild member information.
-
.get_current_user_guilds(limit: 200, after: nil, before: nil, with_counts: false) ⇒ Array<Hash>
Get guilds the current user is in.
-
.leave_guild(guild_id) ⇒ void
Leave a guild.
-
.modify_current_user(username: nil, avatar: nil) ⇒ User
Modify the current user.
-
.update_application_role_connection(application_id, platform_name: nil, platform_username: nil, metadata: {}) ⇒ Hash
Update user’s application role connection.
Instance Method Summary collapse
-
#animated_avatar? ⇒ Boolean
Check if the avatar is animated (GIF).
-
#avatar_url(format: nil, size: nil) ⇒ String
Get the user’s avatar URL.
-
#banner_url(format: 'png', size: nil) ⇒ String?
Get the user’s banner URL.
-
#bot? ⇒ Boolean
Check if user is a bot account.
-
#create_dm_channel ⇒ Channel?
Create a DM channel with this user.
-
#default_avatar_url ⇒ String
Get the default avatar URL based on discriminator.
-
#display_name ⇒ String
Get the user’s effective name (global_name or username).
-
#mention ⇒ String
Get mention string for the user.
-
#public_flag?(flag) ⇒ Boolean
Check if user has a specific public flag.
-
#system? ⇒ Boolean
Check if this is the system user.
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
.api ⇒ Object
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
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_connections ⇒ Array<Hash>
Get user connections (for current user only)
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
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
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
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
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
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)
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
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 |
#banner_url(format: 'png', size: nil) ⇒ String?
Get the user’s banner URL
87 88 89 90 91 92 93 |
# File 'lib/discord_rda/entity/user.rb', line 87 def (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
40 41 42 |
# File 'lib/discord_rda/entity/user.rb', line 40 def bot? @raw_data['bot'] || false end |
#create_dm_channel ⇒ Channel?
Create a DM channel with this user
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_url ⇒ String
Get the default avatar URL based on discriminator
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_name ⇒ String
Get the user’s effective name (global_name or username)
34 35 36 |
# File 'lib/discord_rda/entity/user.rb', line 34 def display_name global_name || username end |
#mention ⇒ String
Get mention string for the user
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
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
46 47 48 |
# File 'lib/discord_rda/entity/user.rb', line 46 def system? @raw_data['system'] || false end |