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



1147
1148
1149
# File 'lib/discordrb/data.rb', line 1147

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.



1144
1145
1146
# File 'lib/discordrb/data.rb', line 1144

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.



1130
1131
1132
# File 'lib/discordrb/data.rb', line 1130

def channels
  @channels
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,



1137
1138
1139
# File 'lib/discordrb/data.rb', line 1137

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.



1141
1142
1143
# File 'lib/discordrb/data.rb', line 1141

def member_count
  @member_count
end

#ownerMember

Returns The server owner.

Returns:

  • (Member)

    The server owner.



1127
1128
1129
# File 'lib/discordrb/data.rb', line 1127

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



1124
1125
1126
# File 'lib/discordrb/data.rb', line 1124

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.



1133
1134
1135
# File 'lib/discordrb/data.rb', line 1133

def roles
  @roles
end

Instance Method Details

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



1287
1288
1289
# File 'lib/discordrb/data.rb', line 1287

def ban(user, message_days = 0)
  API.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.



1279
1280
1281
1282
# File 'lib/discordrb/data.rb', line 1279

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

#create_channel(name, type = 'text') ⇒ Channel

Creates a channel on this server with the given name.

Returns:

  • (Channel)

    the created channel.



1262
1263
1264
1265
# File 'lib/discordrb/data.rb', line 1262

def create_channel(name, type = 'text')
  response = API.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.



1271
1272
1273
1274
1275
1276
# File 'lib/discordrb/data.rb', line 1271

def create_role
  response = API.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)



1174
1175
1176
# File 'lib/discordrb/data.rb', line 1174

def default_channel
  @bot.channel(@id)
end

#deleteObject

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



1311
1312
1313
# File 'lib/discordrb/data.rb', line 1311

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

#icon=(icon) ⇒ Object

Sets the server's icon.

Parameters:

  • icon (String, #read)

    The new icon, in base64-encoded JPG format.



1340
1341
1342
1343
1344
1345
1346
1347
1348
# File 'lib/discordrb/data.rb', line 1340

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



1394
1395
1396
# File 'lib/discordrb/data.rb', line 1394

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

#kick(user) ⇒ Object

Kicks a user from this server.

Parameters:



1299
1300
1301
# File 'lib/discordrb/data.rb', line 1299

def kick(user)
  API.kick_user(@bot.token, @id, user.resolve_id)
end

#leaveObject

Leave the server



1316
1317
1318
# File 'lib/discordrb/data.rb', line 1316

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

#member(id) ⇒ Object

Gets a member on this server based on user ID

Parameters:

  • id (Integer)

    The user ID to look for



1188
1189
1190
1191
1192
1193
1194
# File 'lib/discordrb/data.rb', line 1188

def member(id)
  id = id.resolve_id
  return @members[id] if member_cached?(id)

  member = bot.member(@id, id)
  @members[id] = member
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.



1197
1198
1199
1200
1201
1202
1203
1204
# File 'lib/discordrb/data.rb', line 1197

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.



1306
1307
1308
# File 'lib/discordrb/data.rb', line 1306

def move(user, channel)
  API.move_user(@bot.token, @id, user.id, channel.id)
end

#name=(name) ⇒ Object

Sets the server's name.

Parameters:

  • name (String)

    The new server name.



1328
1329
1330
# File 'lib/discordrb/data.rb', line 1328

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

#role(id) ⇒ Object

Gets a role on this server based on its ID.

Parameters:

  • id (Integer)

    The role ID to look for.



1182
1183
1184
# File 'lib/discordrb/data.rb', line 1182

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

#unban(user) ⇒ Object

Unbans a previously banned user from this server.

Parameters:



1293
1294
1295
# File 'lib/discordrb/data.rb', line 1293

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