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



1116
1117
1118
# File 'lib/discordrb/data.rb', line 1116

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.



1113
1114
1115
# File 'lib/discordrb/data.rb', line 1113

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.



1099
1100
1101
# File 'lib/discordrb/data.rb', line 1099

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,



1106
1107
1108
# File 'lib/discordrb/data.rb', line 1106

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.



1110
1111
1112
# File 'lib/discordrb/data.rb', line 1110

def member_count
  @member_count
end

#ownerMember

Returns The server owner.

Returns:

  • (Member)

    The server owner.



1096
1097
1098
# File 'lib/discordrb/data.rb', line 1096

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



1093
1094
1095
# File 'lib/discordrb/data.rb', line 1093

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.



1102
1103
1104
# File 'lib/discordrb/data.rb', line 1102

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.



1256
1257
1258
# File 'lib/discordrb/data.rb', line 1256

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.



1248
1249
1250
1251
# File 'lib/discordrb/data.rb', line 1248

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.



1231
1232
1233
1234
# File 'lib/discordrb/data.rb', line 1231

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.



1240
1241
1242
1243
1244
1245
# File 'lib/discordrb/data.rb', line 1240

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)



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

def default_channel
  @bot.channel(@id)
end

#deleteObject

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



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

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.



1309
1310
1311
1312
1313
1314
1315
1316
1317
# File 'lib/discordrb/data.rb', line 1309

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



1363
1364
1365
# File 'lib/discordrb/data.rb', line 1363

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:



1268
1269
1270
# File 'lib/discordrb/data.rb', line 1268

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

#leaveObject

Leave the server



1285
1286
1287
# File 'lib/discordrb/data.rb', line 1285

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



1157
1158
1159
1160
1161
1162
1163
# File 'lib/discordrb/data.rb', line 1157

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.



1166
1167
1168
1169
1170
1171
1172
1173
# File 'lib/discordrb/data.rb', line 1166

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.



1275
1276
1277
# File 'lib/discordrb/data.rb', line 1275

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.



1297
1298
1299
# File 'lib/discordrb/data.rb', line 1297

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.



1151
1152
1153
# File 'lib/discordrb/data.rb', line 1151

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

#unban(user) ⇒ Object

Unbans a previously banned user from this server.

Parameters:



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

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