class Discordrb::User

User on Discord, including internal data like discriminators

Attributes

game[R]

@return [String, nil] the game the user is currently playing, or `nil` if none is being played.

status[R]

@return [Symbol] the current online status of the user (`:online`, `:offline` or `:idle`)

stream_type[R]

@return [String, Integer, nil] 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.
stream_url[R]

@return [String, nil] the URL to the stream, if the user is currently streaming something.

Public Class Methods

new(data, bot) click to toggle source
# File lib/discordrb/data.rb, line 165
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

Public Instance Methods

await(key, attributes = {}, &block) click to toggle source

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.rb, line 238
def await(key, attributes = {}, &block)
  @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block)
end
await!(attributes = {}) click to toggle source

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.rb, line 245
def await!(attributes = {})
  @bot.add_await!(Discordrb::Events::MessageEvent, { from: @id }.merge(attributes))
end
current_bot?() click to toggle source

Is the user the bot? @return [true, false] whether this user is the bot

# File lib/discordrb/data.rb, line 259
def current_bot?
  @bot.profile.id == @id
end
dm(content = nil)
Alias for: pm
inspect() click to toggle source

The inspect method is overwritten to give more useful output

# File lib/discordrb/data.rb, line 275
def inspect
  "<User username=#{@username} id=#{@id} discriminator=#{@discriminator}>"
end
on(server) click to toggle source

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.rb, line 252
def on(server)
  id = server.resolve_id
  @bot.server(id).member(@id)
end
pm(content = nil) click to toggle source

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.rb, line 188
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
Also aliased as: dm
send_file(file, caption = nil) click to toggle source

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 @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.rb, line 207
def send_file(file, caption = nil)
  pm.send_file(file, caption: caption)
end
update_presence(data) click to toggle source

Set the user's presence data @note for internal use only @!visibility private

# File lib/discordrb/data.rb, line 221
def update_presence(data)
  @status = data['status'].to_sym

  if data['game']
    game = data['game']

    @game = game['name']
    @stream_url = game['url']
    @stream_type = game['type']
  else
    @game = @stream_url = @stream_type = nil
  end
end
update_username(username) click to toggle source

Set the user's name @note for internal use only @!visibility private

# File lib/discordrb/data.rb, line 214
def update_username(username)
  @username = username
end
webhook?() click to toggle source

@return [true, false] whether this user is a fake user for a webhook message

# File lib/discordrb/data.rb, line 264
def webhook?
  @discriminator == Message::ZERO_DISCRIM
end