Class: Discordrb::Server

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

Overview

A server on Discord

Instance Attribute Summary collapse

Attributes included from ServerAttributes

#icon_id, #name

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from ServerAttributes

#icon_url

Methods included from IDObject

#==, #creation_time, synthesise

Instance Attribute Details

#afk_channelChannel?

Returns the AFK voice channel of this server, or nil if none is set.

Returns:

  • (Channel, nil)

    the AFK voice channel of this server, or nil if none is set



2237
2238
2239
# File 'lib/discordrb/data.rb', line 2237

def afk_channel
  @afk_channel
end

#afk_timeoutInteger

Returns the amount of time after which a voice user gets moved into the AFK channel, in seconds.

Returns:

  • (Integer)

    the amount of time after which a voice user gets moved into the AFK channel, in seconds.



2234
2235
2236
# File 'lib/discordrb/data.rb', line 2234

def afk_timeout
  @afk_timeout
end

#channelsArray<Channel> (readonly)

Returns an array of all the channels (text and voice) on this server.

Returns:

  • (Array<Channel>)

    an array of all the channels (text and voice) on this server.



2210
2211
2212
# File 'lib/discordrb/data.rb', line 2210

def channels
  @channels
end

#emojiHash<Integer, Emoji> (readonly) Also known as: emojis

Returns a hash of all the emoji available on this server.

Returns:

  • (Hash<Integer, Emoji>)

    a hash of all the emoji available on this server.



2216
2217
2218
# File 'lib/discordrb/data.rb', line 2216

def emoji
  @emoji
end

#featuresArray<Symbol> (readonly)

Returns the features of the server (eg. "INVITE_SPLASH").

Returns:

  • (Array<Symbol>)

    the features of the server (eg. "INVITE_SPLASH")



2225
2226
2227
# File 'lib/discordrb/data.rb', line 2225

def features
  @features
end

#largetrue, false (readonly) Also known as: large?

it means the members list may be inaccurate for a couple seconds after starting up the bot.

Returns:

  • (true, false)

    whether or not this server is large (members > 100). If it is,



2221
2222
2223
# File 'lib/discordrb/data.rb', line 2221

def large
  @large
end

#member_countInteger (readonly)

Returns the absolute number of members on this server, offline or not.

Returns:

  • (Integer)

    the absolute number of members on this server, offline or not.



2228
2229
2230
# File 'lib/discordrb/data.rb', line 2228

def member_count
  @member_count
end

#ownerMember

Returns The server owner.

Returns:

  • (Member)

    The server owner.



2207
2208
2209
# File 'lib/discordrb/data.rb', line 2207

def owner
  @owner
end

#regionString

Returns the region the server is on (e. g. amsterdam).

Returns:

  • (String)

    the region the server is on (e. g. amsterdam).



2204
2205
2206
# File 'lib/discordrb/data.rb', line 2204

def region
  @region
end

#rolesArray<Role> (readonly)

Returns an array of all the roles created on this server.

Returns:

  • (Array<Role>)

    an array of all the roles created on this server.



2213
2214
2215
# File 'lib/discordrb/data.rb', line 2213

def roles
  @roles
end

#verification_levelSymbol (readonly)

Returns the verification level of the server (:none = none, :low = 'Must have a verified email on their Discord account', :medium = 'Has to be registered with Discord for at least 5 minutes', :high = 'Has to be a member of this server for at least 10 minutes').

Returns:

  • (Symbol)

    the verification level of the server (:none = none, :low = 'Must have a verified email on their Discord account', :medium = 'Has to be registered with Discord for at least 5 minutes', :high = 'Has to be a member of this server for at least 10 minutes').



2231
2232
2233
# File 'lib/discordrb/data.rb', line 2231

def verification_level
  @verification_level
end

#voice_statesHash<Integer => VoiceState> (readonly)

Returns the hash (user ID => voice state) of voice states of members on this server.

Returns:

  • (Hash<Integer => VoiceState>)

    the hash (user ID => voice state) of voice states of members on this server



2240
2241
2242
# File 'lib/discordrb/data.rb', line 2240

def voice_states
  @voice_states
end

Instance Method Details

#any_emoji?true, false Also known as: has_emoji?, emoji?

Returns whether this server has any emoji or not.

Returns:

  • (true, false)

    whether this server has any emoji or not.



2575
2576
2577
# File 'lib/discordrb/data.rb', line 2575

def any_emoji?
  @emoji.any?
end

#ban(user, message_days = 0) ⇒ Object

Bans a user from this server.

Parameters:

  • user (User, #resolve_id)

    The user to ban.

  • message_days (Integer) (defaults to: 0)

    How many days worth of messages sent by the user should be deleted.



2499
2500
2501
# File 'lib/discordrb/data.rb', line 2499

def ban(user, message_days = 0)
  API::Server.ban_user(@bot.token, @id, user.resolve_id, message_days)
end

#bansArray<User>

Returns a list of banned users on this server.

Returns:

  • (Array<User>)

    a list of banned users on this server.



2491
2492
2493
2494
# File 'lib/discordrb/data.rb', line 2491

def bans
  users = JSON.parse(API::Server.bans(@bot.token, @id))
  users.map { |e| User.new(e['user'], @bot) }
end

#create_channel(name, type = 0) ⇒ Channel

Creates a channel on this server with the given name.

Parameters:

  • name (String)

    Name of the channel to create

  • type (Integer) (defaults to: 0)

    Type of channel to create (0: text, 2: voice)

Returns:

  • (Channel)

    the created channel.

Raises:

  • (ArgumentError)

    if type is not 0 or 2



2473
2474
2475
2476
2477
# File 'lib/discordrb/data.rb', line 2473

def create_channel(name, type = 0)
  raise ArgumentError, 'Channel type must be either 0 (text) or 2 (voice)!' unless [0, 2].include?(type)
  response = API::Server.create_channel(@bot.token, @id, name, type)
  Channel.new(JSON.parse(response), @bot)
end

#create_roleRole

Creates a role on this server which can then be modified. It will be initialized (on Discord's side) with the regular role defaults the client uses, i. e. name is "new role", permissions are the default, colour is the default etc.

Returns:

  • (Role)

    the created role.



2483
2484
2485
2486
2487
2488
# File 'lib/discordrb/data.rb', line 2483

def create_role
  response = API::Server.create_role(@bot.token, @id)
  role = Role.new(JSON.parse(response), @bot, self)
  @roles << role
  role
end

#default_channelChannel Also known as: general_channel

Returns The default channel on this server (usually called #general).

Returns:

  • (Channel)

    The default channel on this server (usually called #general)



2275
2276
2277
# File 'lib/discordrb/data.rb', line 2275

def default_channel
  @bot.channel(@id)
end

#deleteObject

Deletes this server. Be aware that this is permanent and impossible to undo, so be careful!



2523
2524
2525
# File 'lib/discordrb/data.rb', line 2523

def delete
  API::Server.delete(@bot.token, @id)
end

#embed?true, false

Returns whether or not the server has widget enabled.

Returns:

  • (true, false)

    whether or not the server has widget enabled



2328
2329
2330
2331
# File 'lib/discordrb/data.rb', line 2328

def embed?
  cache_embed if @embed.nil?
  @embed
end

#icon=(icon) ⇒ Object

Sets the server's icon.

Parameters:

  • icon (String, #read)

    The new icon, in base64-encoded JPG format.



2552
2553
2554
2555
2556
2557
2558
2559
2560
# File 'lib/discordrb/data.rb', line 2552

def icon=(icon)
  if icon.respond_to? :read
    icon_string = 'data:image/jpg;base64,'
    icon_string += Base64.strict_encode64(icon.read)
    update_server_data(icon: icon_string)
  else
    update_server_data(icon: icon)
  end
end

#inspectObject

The inspect method is overwritten to give more useful output



2644
2645
2646
# File 'lib/discordrb/data.rb', line 2644

def inspect
  "<Server name=#{@name} id=#{@id} large=#{@large} region=#{@region} owner=#{@owner} afk_channel_id=#{@afk_channel_id} afk_timeout=#{@afk_timeout}>"
end

#integrationsArray<Integration>

Returns an array of all the integrations connected to this server.

Returns:

  • (Array<Integration>)

    an array of all the integrations connected to this server.



2315
2316
2317
2318
# File 'lib/discordrb/data.rb', line 2315

def integrations
  integration = JSON.parse(API::Server.integrations(@bot.token, @id))
  integration.map { |element| Integration.new(element, @bot, self) }
end

#kick(user) ⇒ Object

Kicks a user from this server.

Parameters:



2511
2512
2513
# File 'lib/discordrb/data.rb', line 2511

def kick(user)
  API::Server.remove_member(@bot.token, @id, user.resolve_id)
end

#leaveObject

Leave the server



2528
2529
2530
# File 'lib/discordrb/data.rb', line 2528

def leave
  API::User.leave_server(@bot.token, @id)
end

#member(id, request = true) ⇒ Object

Gets a member on this server based on user ID

Parameters:

  • id (Integer)

    The user ID to look for

  • request (true, false) (defaults to: true)

    Whether the member should be requested from Discord if it's not cached



2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
# File 'lib/discordrb/data.rb', line 2291

def member(id, request = true)
  id = id.resolve_id
  return @members[id] if member_cached?(id)
  return nil unless request

  member = @bot.member(self, id)
  @members[id] = member
rescue
  nil
end

#membersArray<Member> Also known as: users

Returns an array of all the members on this server.

Returns:

  • (Array<Member>)

    an array of all the members on this server.



2303
2304
2305
2306
2307
2308
2309
2310
# File 'lib/discordrb/data.rb', line 2303

def members
  return @members.values if @chunked

  @bot.debug("Members for server #{@id} not chunked yet - initiating")
  @bot.request_chunks(@id)
  sleep 0.05 until @chunked
  @members.values
end

#move(user, channel) ⇒ Object

Forcibly moves a user into a different voice channel. Only works if the bot has the permission needed.

Parameters:

  • user (User)

    The user to move.

  • channel (Channel)

    The voice channel to move into.



2518
2519
2520
# File 'lib/discordrb/data.rb', line 2518

def move(user, channel)
  API::Server.update_member(@bot.token, @id, user.id, channel_id: channel.id)
end

#name=(name) ⇒ Object

Sets the server's name.

Parameters:

  • name (String)

    The new server name.



2540
2541
2542
# File 'lib/discordrb/data.rb', line 2540

def name=(name)
  update_server_data(name: name)
end

#online_members(include_idle: false, include_bots: true) ⇒ Array<Member> Also known as: online_users

Returns an array of online members on this server.

Parameters:

  • include_idle (true, false) (defaults to: false)

    Whether to count idle members as online.

  • include_bots (true, false) (defaults to: true)

    Whether to include bot accounts in the count.

Returns:

  • (Array<Member>)

    an array of online members on this server.



2336
2337
2338
2339
2340
# File 'lib/discordrb/data.rb', line 2336

def online_members(include_idle: false, include_bots: true)
  @members.values.select do |e|
    ((include_idle ? e.idle? : false) || e.online?) && (include_bots ? true : !e.bot_account?)
  end
end

#role(id) ⇒ Object

Gets a role on this server based on its ID.

Parameters:



2283
2284
2285
2286
# File 'lib/discordrb/data.rb', line 2283

def role(id)
  id = id.resolve_id
  @roles.find { |e| e.id == id }
end

#splash_idString

Returns the hexadecimal ID used to identify this server's splash image for their VIP invite page.

Returns:

  • (String)

    the hexadecimal ID used to identify this server's splash image for their VIP invite page.



2377
2378
2379
# File 'lib/discordrb/data.rb', line 2377

def splash_id
  @splash_id ||= JSON.parse(API::Server.resolve(@bot.token, @id))['splash']
end

#splash_urlString?

Returns the splash image URL for the server's VIP invite page. nil if there is no splash image.

Returns:

  • (String, nil)

    the splash image URL for the server's VIP invite page. nil if there is no splash image.



2383
2384
2385
2386
2387
# File 'lib/discordrb/data.rb', line 2383

def splash_url
  splash_id if @splash_id.nil?
  return nil unless @splash_id
  API.splash_url(@id, @splash_id)
end

#text_channelsArray<Channel>

Returns an array of text channels on this server.

Returns:

  • (Array<Channel>)

    an array of text channels on this server



2345
2346
2347
# File 'lib/discordrb/data.rb', line 2345

def text_channels
  @channels.select(&:text?)
end

#unban(user) ⇒ Object

Unbans a previously banned user from this server.

Parameters:



2505
2506
2507
# File 'lib/discordrb/data.rb', line 2505

def unban(user)
  API::Server.unban_user(@bot.token, @id, user.resolve_id)
end

#voice_channelsArray<Channel>

Returns an array of voice channels on this server.

Returns:

  • (Array<Channel>)

    an array of voice channels on this server



2350
2351
2352
# File 'lib/discordrb/data.rb', line 2350

def voice_channels
  @channels.select(&:voice?)
end

#widget_banner_url(style) ⇒ String?

Returns the widget banner URL to the server that displays the amount of online members, server icon and server name in a stylish way. nil if the widget is not enabled.

Parameters:

  • style (Symbol)

    The style the picture should have. Possible styles are:

    • :banner1 creates a rectangular image with the server name, member count and icon, a "Powered by Discord" message on the bottom and an arrow on the right.
    • :banner2 creates a less tall rectangular image that has the same information as banner1, but the Discord logo on the right - together with the arrow and separated by a diagonal separator.
    • :banner3 creates an image similar in size to banner1, but it has the arrow in the bottom part, next to the Discord logo and with a "Chat now" text.
    • :banner4 creates a tall, almost square, image that prominently features the Discord logo at the top and has a "Join my server" in a pill-style button on the bottom. The information about the server is in the same format as the other three banner styles.
    • :shield creates a very small, long rectangle, of the style you'd find at the top of GitHub README.md files. It features a small version of the Discord logo at the left and the member count at the right.

Returns:

  • (String, nil)

    the widget banner URL to the server that displays the amount of online members, server icon and server name in a stylish way. nil if the widget is not enabled.



2370
2371
2372
2373
2374
# File 'lib/discordrb/data.rb', line 2370

def widget_banner_url(style)
  return nil unless @embed
  cache_embed if @embed.nil?
  API.widget_url(@id, style)
end

#widget_urlString?

Returns the widget URL to the server that displays the amount of online members in a stylish way. nil if the widget is not enabled.

Returns:

  • (String, nil)

    the widget URL to the server that displays the amount of online members in a stylish way. nil if the widget is not enabled.



2356
2357
2358
2359
2360
# File 'lib/discordrb/data.rb', line 2356

def widget_url
  cache_embed if @embed.nil?
  return nil unless @embed
  API.widget_url(@id)
end