Class: Discordrb::User

Inherits:
Object
  • Object
show all
Includes:
IDObject, UserAttributes
Defined in:
lib/discordrb/data.rb

Overview

User on Discord, including internal data like discriminators

Direct Known Subclasses

Member, Profile, Recipient

Instance Attribute Summary collapse

Attributes included from UserAttributes

#avatar_id, #bot_account, #discriminator, #username

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from UserAttributes

#avatar_url, #distinct, #mention

Methods included from IDObject

#==, #creation_time, synthesise

Constructor Details

#initialize(data, bot) ⇒ User


164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/discordrb/data.rb', line 164

def initialize(data, bot)
  @bot = bot

  @username = data['username']
  @id = data['id'].to_i
  @discriminator = data['discriminator']
  @avatar_id = data['avatar']
  @roles = {}

   = false
   = true if data['bot']

  @status = :offline
end

Instance Attribute Details

#gameString? (readonly)


155
156
157
# File 'lib/discordrb/data.rb', line 155

def game
  @game
end

#statusSymbol (readonly)


152
153
154
# File 'lib/discordrb/data.rb', line 152

def status
  @status
end

#stream_typeString, ... (readonly)


162
163
164
# File 'lib/discordrb/data.rb', line 162

def stream_type
  @stream_type
end

#stream_urlString? (readonly)


158
159
160
# File 'lib/discordrb/data.rb', line 158

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.

See Also:


237
238
239
# File 'lib/discordrb/data.rb', line 237

def await(key, attributes = {}, &block)
  @bot.add_await(key, Discordrb::Events::MessageEvent, { from: @id }.merge(attributes), &block)
end

#await!(attributes = {}) ⇒ 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.

See Also:


244
245
246
# File 'lib/discordrb/data.rb', line 244

def await!(attributes = {})
  @bot.add_await!(Discordrb::Events::MessageEvent, { from: @id }.merge(attributes))
end

#current_bot?true, false

Is the user the bot?


258
259
260
# File 'lib/discordrb/data.rb', line 258

def current_bot?
  @bot.profile.id == @id
end

#inspectObject

The inspect method is overwritten to give more useful output


274
275
276
# File 'lib/discordrb/data.rb', line 274

def inspect
  "<User username=#{@username} id=#{@id} discriminator=#{@discriminator}>"
end

#on(server) ⇒ Member

Gets the member this user is on a server


251
252
253
254
# File 'lib/discordrb/data.rb', line 251

def on(server)
  id = server.resolve_id
  @bot.server(id).member(@id)
end

#pmChannel #pm(content) ⇒ Message Also known as: dm

Get a user's PM channel or send them a PM

Overloads:

  • #pmChannel

    Creates a private message channel for this user or returns an existing one if it already exists

  • #pm(content) ⇒ Message

    Sends a private to this user.


187
188
189
190
191
192
193
194
195
196
# File 'lib/discordrb/data.rb', line 187

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

#send_file(file, caption = nil) ⇒ Message

Send the user a file.

Examples:

Send a file from disk

user.send_file(File.open('rubytaco.png', 'r'))

206
207
208
# File 'lib/discordrb/data.rb', line 206

def send_file(file, caption = nil)
  pm.send_file(file, caption: caption)
end

#webhook?true, false


263
264
265
# File 'lib/discordrb/data.rb', line 263

def webhook?
  @discriminator == Message::ZERO_DISCRIM
end