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

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



2032
2033
2034
# File 'lib/discordrb/data.rb', line 2032

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.



2029
2030
2031
# File 'lib/discordrb/data.rb', line 2029

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.



2005
2006
2007
# File 'lib/discordrb/data.rb', line 2005

def channels
  @channels
end

#emojiArray<Emoji> (readonly) Also known as: emojis

Returns an array of all the emoji available on this server.

Returns:

  • (Array<Emoji>)

    an array of all the emoji available on this server.



2011
2012
2013
# File 'lib/discordrb/data.rb', line 2011

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")



2020
2021
2022
# File 'lib/discordrb/data.rb', line 2020

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,



2016
2017
2018
# File 'lib/discordrb/data.rb', line 2016

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.



2023
2024
2025
# File 'lib/discordrb/data.rb', line 2023

def member_count
  @member_count
end

#ownerMember

Returns The server owner.

Returns:

  • (Member)

    The server owner.



2002
2003
2004
# File 'lib/discordrb/data.rb', line 2002

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).



1999
2000
2001
# File 'lib/discordrb/data.rb', line 1999

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.



2008
2009
2010
# File 'lib/discordrb/data.rb', line 2008

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').



2026
2027
2028
# File 'lib/discordrb/data.rb', line 2026

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



2035
2036
2037
# File 'lib/discordrb/data.rb', line 2035

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.



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

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.



2294
2295
2296
# File 'lib/discordrb/data.rb', line 2294

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.



2286
2287
2288
2289
# File 'lib/discordrb/data.rb', line 2286

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



2268
2269
2270
2271
2272
# File 'lib/discordrb/data.rb', line 2268

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.



2278
2279
2280
2281
2282
2283
# File 'lib/discordrb/data.rb', line 2278

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)



2070
2071
2072
# File 'lib/discordrb/data.rb', line 2070

def default_channel
  @bot.channel(@id)
end

#deleteObject

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



2318
2319
2320
# File 'lib/discordrb/data.rb', line 2318

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



2122
2123
2124
2125
# File 'lib/discordrb/data.rb', line 2122

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.



2347
2348
2349
2350
2351
2352
2353
2354
2355
# File 'lib/discordrb/data.rb', line 2347

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



2415
2416
2417
# File 'lib/discordrb/data.rb', line 2415

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.



2109
2110
2111
2112
# File 'lib/discordrb/data.rb', line 2109

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:



2306
2307
2308
# File 'lib/discordrb/data.rb', line 2306

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

#leaveObject

Leave the server



2323
2324
2325
# File 'lib/discordrb/data.rb', line 2323

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



2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
# File 'lib/discordrb/data.rb', line 2085

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.



2097
2098
2099
2100
2101
2102
2103
2104
# File 'lib/discordrb/data.rb', line 2097

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.



2313
2314
2315
# File 'lib/discordrb/data.rb', line 2313

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.



2335
2336
2337
# File 'lib/discordrb/data.rb', line 2335

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.



2130
2131
2132
2133
2134
# File 'lib/discordrb/data.rb', line 2130

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:

  • id (Integer)

    The role ID to look for.



2078
2079
2080
# File 'lib/discordrb/data.rb', line 2078

def role(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.



2171
2172
2173
2174
# File 'lib/discordrb/data.rb', line 2171

def splash_id
  @splash_id = JSON.parse(API.server(@bot.token, @id))['splash'] if @splash_id.nil?
  @splash_id
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.



2178
2179
2180
2181
2182
# File 'lib/discordrb/data.rb', line 2178

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



2139
2140
2141
# File 'lib/discordrb/data.rb', line 2139

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

#unban(user) ⇒ Object

Unbans a previously banned user from this server.

Parameters:



2300
2301
2302
# File 'lib/discordrb/data.rb', line 2300

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



2144
2145
2146
# File 'lib/discordrb/data.rb', line 2144

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.



2164
2165
2166
2167
2168
# File 'lib/discordrb/data.rb', line 2164

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.



2150
2151
2152
2153
2154
# File 'lib/discordrb/data.rb', line 2150

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