Class: Discordrb::Server

Inherits:
Object
  • Object
show all
Includes:
IDObject, ServerAttributes
Defined in:
lib/discordrb/data.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



2897
2898
2899
# File 'lib/discordrb/data.rb', line 2897

def afk_timeout
  @afk_timeout
end

#channelsArray<Channel> (readonly)



2876
2877
2878
# File 'lib/discordrb/data.rb', line 2876

def channels
  @channels
end

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



2882
2883
2884
# File 'lib/discordrb/data.rb', line 2882

def emoji
  @emoji
end

#featuresArray<Symbol> (readonly)



2891
2892
2893
# File 'lib/discordrb/data.rb', line 2891

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.



2887
2888
2889
# File 'lib/discordrb/data.rb', line 2887

def large
  @large
end

#member_countInteger (readonly)



2894
2895
2896
# File 'lib/discordrb/data.rb', line 2894

def member_count
  @member_count
end

#ownerMember



2873
2874
2875
# File 'lib/discordrb/data.rb', line 2873

def owner
  @owner
end

#region_idString (readonly)



2870
2871
2872
# File 'lib/discordrb/data.rb', line 2870

def region_id
  @region_id
end

#rolesArray<Role> (readonly)



2879
2880
2881
# File 'lib/discordrb/data.rb', line 2879

def roles
  @roles
end

#voice_statesHash<Integer => VoiceState> (readonly)



2900
2901
2902
# File 'lib/discordrb/data.rb', line 2900

def voice_states
  @voice_states
end

Instance Method Details

#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



3103
3104
3105
3106
3107
3108
# File 'lib/discordrb/data.rb', line 3103

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?



3563
3564
3565
# File 'lib/discordrb/data.rb', line 3563

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

#afk_channel=(afk_channel) ⇒ Object

Sets the server's AFK channel.



3435
3436
3437
# File 'lib/discordrb/data.rb', line 3435

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?



3523
3524
3525
# File 'lib/discordrb/data.rb', line 3523

def any_emoji?
  @emoji.any?
end

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



2999
3000
3001
3002
3003
3004
3005
3006
# File 'lib/discordrb/data.rb', line 2999

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>



3400
3401
3402
3403
3404
3405
3406
3407
# File 'lib/discordrb/data.rb', line 3400

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.



3352
3353
3354
# File 'lib/discordrb/data.rb', line 3352

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

#bansArray<ServerBan>



3341
3342
3343
3344
3345
3346
# File 'lib/discordrb/data.rb', line 3341

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

Raises:

  • (ArgumentError)

    if days is not between 1 and 30 (inclusive)



3126
3127
3128
3129
3130
3131
# File 'lib/discordrb/data.rb', line 3126

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

#categoriesArray<Channel>



3146
3147
3148
# File 'lib/discordrb/data.rb', line 3146

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

Raises:

  • (ArgumentError)

    if type is not 0 (text), 2 (voice), or 4 (category)



3302
3303
3304
3305
3306
3307
3308
3309
3310
# File 'lib/discordrb/data.rb', line 3302

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, reason: nil)
  type = Channel::TYPES[type] if type.is_a?(Symbol)
  raise ArgumentError, 'Channel type must be either 0 (text), 2 (voice), or 4 (category)!' unless [0, 2, 4].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, 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.



3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
# File 'lib/discordrb/data.rb', line 3322

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.



2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
# File 'lib/discordrb/data.rb', line 2937

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



3480
3481
3482
# File 'lib/discordrb/data.rb', line 3480

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



3486
3487
3488
3489
3490
# File 'lib/discordrb/data.rb', line 3486

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!



3378
3379
3380
# File 'lib/discordrb/data.rb', line 3378

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

#embed_channelChannel? Also known as: widget_channel



3027
3028
3029
3030
# File 'lib/discordrb/data.rb', line 3027

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)



3052
3053
3054
# File 'lib/discordrb/data.rb', line 3052

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



3035
3036
3037
# File 'lib/discordrb/data.rb', line 3035

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

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



3018
3019
3020
3021
# File 'lib/discordrb/data.rb', line 3018

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

#everyone_roleRole



2951
2952
2953
# File 'lib/discordrb/data.rb', line 2951

def everyone_role
  role(@id)
end

#explicit_content_filterSymbol Also known as: content_filter_level



3508
3509
3510
# File 'lib/discordrb/data.rb', line 3508

def explicit_content_filter
  FILTER_LEVELS.key @explicit_content_filter
end

#explicit_content_filter=(filter_level) ⇒ Object

Sets the server content filter.



3516
3517
3518
3519
3520
# File 'lib/discordrb/data.rb', line 3516

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.



3423
3424
3425
3426
3427
3428
3429
3430
3431
# File 'lib/discordrb/data.rb', line 3423

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



3622
3623
3624
# File 'lib/discordrb/data.rb', line 3622

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>



2989
2990
2991
2992
# File 'lib/discordrb/data.rb', line 2989

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.



3539
3540
3541
3542
# File 'lib/discordrb/data.rb', line 3539

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.



3366
3367
3368
# File 'lib/discordrb/data.rb', line 3366

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

#leaveObject

Leave the server.



3383
3384
3385
# File 'lib/discordrb/data.rb', line 3383

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



2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
# File 'lib/discordrb/data.rb', line 2965

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



2977
2978
2979
2980
2981
2982
2983
2984
# File 'lib/discordrb/data.rb', line 2977

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.



3071
3072
3073
3074
3075
3076
3077
# File 'lib/discordrb/data.rb', line 3071

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.



3373
3374
3375
# File 'lib/discordrb/data.rb', line 3373

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.



3395
3396
3397
# File 'lib/discordrb/data.rb', line 3395

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

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



3084
3085
3086
3087
3088
# File 'lib/discordrb/data.rb', line 3084

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>



3151
3152
3153
# File 'lib/discordrb/data.rb', line 3151

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

Raises:

  • (ArgumentError)

    if days is not between 1 and 30 (inclusive)



3114
3115
3116
3117
3118
3119
# File 'lib/discordrb/data.rb', line 3114

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.



3411
3412
3413
# File 'lib/discordrb/data.rb', line 3411

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.



3417
3418
3419
# File 'lib/discordrb/data.rb', line 3417

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

#role(id) ⇒ Object

Gets a role on this server based on its ID.



2957
2958
2959
2960
# File 'lib/discordrb/data.rb', line 2957

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)



3061
3062
3063
# File 'lib/discordrb/data.rb', line 3061

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



3044
3045
3046
# File 'lib/discordrb/data.rb', line 3044

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

#splash=(splash_hash) ⇒ Object

Sets the server splash



3496
3497
3498
# File 'lib/discordrb/data.rb', line 3496

def splash=(splash_hash)
  update_server_data(splash: splash_hash)
end

#splash_idString



3180
3181
3182
# File 'lib/discordrb/data.rb', line 3180

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

#splash_urlString?



3186
3187
3188
3189
3190
3191
# File 'lib/discordrb/data.rb', line 3186

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

  API.splash_url(@id, @splash_id)
end

#system_channelChannel?



3568
3569
3570
# File 'lib/discordrb/data.rb', line 3568

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

#system_channel=(system_channel) ⇒ Object

Sets the server's system channel.



3441
3442
3443
# File 'lib/discordrb/data.rb', line 3441

def system_channel=(system_channel)
  update_server_data(system_channel_id: system_channel.resolve_id)
end

#text_channelsArray<Channel>



3136
3137
3138
# File 'lib/discordrb/data.rb', line 3136

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

#unban(user, reason = nil) ⇒ Object

Unbans a previously banned user from this server.



3359
3360
3361
# File 'lib/discordrb/data.rb', line 3359

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

#verification_levelSymbol



3461
3462
3463
# File 'lib/discordrb/data.rb', line 3461

def verification_level
  VERIFICATION_LEVELS.key @verification_level
end

#verification_level=(level) ⇒ Object

Sets the verification level of the server



3467
3468
3469
3470
3471
# File 'lib/discordrb/data.rb', line 3467

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

  update_server_data(verification_level: level)
end

#voice_channelsArray<Channel>



3141
3142
3143
# File 'lib/discordrb/data.rb', line 3141

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

#webhooksArray<Webhook>

Requests a list of Webhooks on the server.



3532
3533
3534
3535
# File 'lib/discordrb/data.rb', line 3532

def webhooks
  webhooks = JSON.parse(API::Server.webhooks(@bot.token, @id))
  webhooks.map { |webhook| Webhook.new(webhook, @bot) }
end

#widget_banner_url(style) ⇒ String?



3172
3173
3174
3175
3176
3177
# File 'lib/discordrb/data.rb', line 3172

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

  API.widget_url(@id, style)
end

#widget_urlString?



3157
3158
3159
3160
3161
3162
# File 'lib/discordrb/data.rb', line 3157

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

  API.widget_url(@id)
end