class Discordrb::User
User
on Discord, including internal data like discriminators
Attributes
@return [ActivitySet] the activities of the user
@return [Hash<Symbol, Symbol>] the current online status (‘:online`, `:idle` or `:dnd`) of the user
on various device types (`:desktop`, `:mobile`, or `:web`). The value will be `nil` if the user is offline or invisible.
@return [Symbol] the current online status of the user (‘:online`, `:offline` or `:idle`)
Public Class Methods
@!visibility private
# File lib/discordrb/data/user.rb, line 115 def initialize(data, bot) @bot = bot @username = data['username'] @global_name = data['global_name'] @id = data['id'].to_i @discriminator = data['discriminator'] @avatar_id = data['avatar'] @roles = {} @activities = Discordrb::ActivitySet.new @public_flags = data['public_flags'] || 0 @bot_account = false @bot_account = true if data['bot'] @webhook_account = false @webhook_account = true if data['_webhook'] @status = :offline @client_status = process_client_status(data['client_status']) end
Public Instance Methods
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 Bot#add_await
# File lib/discordrb/data/user.rb, line 197 def await(key, attributes = {}, &block) @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block) end
Add a blocking 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 Bot#add_await!
# File lib/discordrb/data/user.rb, line 204 def await!(attributes = {}, &block) @bot.add_await!(Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block) end
Is the user the bot? @return [true, false] whether this user is the bot
# File lib/discordrb/data/user.rb, line 218 def current_bot? @bot.profile.id == @id end
@return [String, nil] the game the user is currently playing, or ‘nil` if nothing is being played. @deprecated Please use {ActivitySet#games} for information about the user’s game activity
# File lib/discordrb/data/user.rb, line 243 def game @activities.games.first&.name end
The inspect method is overwritten to give more useful output
# File lib/discordrb/data/user.rb, line 260 def inspect "<User username=#{@username} id=#{@id} discriminator=#{@discriminator}>" end
Gets the member this user is on a server @param server [Server] The server to get the member for @return [Member] this user as a member on a particular server
# File lib/discordrb/data/user.rb, line 211 def on(server) id = server.resolve_id @bot.server(id).member(@id) end
Get a user’s PM channel or send them a PM @overload pm
Creates a private message channel for this user or returns an existing one if it already exists @return [Channel] the PM channel to this user.
@overload pm(content)
Sends a private to this user. @param content [String] The content to send. @return [Message] the message sent to this user.
# File lib/discordrb/data/user.rb, line 145 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.pm_channel(@id) end end
@!visibility private
# File lib/discordrb/data/user.rb, line 223 def process_client_status(client_status) (client_status || {}).to_h { |k, v| [k.to_sym, v.to_sym] } end
Send the user a file. @param file [File] The file to send to the user @param caption [String] The caption of the file being sent @param filename [String] Overrides the filename of the uploaded file @param spoiler [true, false] Whether or not this file should appear as a spoiler. @return [Message] the message sent to this user. @example Send a file from disk
user.send_file(File.open('rubytaco.png', 'r'))
# File lib/discordrb/data/user.rb, line 166 def send_file(file, caption = nil, filename: nil, spoiler: nil) pm.send_file(file, caption: caption, filename: filename, spoiler: spoiler) end
@return [Integer] returns 1 for twitch streams, or 0 for no stream. @deprecated Please use {ActivitySet#streaming} for information about the user’s stream activity
# File lib/discordrb/data/user.rb, line 249 def stream_type @activities.streaming ? 1 : 0 end
@return [String, nil] the URL to the stream, if the user is currently streaming something @deprecated Please use {ActivitySet#streaming} for information about the user’s stream activity
# File lib/discordrb/data/user.rb, line 255 def stream_url @activities.streaming.first&.url end
Set the user’s global_name @note For internal use only. @!visibility private
# File lib/discordrb/data/user.rb, line 180 def update_global_name(global_name) @global_name = global_name end
Set the user’s presence data @note for internal use only @!visibility private
# File lib/discordrb/data/user.rb, line 187 def update_presence(data) @status = data['status'].to_sym @client_status = process_client_status(data['client_status']) @activities = Discordrb::ActivitySet.new(data['activities'].map { |act| Activity.new(act, @bot) }) end
Set the user’s username @note for internal use only @!visibility private
# File lib/discordrb/data/user.rb, line 173 def update_username(username) @username = username end