Class: Discordrb::User
- Inherits:
-
Object
- Object
- Discordrb::User
- Includes:
- IDObject, UserAttributes
- Defined in:
- lib/discordrb/data/user.rb
Overview
User on Discord, including internal data like discriminators
Instance Attribute Summary collapse
-
#game ⇒ String?
readonly
The game the user is currently playing, or
nil
if none is being played. -
#status ⇒ Symbol
readonly
The current online status of the user (
:online
,:offline
or:idle
). -
#stream_type ⇒ String, ...
readonly
The type of the stream.
-
#stream_url ⇒ String?
readonly
The URL to the stream, if the user is currently streaming something.
Attributes included from UserAttributes
#avatar_id, #bot_account, #discriminator, #username
Attributes included from IDObject
Instance Method Summary collapse
-
#await(key, attributes = {}, &block) ⇒ Object
Add an await for a message from this user.
-
#await!(attributes = {}, &block) ⇒ Object
Add a blocking await for a message from this user.
-
#current_bot? ⇒ true, false
Is the user the bot?.
-
#dnd? ⇒ true, false
Whether this user is set to do not disturb.
-
#idle? ⇒ true, false
Whether this user is idle.
-
#initialize(data, bot) ⇒ User
constructor
A new instance of User.
-
#inspect ⇒ Object
The inspect method is overwritten to give more useful output.
-
#offline? ⇒ true, false
Whether this user is offline.
-
#on(server) ⇒ Member
Gets the member this user is on a server.
-
#online? ⇒ true, false
Whether this user is online.
-
#pm(content = nil) ⇒ Object
(also: #dm)
Get a user's PM channel or send them a PM.
-
#send_file(file, caption = nil, filename: nil, spoiler: nil) ⇒ Message
Send the user a file.
-
#webhook? ⇒ true, false
Whether this user is a fake user for a webhook message.
Methods included from UserAttributes
#avatar_url, #distinct, #mention
Methods included from IDObject
#==, #creation_time, synthesise
Constructor Details
#initialize(data, bot) ⇒ User
Returns a new instance of User.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/discordrb/data/user.rb', line 64 def initialize(data, bot) @bot = bot @username = data['username'] @id = data['id'].to_i @discriminator = data['discriminator'] @avatar_id = data['avatar'] @roles = {} @bot_account = false @bot_account = true if data['bot'] @status = :offline end |
Instance Attribute Details
#game ⇒ String? (readonly)
Returns the game the user is currently playing, or nil
if none is being played.
55 56 57 |
# File 'lib/discordrb/data/user.rb', line 55 def game @game end |
#status ⇒ Symbol (readonly)
Returns the current online status of the user (:online
, :offline
or :idle
).
52 53 54 |
# File 'lib/discordrb/data/user.rb', line 52 def status @status end |
#stream_type ⇒ String, ... (readonly)
Returns the type of the stream. Can technically be set to anything, most of the time it will be 0 for no stream or 1 for Twitch streams.
62 63 64 |
# File 'lib/discordrb/data/user.rb', line 62 def stream_type @stream_type end |
#stream_url ⇒ String? (readonly)
Returns the URL to the stream, if the user is currently streaming something.
58 59 60 |
# File 'lib/discordrb/data/user.rb', line 58 def stream_url @stream_url end |
Instance Method Details
#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.
139 140 141 |
# File 'lib/discordrb/data/user.rb', line 139 def await(key, attributes = {}, &block) @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block) end |
#await!(attributes = {}, &block) ⇒ Object
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.
146 147 148 |
# File 'lib/discordrb/data/user.rb', line 146 def await!(attributes = {}, &block) @bot.add_await!(Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block) end |
#current_bot? ⇒ true, false
Is the user the bot?
160 161 162 |
# File 'lib/discordrb/data/user.rb', line 160 def current_bot? @bot.profile.id == @id end |
#dnd? ⇒ true, false
Returns whether this user is set to do not disturb.
177 178 179 180 181 |
# File 'lib/discordrb/data/user.rb', line 177 %i[offline idle online dnd].each do |e| define_method(e.to_s + '?') do @status.to_sym == e end end |
#idle? ⇒ true, false
Returns whether this user is idle.
177 178 179 180 181 |
# File 'lib/discordrb/data/user.rb', line 177 %i[offline idle online dnd].each do |e| define_method(e.to_s + '?') do @status.to_sym == e end end |
#inspect ⇒ Object
The inspect method is overwritten to give more useful output
184 185 186 |
# File 'lib/discordrb/data/user.rb', line 184 def inspect "<User username=#{@username} id=#{@id} discriminator=#{@discriminator}>" end |
#offline? ⇒ true, false
Returns whether this user is offline.
177 178 179 180 181 |
# File 'lib/discordrb/data/user.rb', line 177 %i[offline idle online dnd].each do |e| define_method(e.to_s + '?') do @status.to_sym == e end end |
#on(server) ⇒ Member
Gets the member this user is on a server
153 154 155 156 |
# File 'lib/discordrb/data/user.rb', line 153 def on(server) id = server.resolve_id @bot.server(id).member(@id) end |
#online? ⇒ true, false
Returns whether this user is online.
177 178 179 180 181 |
# File 'lib/discordrb/data/user.rb', line 177 %i[offline idle online dnd].each do |e| define_method(e.to_s + '?') do @status.to_sym == e end end |
#pm ⇒ Channel #pm(content) ⇒ Message Also known as: dm
Get a user's PM channel or send them a PM
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/discordrb/data/user.rb', line 87 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.pm_channel(@id) end end |
#send_file(file, caption = nil, filename: nil, spoiler: nil) ⇒ Message
Send the user a file.
108 109 110 |
# File 'lib/discordrb/data/user.rb', line 108 def send_file(file, caption = nil, filename: nil, spoiler: nil) pm.send_file(file, caption: caption, filename: filename, spoiler: spoiler) end |
#webhook? ⇒ true, false
Returns whether this user is a fake user for a webhook message.
165 166 167 |
# File 'lib/discordrb/data/user.rb', line 165 def webhook? @discriminator == Message::ZERO_DISCRIM end |