Class: Discordrb::Server

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

Overview

A server on Discord

Constant Summary collapse

VERIFICATION_LEVELS =

A map of possible server verification levels to symbol names

{
  none: 0,
  low: 1,
  medium: 2,
  high: 3,
  very_high: 4
}.freeze
NOTIFICATION_LEVELS =

A map of possible message notification levels to symbol names

{
  all_messages: 0,
  only_mentions: 1
}.freeze
FILTER_LEVELS =

A map of possible content filter levels to symbol names

{
  disabled: 0,
  members_without_roles: 1,
  all_members: 2
}.freeze

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


54
55
56
# File 'lib/discordrb/data/server.rb', line 54

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.


33
34
35
# File 'lib/discordrb/data/server.rb', line 33

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.


39
40
41
# File 'lib/discordrb/data/server.rb', line 39

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


48
49
50
# File 'lib/discordrb/data/server.rb', line 48

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,


44
45
46
# File 'lib/discordrb/data/server.rb', line 44

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.


51
52
53
# File 'lib/discordrb/data/server.rb', line 51

def member_count
  @member_count
end

#ownerMember

Returns The server owner.

Returns:

  • (Member)

    The server owner.


30
31
32
# File 'lib/discordrb/data/server.rb', line 30

def owner
  @owner
end

#region_idString (readonly)

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

Returns:

  • (String)

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


27
28
29
# File 'lib/discordrb/data/server.rb', line 27

def region_id
  @region_id
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.


36
37
38
# File 'lib/discordrb/data/server.rb', line 36

def roles
  @roles
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


57
58
59
# File 'lib/discordrb/data/server.rb', line 57

def voice_states
  @voice_states
end

Instance Method Details

#add_emoji(name, image, roles = [], reason: nil) ⇒ Emoji

Adds a new custom emoji on this server.

Parameters:

  • name (String)

    The name of emoji to create.

  • image (String, #read)

    A base64 encoded string with the image data, or an object that responds to #read, such as File.

  • roles (Array<Role, String, Integer>) (defaults to: [])

    An array of roles, or role IDs to be whitelisted for this emoji.

  • reason (String) (defaults to: nil)

    The reason the for the creation of this emoji.

Returns:

  • (Emoji)

    The emoji that has been added.


518
519
520
521
522
523
524
525
526
527
528
# File 'lib/discordrb/data/server.rb', line 518

def add_emoji(name, image, roles = [], reason: nil)
  image_string = image
  if image.respond_to? :read
    image_string = 'data:image/jpg;base64,'
    image_string += Base64.strict_encode64(image.read)
  end

  data = JSON.parse(API::Server.add_emoji(@bot.token, @id, image_string, name, roles.map(&:resolve_id), reason))
  new_emoji = Emoji.new(data)
  @emoji[new_emoji.id] = new_emoji
end

#add_member_using_token(user, access_token, nick: nil, roles: [], deaf: false, mute: false) ⇒ Member

Note:

Your bot must be present in this server, and have permission to create instant invites for this to work.

Adds a member to this guild that has granted this bot's application an OAuth2 access token with the guilds.join scope. For more information about Discord's OAuth2 implementation, see: https://discordapp.com/developers/docs/topics/oauth2

Parameters:

  • user (User, String, Integer)

    the user, or ID of the user to add to this server

  • access_token (String)

    the OAuth2 Bearer token that has been granted the guilds.join scope

  • nick (String) (defaults to: nil)

    the nickname to give this member upon joining

  • roles (Role, Array<Role, String, Integer>) (defaults to: [])

    the role (or roles) to give this member upon joining

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

    whether this member will be server deafened upon joining

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

    whether this member will be server muted upon joining

Returns:

  • (Member)

    the created member


275
276
277
278
279
280
# File 'lib/discordrb/data/server.rb', line 275

def add_member_using_token(user, access_token, nick: nil, roles: [], deaf: false, mute: false)
  user_id = user.resolve_id
  roles = roles.is_a?(Array) ? roles.map(&:resolve_id) : [roles.resolve_id]
  response = JSON.parse(API::Server.add_member(@bot.token, @id, user_id, access_token, nick, roles, deaf, mute))
  add_member Member.new(response, self, @bot)
end

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


773
774
775
# File 'lib/discordrb/data/server.rb', line 773

def afk_channel
  @bot.channel(@afk_channel_id) if @afk_channel_id
end

#afk_channel=(afk_channel) ⇒ Object

Sets the server's AFK channel.

Parameters:

  • afk_channel (Channel, nil)

    The new AFK channel, or nil if there should be none set.


645
646
647
# File 'lib/discordrb/data/server.rb', line 645

def afk_channel=(afk_channel)
  update_server_data(afk_channel_id: afk_channel.resolve_id)
end

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


733
734
735
# File 'lib/discordrb/data/server.rb', line 733

def any_emoji?
  @emoji.any?
end

#audit_logs(action: nil, user: nil, limit: 50, before: nil) ⇒ AuditLogs

Returns The server's audit logs.

Parameters:

  • action (Symbol) (defaults to: nil)

    The action to only include.

  • user (User, String, Integer) (defaults to: nil)

    The user, or their ID, to filter entries to.

  • limit (Integer) (defaults to: 50)

    The amount of entries to limit it to.

  • before (Entry, String, Integer) (defaults to: nil)

    The entry, or its ID, to use to not include all entries after it.

Returns:


171
172
173
174
175
176
177
178
# File 'lib/discordrb/data/server.rb', line 171

def audit_logs(action: nil, user: nil, limit: 50, before: nil)
  raise 'Invalid audit log action!' if action && AuditLogs::ACTIONS.key(action).nil?

  action = AuditLogs::ACTIONS.key(action)
  user = user.resolve_id if user
  before = before.resolve_id if before
  AuditLogs.new(self, @bot, JSON.parse(API::Server.audit_logs(@bot.token, @id, limit, user, action, before)))
end

#available_voice_regionsArray<VoiceRegion>

Returns collection of available voice regions to this guild.

Returns:

  • (Array<VoiceRegion>)

    collection of available voice regions to this guild


610
611
612
613
614
615
616
617
# File 'lib/discordrb/data/server.rb', line 610

def available_voice_regions
  return @available_voice_regions if @available_voice_regions

  @available_voice_regions = {}

  data = JSON.parse API::Server.regions(@bot.token, @id)
  @available_voice_regions = data.map { |e| VoiceRegion.new e }
end

#ban(user, message_days = 0, reason: nil) ⇒ Object

Bans a user from this server.

Parameters:

  • user (User, String, Integer)

    The user to ban.

  • message_days (Integer) (defaults to: 0)

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

  • reason (String) (defaults to: nil)

    The reason the user is being banned.


562
563
564
# File 'lib/discordrb/data/server.rb', line 562

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

#bansArray<ServerBan>

Returns a list of banned users on this server and the reason they were banned.

Returns:

  • (Array<ServerBan>)

    a list of banned users on this server and the reason they were banned.


551
552
553
554
555
556
# File 'lib/discordrb/data/server.rb', line 551

def bans
  response = JSON.parse(API::Server.bans(@bot.token, @id))
  response.map do |e|
    ServerBan.new(self, User.new(e['user'], @bot), e['reason'])
  end
end

#begin_prune(days, reason = nil) ⇒ Integer Also known as: prune

Prunes (kicks) an amount of members for inactivity

Parameters:

  • days (Integer)

    the number of days to consider for inactivity (between 1 and 30)

  • reason (String) (defaults to: nil)

    The reason the for the prune.

Returns:

  • (Integer)

    the number of members removed at the end of the operation

Raises:

  • (ArgumentError)

    if days is not between 1 and 30 (inclusive)


298
299
300
301
302
303
# File 'lib/discordrb/data/server.rb', line 298

def begin_prune(days, reason = nil)
  raise ArgumentError, 'Days must be between 1 and 30' unless days.between?(1, 30)

  response = JSON.parse API::Server.begin_prune(@bot.token, @id, days, reason)
  response['pruned']
end

#botMember

Returns the bot's own Member on this server.

Returns:

  • (Member)

    the bot's own Member on this server


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

def bot
  member(@bot.profile)
end

#bot_membersArray<Member>

Returns an array of all the bot members on this server.

Returns:

  • (Array<Member>)

    an array of all the bot members on this server.


146
147
148
# File 'lib/discordrb/data/server.rb', line 146

def bot_members
  members.select(&:bot_account?)
end

#categoriesArray<Channel>

Returns an array of category channels on this server.

Returns:

  • (Array<Channel>)

    an array of category channels on this server


318
319
320
# File 'lib/discordrb/data/server.rb', line 318

def categories
  @channels.select(&:category?)
end

#create_channel(name, type = 0, topic: nil, bitrate: nil, user_limit: nil, permission_overwrites: nil, parent: nil, nsfw: false, rate_limit_per_user: nil, position: nil, reason: nil) ⇒ Channel

Note:

If parent is provided, permission overwrites have the follow behavior:

  1. If overwrites is null, the new channel inherits the parent's permissions.
  2. If overwrites is [], the new channel inherits the parent's permissions.
  3. If you supply one or more overwrites, the channel will be created with those permissions and ignore the parents.

Creates a channel on this server with the given name.

Parameters:

  • name (String)

    Name of the channel to create

  • type (Integer, Symbol) (defaults to: 0)

    Type of channel to create (0: text, 2: voice, 4: category, 5: news, 6: store)

  • topic (String) (defaults to: nil)

    the topic of this channel, if it will be a text channel

  • bitrate (Integer) (defaults to: nil)

    the bitrate of this channel, if it will be a voice channel

  • user_limit (Integer) (defaults to: nil)

    the user limit of this channel, if it will be a voice channel

  • permission_overwrites (Array<Hash>, Array<Overwrite>) (defaults to: nil)

    permission overwrites for this channel

  • parent (Channel, String, Integer) (defaults to: nil)

    parent category, or its ID, for this channel to be created in.

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

    whether this channel should be created as nsfw

  • rate_limit_per_user (Integer) (defaults to: nil)

    how many seconds users need to wait in between messages.

  • reason (String) (defaults to: nil)

    The reason the for the creation of this channel.

Returns:

  • (Channel)

    the created channel.

Raises:

  • (ArgumentError)

    if type is not 0 (text), 2 (voice), 4 (category), 5 (news), or 6 (store)


474
475
476
477
478
479
480
481
482
# File 'lib/discordrb/data/server.rb', line 474

def create_channel(name, type = 0, topic: nil, bitrate: nil, user_limit: nil, permission_overwrites: nil, parent: nil, nsfw: false, rate_limit_per_user: nil, position: nil, reason: nil)
  type = Channel::TYPES[type] if type.is_a?(Symbol)
  raise ArgumentError, 'Channel type must be either 0 (text), 2 (voice), 4 (category), news (5), or store (6)!' unless [0, 2, 4, 5, 6].include?(type)

  permission_overwrites.map! { |e| e.is_a?(Overwrite) ? e.to_hash : e } if permission_overwrites.is_a?(Array)
  parent_id = parent.respond_to?(:resolve_id) ? parent.resolve_id : nil
  response = API::Server.create_channel(@bot.token, @id, name, type, topic, bitrate, user_limit, permission_overwrites, parent_id, nsfw, rate_limit_per_user, position, reason)
  Channel.new(JSON.parse(response), @bot)
end

#create_role(name: 'new role', colour: 0, hoist: false, mentionable: false, permissions: 104_324_161, reason: nil) ⇒ Role

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

Parameters:

  • name (String) (defaults to: 'new role')

    Name of the role to create

  • colour (Integer, ColourRGB, #combined) (defaults to: 0)

    The roles colour

  • hoist (true, false) (defaults to: false)
  • mentionable (true, false) (defaults to: false)
  • permissions (Integer, Array<Symbol>, Permissions, #bits) (defaults to: 104_324_161)

    The permissions to write to the new role.

  • reason (String) (defaults to: nil)

    The reason the for the creation of this role.

Returns:

  • (Role)

    the created role.


494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/discordrb/data/server.rb', line 494

def create_role(name: 'new role', colour: 0, hoist: false, mentionable: false, permissions: 104_324_161, reason: nil)
  colour = colour.respond_to?(:combined) ? colour.combined : colour

  permissions = if permissions.is_a?(Array)
                  Permissions.bits(permissions)
                elsif permissions.respond_to?(:bits)
                  permissions.bits
                else
                  permissions
                end

  response = API::Server.create_role(@bot.token, @id, name, colour, hoist, mentionable, permissions, reason)

  role = Role.new(JSON.parse(response), @bot, self)
  @roles << role
  role
end

#default_channel(send_messages = false) ⇒ Channel? Also known as: general_channel

The default channel is the text channel on this server with the highest position that the bot has Read Messages permission on.

Parameters:

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

    whether to additionally consider if the bot has Send Messages permission

Returns:

  • (Channel, nil)

    The default channel on this server, or nil if there are no channels that the bot can read.


94
95
96
97
98
99
100
101
102
103
# File 'lib/discordrb/data/server.rb', line 94

def default_channel(send_messages = false)
  bot_member = member(@bot.profile)
  text_channels.sort_by { |e| [e.position, e.id] }.find do |e|
    if send_messages
      bot_member.can_read_messages?(e) && bot_member.can_send_messages?(e)
    else
      bot_member.can_read_messages?(e)
    end
  end
end

#default_message_notificationsSymbol

Returns the default message notifications settings of the server (:all = 'All messages', :mentions = 'Only @mentions').

Returns:

  • (Symbol)

    the default message notifications settings of the server (:all = 'All messages', :mentions = 'Only @mentions').


690
691
692
# File 'lib/discordrb/data/server.rb', line 690

def default_message_notifications
  NOTIFICATION_LEVELS.key @default_message_notifications
end

#default_message_notifications=(notification_level) ⇒ Object Also known as: notification_level=

Sets the default message notification level

Parameters:


696
697
698
699
700
# File 'lib/discordrb/data/server.rb', line 696

def default_message_notifications=(notification_level)
  notification_level = NOTIFICATION_LEVELS[notification_level] if notification_level.is_a?(Symbol)

  update_server_data(default_message_notifications: notification_level)
end

#deleteObject

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


588
589
590
# File 'lib/discordrb/data/server.rb', line 588

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

#delete_emoji(emoji, reason: nil) ⇒ Object

Delete a custom emoji on this server

Parameters:

  • emoji (Emoji, String, Integer)

    The emoji or emoji ID to be deleted.

  • reason (String) (defaults to: nil)

    The reason the for the deletion of this emoji.


533
534
535
# File 'lib/discordrb/data/server.rb', line 533

def delete_emoji(emoji, reason: nil)
  API::Server.delete_emoji(@bot.token, @id, emoji.resolve_id, reason)
end

#edit_emoji(emoji, name: nil, roles: nil, reason: nil) ⇒ Emoji

Changes the name and/or role whitelist of an emoji on this server.

Parameters:

  • emoji (Emoji, String, Integer)

    The emoji or emoji ID to edit.

  • name (String) (defaults to: nil)

    The new name for the emoji.

  • roles (Array<Role, String, Integer>) (defaults to: nil)

    A new array of roles, or role IDs, to whitelist.

  • reason (String) (defaults to: nil)

    The reason for the editing of this emoji.

Returns:

  • (Emoji)

    The edited emoji.


543
544
545
546
547
548
# File 'lib/discordrb/data/server.rb', line 543

def edit_emoji(emoji, name: nil, roles: nil, reason: nil)
  emoji = @emoji[emoji.resolve_id]
  data = JSON.parse(API::Server.edit_emoji(@bot.token, @id, emoji.resolve_id, name || emoji.name, (roles || emoji.roles).map(&:resolve_id), reason))
  new_emoji = Emoji.new(data)
  @emoji[new_emoji.id] = new_emoji
end

#embed_channelChannel? Also known as: widget_channel

Returns the channel the server embed will make an invite for.

Returns:

  • (Channel, nil)

    the channel the server embed will make an invite for.


199
200
201
202
# File 'lib/discordrb/data/server.rb', line 199

def embed_channel
  cache_embed_data if @embed_enabled.nil?
  @bot.channel(@embed_channel_id) if @embed_channel_id
end

#embed_channel=(channel) ⇒ Object Also known as: widget_channel=

Changes the channel on the server's embed (widget)

Parameters:


224
225
226
# File 'lib/discordrb/data/server.rb', line 224

def embed_channel=(channel)
  modify_embed(embed?, channel)
end

#embed_enabled=(value) ⇒ Object Also known as: widget_enabled=

Sets whether this server's embed (widget) is enabled

Parameters:

  • value (true, false)

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

def embed_enabled=(value)
  modify_embed(value, embed_channel)
end

#embed_enabled?true, false Also known as: widget_enabled, widget?, embed?

Returns whether or not the server has widget enabled.

Returns:

  • (true, false)

    whether or not the server has widget enabled


190
191
192
193
# File 'lib/discordrb/data/server.rb', line 190

def embed_enabled?
  cache_embed_data if @embed_enabled.nil?
  @embed_enabled
end

#everyone_roleRole

Returns The @everyone role on this server.

Returns:

  • (Role)

    The @everyone role on this server


108
109
110
# File 'lib/discordrb/data/server.rb', line 108

def everyone_role
  role(@id)
end

#explicit_content_filterSymbol Also known as: content_filter_level

Returns the explicit content filter level of the server (:none = 'Don't scan any messages.', :exclude_roles = 'Scan messages for members without a role.', :all = 'Scan messages sent by all members.').

Returns:

  • (Symbol)

    the explicit content filter level of the server (:none = 'Don't scan any messages.', :exclude_roles = 'Scan messages for members without a role.', :all = 'Scan messages sent by all members.').


718
719
720
# File 'lib/discordrb/data/server.rb', line 718

def explicit_content_filter
  FILTER_LEVELS.key @explicit_content_filter
end

#explicit_content_filter=(filter_level) ⇒ Object

Sets the server content filter.

Parameters:


726
727
728
729
730
# File 'lib/discordrb/data/server.rb', line 726

def explicit_content_filter=(filter_level)
  filter_level = FILTER_LEVELS[filter_level] if filter_level.is_a?(Symbol)

  update_server_data(explicit_content_filter: filter_level)
end

#icon=(icon) ⇒ Object

Sets the server's icon.

Parameters:

  • icon (String, #read)

    The new icon, in base64-encoded JPG format.


633
634
635
636
637
638
639
640
641
# File 'lib/discordrb/data/server.rb', line 633

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_id: icon_string)
  else
    update_server_data(icon_id: icon)
  end
end

#inspectObject

The inspect method is overwritten to give more useful output


832
833
834
# File 'lib/discordrb/data/server.rb', line 832

def inspect
  "<Server name=#{@name} id=#{@id} large=#{@large} region=#{@region} owner=#{@owner} afk_channel_id=#{@afk_channel_id} system_channel_id=#{@system_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.


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

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

#invitesArray<Invite>

Requests a list of Invites to the server.

Returns:

  • (Array<Invite>)

    invites to the server.


749
750
751
752
# File 'lib/discordrb/data/server.rb', line 749

def invites
  invites = JSON.parse(API::Server.invites(@bot.token, @id))
  invites.map { |invite| Invite.new(invite, @bot) }
end

#kick(user, reason = nil) ⇒ Object

Kicks a user from this server.

Parameters:

  • user (User, String, Integer)

    The user to kick.

  • reason (String) (defaults to: nil)

    The reason the user is being kicked.


576
577
578
# File 'lib/discordrb/data/server.rb', line 576

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

#leaveObject

Leave the server.


593
594
595
# File 'lib/discordrb/data/server.rb', line 593

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


122
123
124
125
126
127
128
129
130
131
# File 'lib/discordrb/data/server.rb', line 122

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 unless member.nil?
rescue StandardError
  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.


134
135
136
137
138
139
140
141
# File 'lib/discordrb/data/server.rb', line 134

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

#modify_embed(enabled, channel, reason = nil) ⇒ Object Also known as: modify_widget

Changes the channel on the server's embed (widget), and sets whether it is enabled.

Parameters:

  • enabled (true, false)

    whether the embed (widget) is enabled

  • channel (Channel, String, Integer)

    the channel, or its ID, to be referenced by the embed

  • reason (String, nil) (defaults to: nil)

    the reason to be shown in the audit log for this action


243
244
245
246
247
248
249
# File 'lib/discordrb/data/server.rb', line 243

def modify_embed(enabled, channel, reason = nil)
  cache_embed_data if @embed_enabled.nil?
  channel_id = channel ? channel.resolve_id : @embed_channel_id
  response = JSON.parse(API::Server.modify_embed(@bot.token, @id, enabled, channel_id, reason))
  @embed_enabled = response['enabled']
  @embed_channel_id = response['channel_id']
end

#move(user, channel) ⇒ Object

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

Parameters:


583
584
585
# File 'lib/discordrb/data/server.rb', line 583

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

#name=(name) ⇒ Object

Sets the server's name.

Parameters:

  • name (String)

    The new server name.


605
606
607
# File 'lib/discordrb/data/server.rb', line 605

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

#non_bot_membersArray<Member>

Returns an array of all the non bot members on this server.

Returns:

  • (Array<Member>)

    an array of all the non bot members on this server.


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

def non_bot_members
  members.reject(&:bot_account?)
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.


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

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

#orphan_channelsArray<Channel>

Returns an array of channels on this server that are not in a category.

Returns:

  • (Array<Channel>)

    an array of channels on this server that are not in a category


323
324
325
# File 'lib/discordrb/data/server.rb', line 323

def orphan_channels
  @channels.reject { |c| c.parent || c.category? }
end

#prune_count(days) ⇒ Integer

Returns the amount of members that are candidates for pruning

Parameters:

  • days (Integer)

    the number of days to consider for inactivity

Returns:

  • (Integer)

    number of members to be removed

Raises:

  • (ArgumentError)

    if days is not between 1 and 30 (inclusive)


286
287
288
289
290
291
# File 'lib/discordrb/data/server.rb', line 286

def prune_count(days)
  raise ArgumentError, 'Days must be between 1 and 30' unless days.between?(1, 30)

  response = JSON.parse API::Server.prune_count(@bot.token, @id, days)
  response['pruned']
end

#regionVoiceRegion?

Note:

This may return nil if this server's voice region is deprecated.

Returns voice region data for this server's region.

Returns:

  • (VoiceRegion, nil)

    voice region data for this server's region


621
622
623
# File 'lib/discordrb/data/server.rb', line 621

def region
  available_voice_regions.find { |e| e.id == @region_id }
end

#region=(region) ⇒ Object

Moves the server to another region. This will cause a voice interruption of at most a second.

Parameters:

  • region (String)

    The new region the server should be in.


627
628
629
# File 'lib/discordrb/data/server.rb', line 627

def region=(region)
  update_server_data(region: region.to_s)
end

#role(id) ⇒ Object

Gets a role on this server based on its ID.

Parameters:


114
115
116
117
# File 'lib/discordrb/data/server.rb', line 114

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

#set_embed_channel(channel, reason = nil) ⇒ Object Also known as: set_widget_channel

Changes the channel on the server's embed (widget)

Parameters:

  • channel (Channel, String, Integer)

    the channel, or its ID, to be referenced by the embed

  • reason (String, nil) (defaults to: nil)

    the reason to be shown in the audit log for this action


233
234
235
# File 'lib/discordrb/data/server.rb', line 233

def set_embed_channel(channel, reason = nil)
  modify_embed(embed?, channel, reason)
end

#set_embed_enabled(value, reason = nil) ⇒ Object Also known as: set_widget_enabled

Sets whether this server's embed (widget) is enabled

Parameters:

  • value (true, false)
  • reason (String, nil) (defaults to: nil)

    the reason to be shown in the audit log for this action


216
217
218
# File 'lib/discordrb/data/server.rb', line 216

def set_embed_enabled(value, reason = nil)
  modify_embed(value, embed_channel, reason)
end

#splash=(splash_hash) ⇒ Object

Sets the server splash

Parameters:

  • splash_hash (String)

    The splash hash


706
707
708
# File 'lib/discordrb/data/server.rb', line 706

def splash=(splash_hash)
  update_server_data(splash: splash_hash)
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.


352
353
354
# File 'lib/discordrb/data/server.rb', line 352

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.


358
359
360
361
362
363
# File 'lib/discordrb/data/server.rb', line 358

def splash_url
  splash_id if @splash_id.nil?
  return nil unless @splash_id

  API.splash_url(@id, @splash_id)
end

#system_channelChannel?

Returns the system channel (used for automatic welcome messages) of a server, or nil if none is set.

Returns:

  • (Channel, nil)

    the system channel (used for automatic welcome messages) of a server, or nil if none is set.


778
779
780
# File 'lib/discordrb/data/server.rb', line 778

def system_channel
  @bot.channel(@system_channel_id) if @system_channel_id
end

#system_channel=(system_channel) ⇒ Object

Sets the server's system channel.

Parameters:

  • system_channel (Channel, String, Integer, nil)

    The new system channel, or nil should it be disabled.


651
652
653
# File 'lib/discordrb/data/server.rb', line 651

def system_channel=(system_channel)
  update_server_data(system_channel_id: system_channel.resolve_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


308
309
310
# File 'lib/discordrb/data/server.rb', line 308

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

#unban(user, reason = nil) ⇒ Object

Unbans a previously banned user from this server.

Parameters:

  • user (User, String, Integer)

    The user to unban.

  • reason (String) (defaults to: nil)

    The reason the user is being unbanned.


569
570
571
# File 'lib/discordrb/data/server.rb', line 569

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

#verification_levelSymbol

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', :very_high = 'Must have a verified phone on their Discord account').

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', :very_high = 'Must have a verified phone on their Discord account').


671
672
673
# File 'lib/discordrb/data/server.rb', line 671

def verification_level
  VERIFICATION_LEVELS.key @verification_level
end

#verification_level=(level) ⇒ Object

Sets the verification level of the server

Parameters:


677
678
679
680
681
# File 'lib/discordrb/data/server.rb', line 677

def verification_level=(level)
  level = VERIFICATION_LEVELS[level] if level.is_a?(Symbol)

  update_server_data(verification_level: level)
end

#voice_channelsArray<Channel>

Returns an array of voice channels on this server.

Returns:

  • (Array<Channel>)

    an array of voice channels on this server


313
314
315
# File 'lib/discordrb/data/server.rb', line 313

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

#webhooksArray<Webhook>

Requests a list of Webhooks on the server.

Returns:

  • (Array<Webhook>)

    webhooks on the server.


742
743
744
745
# File 'lib/discordrb/data/server.rb', line 742

def webhooks
  webhooks = JSON.parse(API::Server.webhooks(@bot.token, @id))
  webhooks.map { |webhook| Webhook.new(webhook, @bot) }
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.


344
345
346
347
348
349
# File 'lib/discordrb/data/server.rb', line 344

def widget_banner_url(style)
  update_data if @embed_enabled.nil?
  return unless @embed_enabled

  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.


329
330
331
332
333
334
# File 'lib/discordrb/data/server.rb', line 329

def widget_url
  update_data if @embed_enabled.nil?
  return unless @embed_enabled

  API.widget_url(@id)
end