Class: MijDiscord::Data::Server
- Inherits:
-
Object
- Object
- MijDiscord::Data::Server
- Includes:
- IDObject
- Defined in:
- lib/mij-discord/data/server.rb
Constant Summary collapse
- VERIFICATION_LEVEL =
{ 0 => :none, 1 => :low, 2 => :medium, 3 => :high, 4 => :very_high, }.freeze
- CONTENT_FILTER_LEVEL =
{ 0 => :none, 1 => :no_roles, 2 => :all, }.freeze
- DEFAULT_NOTIFICATIONS =
{ 0 => :all, 1 => :mentions, }.freeze
- CHANNEL_TYPES =
{ :text => 0, :voice => 2, :category => 4, }.freeze
Instance Attribute Summary collapse
-
#afk_channel ⇒ Object
readonly
Returns the value of attribute afk_channel.
-
#afk_timeout ⇒ Object
readonly
Returns the value of attribute afk_timeout.
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#content_filter_level ⇒ Object
(also: #content_filter)
readonly
Returns the value of attribute content_filter_level.
-
#default_notifications ⇒ Object
readonly
Returns the value of attribute default_notifications.
-
#embed_enabled ⇒ Object
(also: #embed_enabled?, #has_embed?)
readonly
Returns the value of attribute embed_enabled.
-
#features ⇒ Object
readonly
Returns the value of attribute features.
-
#icon_id ⇒ Object
readonly
Returns the value of attribute icon_id.
-
#large ⇒ Object
(also: #large?)
readonly
Returns the value of attribute large.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#verification_level ⇒ Object
readonly
attr_reader :member_count.
Attributes included from IDObject
Instance Method Summary collapse
- #available_regions ⇒ Object
- #ban(user, message_days = 0, reason = nil) ⇒ Object
- #bans ⇒ Object
- #channel(id) ⇒ Object
- #channels ⇒ Object
- #create_channel(name, reason = nil, type: :text, bitrate: nil, user_limit: nil, permissions: [], nsfw: false) ⇒ Object
- #create_role(name, reason = nil, color: 0, hoist: false, mentionable: false, permissions: 104_324_161) ⇒ Object
- #default_channel ⇒ Object (also: #general_channel)
- #delete ⇒ Object
- #embed_url(style = :shield) ⇒ Object
- #emoji(id) ⇒ Object
- #emojis ⇒ Object
- #everyone_role ⇒ Object
- #icon_url(format = :webp) ⇒ Object
-
#initialize(data, bot, invalid = false) ⇒ Server
constructor
A new instance of Server.
- #invites ⇒ Object
- #kick(user, reason = nil) ⇒ Object
- #leave ⇒ Object
- #member(id) ⇒ Object
- #members ⇒ Object
- #online_members(idle: true, bots: false) ⇒ Object
- #prune(days, reason = nil) ⇒ Object
- #prune_count(days) ⇒ Object
- #region ⇒ Object
- #role(id) ⇒ Object
- #roles ⇒ Object
- #search_messages(limit: 25, offset: 0, sort_by: nil, sort_asc: false, nsfw: true, around: 2, content: nil, author: nil, channel: nil, mentions: nil, author_is: nil, author_not: nil, has: nil, has_not: nil, before: nil, after: nil) ⇒ Object
- #set_afk_channel(channel, reason = nil) ⇒ Object (also: #afk_channel=)
- #set_afk_timeout(timeout, reason = nil) ⇒ Object (also: #afk_timeout=)
- #set_icon(icon, reason = nil) ⇒ Object (also: #icon=)
- #set_name(name, reason = nil) ⇒ Object (also: #name=)
- #set_options(reason = nil, name: nil, region: nil, icon: nil, afk_channel: nil, afk_timeout: nil) ⇒ Object
- #set_owner(user, reason = nil) ⇒ Object (also: #owner=)
- #set_region(region, reason = nil) ⇒ Object (also: #region=)
- #text_channels ⇒ Object
- #unban(user, reason = nil) ⇒ Object
- #update_data(data) ⇒ Object
- #update_emojis(data) ⇒ Object
- #update_members_chunk(data) ⇒ Object
- #update_voice_state(data) ⇒ Object
- #voice_channels ⇒ Object
- #voice_state(id) ⇒ Object
- #voice_states ⇒ Object
Methods included from IDObject
#==, #creation_time, #hash, synthesize
Constructor Details
#initialize(data, bot, invalid = false) ⇒ Server
Returns a new instance of Server.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mij-discord/data/server.rb', line 64 def initialize(data, bot, invalid = false) @bot = bot @id = data['id'].to_i @large = data['large'] @members_init = data['member_count'] @members_chunked = 0 @cache = MijDiscord::Cache::ServerCache.new(self, @bot) data['channels']&.each {|ch| @cache.put_channel(ch) } data['roles']&.each {|ro| @cache.put_role(ro) } data['members']&.each {|mb| @cache.put_member(mb) } data['presences']&.each do |pr| next unless pr['user'] user_id = pr['user']['id'].to_i user = @cache.get_member(user_id, local: true) user.update_presence(pr) if user end @voice_states = {} data['voice_states']&.each {|vs| update_voice_state(vs) } update_emojis(data) update_data(data) @owner = member(data['owner_id']) end |
Instance Attribute Details
#afk_channel ⇒ Object (readonly)
Returns the value of attribute afk_channel.
60 61 62 |
# File 'lib/mij-discord/data/server.rb', line 60 def afk_channel @afk_channel end |
#afk_timeout ⇒ Object (readonly)
Returns the value of attribute afk_timeout.
58 59 60 |
# File 'lib/mij-discord/data/server.rb', line 58 def afk_timeout @afk_timeout end |
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
32 33 34 |
# File 'lib/mij-discord/data/server.rb', line 32 def bot @bot end |
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
62 63 64 |
# File 'lib/mij-discord/data/server.rb', line 62 def cache @cache end |
#content_filter_level ⇒ Object (readonly) Also known as: content_filter
Returns the value of attribute content_filter_level.
53 54 55 |
# File 'lib/mij-discord/data/server.rb', line 53 def content_filter_level @content_filter_level end |
#default_notifications ⇒ Object (readonly)
Returns the value of attribute default_notifications.
56 57 58 |
# File 'lib/mij-discord/data/server.rb', line 56 def default_notifications @default_notifications end |
#embed_enabled ⇒ Object (readonly) Also known as: embed_enabled?, has_embed?
Returns the value of attribute embed_enabled.
45 46 47 |
# File 'lib/mij-discord/data/server.rb', line 45 def @embed_enabled end |
#features ⇒ Object (readonly)
Returns the value of attribute features.
43 44 45 |
# File 'lib/mij-discord/data/server.rb', line 43 def features @features end |
#icon_id ⇒ Object (readonly)
Returns the value of attribute icon_id.
36 37 38 |
# File 'lib/mij-discord/data/server.rb', line 36 def icon_id @icon_id end |
#large ⇒ Object (readonly) Also known as: large?
Returns the value of attribute large.
40 41 42 |
# File 'lib/mij-discord/data/server.rb', line 40 def large @large end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
34 35 36 |
# File 'lib/mij-discord/data/server.rb', line 34 def name @name end |
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
38 39 40 |
# File 'lib/mij-discord/data/server.rb', line 38 def owner @owner end |
#verification_level ⇒ Object (readonly)
attr_reader :member_count
51 52 53 |
# File 'lib/mij-discord/data/server.rb', line 51 def verification_level @verification_level end |
Instance Method Details
#available_regions ⇒ Object
310 311 312 313 314 315 |
# File 'lib/mij-discord/data/server.rb', line 310 def available_regions return @voice_regions if @voice_regions response = MijDiscord::Core::API::Server.regions(@bot.auth, @id) @voice_regions = JSON.parse(response).map {|x| VoiceRegion.new(x) } end |
#ban(user, message_days = 0, reason = nil) ⇒ Object
266 267 268 269 |
# File 'lib/mij-discord/data/server.rb', line 266 def ban(user, = 0, reason = nil) MijDiscord::Core::API::Server.ban_user(@bot.auth, @id, user.to_id, , reason) nil end |
#bans ⇒ Object
261 262 263 264 |
# File 'lib/mij-discord/data/server.rb', line 261 def bans response = MijDiscord::Core::API::Server.bans(@bot.auth, @id) JSON.parse(response).map {|x| @bot.cache.put_user(x['user']) } end |
#channel(id) ⇒ Object
156 157 158 |
# File 'lib/mij-discord/data/server.rb', line 156 def channel(id) @cache.get_channel(id) end |
#channels ⇒ Object
152 153 154 |
# File 'lib/mij-discord/data/server.rb', line 152 def channels @cache.list_channels end |
#create_channel(name, reason = nil, type: :text, bitrate: nil, user_limit: nil, permissions: [], nsfw: false) ⇒ Object
246 247 248 249 250 251 252 253 |
# File 'lib/mij-discord/data/server.rb', line 246 def create_channel(name, reason = nil, type: :text, bitrate: nil, user_limit: nil, permissions: [], nsfw: false) raise ArgumentError, 'Invalid channel type specified' unless CHANNEL_TYPES.has_key?(type) = .map {|x| x.is_a(Overwrite) ? x.to_hash : x } response = MijDiscord::Core::API::Server.create_channel(@bot.auth, @id, name, CHANNEL_TYPES[type], bitrate, user_limit, , nsfw, reason) @cache.put_channel(JSON.parse(response)) end |
#create_role(name, reason = nil, color: 0, hoist: false, mentionable: false, permissions: 104_324_161) ⇒ Object
255 256 257 258 259 |
# File 'lib/mij-discord/data/server.rb', line 255 def create_role(name, reason = nil, color: 0, hoist: false, mentionable: false, permissions: 104_324_161) response = MijDiscord::Core::API::Server.create_role(@bot.auth, @id, name, color.to_i, hoist, mentionable, .to_i, reason) @cache.put_role(JSON.parse(response)) end |
#default_channel ⇒ Object Also known as: general_channel
201 202 203 204 205 206 207 208 209 |
# File 'lib/mij-discord/data/server.rb', line 201 def default_channel text_channels.sort_by {|ch| [ch.position, ch.id] }.find do |ch| if (overwrite = ch.[id]) overwrite.allow. || !overwrite.deny. else everyone_role.. end end end |
#delete ⇒ Object
286 287 288 289 |
# File 'lib/mij-discord/data/server.rb', line 286 def delete MijDiscord::Core::API::Server.delete(@bot.auth, @id) nil end |
#embed_url(style = :shield) ⇒ Object
363 364 365 366 |
# File 'lib/mij-discord/data/server.rb', line 363 def (style = :shield) return nil unless @embed_enabled MijDiscord::Core::API.(@id, style) end |
#emoji(id) ⇒ Object
185 186 187 |
# File 'lib/mij-discord/data/server.rb', line 185 def emoji(id) @emojis[id&.to_id] end |
#emojis ⇒ Object
181 182 183 |
# File 'lib/mij-discord/data/server.rb', line 181 def emojis @emojis.values end |
#everyone_role ⇒ Object
189 190 191 |
# File 'lib/mij-discord/data/server.rb', line 189 def everyone_role role(@id) end |
#icon_url(format = :webp) ⇒ Object
358 359 360 361 |
# File 'lib/mij-discord/data/server.rb', line 358 def icon_url(format = :webp) return nil unless @icon_id MijDiscord::Core::API.icon_url(@id, @icon_id, format) end |
#invites ⇒ Object
219 220 221 222 |
# File 'lib/mij-discord/data/server.rb', line 219 def invites response = MijDiscord::Core::API::Server.invites(@bot.auth, @id) JSON.parse(response).map {|x| Invite.new(x, @bot) } end |
#kick(user, reason = nil) ⇒ Object
276 277 278 279 |
# File 'lib/mij-discord/data/server.rb', line 276 def kick(user, reason = nil) MijDiscord::Core::API::Server.remove_member(@bot.auth, @id, user.to_id, reason) nil end |
#leave ⇒ Object
281 282 283 284 |
# File 'lib/mij-discord/data/server.rb', line 281 def leave MijDiscord::Core::API::User.leave_server(@bot.auth, @id) nil end |
#member(id) ⇒ Object
177 178 179 |
# File 'lib/mij-discord/data/server.rb', line 177 def member(id) @cache.get_member(id) end |
#members ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/mij-discord/data/server.rb', line 168 def members unless @members_chunked.nil? @bot.gateway.send_request_members(@id, '', 0) sleep(0.05) while @members_chunked end @cache.list_members end |
#online_members(idle: true, bots: false) ⇒ Object
213 214 215 216 217 |
# File 'lib/mij-discord/data/server.rb', line 213 def online_members(idle: true, bots: false) members.select! do |m| ((idle ? m.idle? : false) || m.online?) && (bots ? true : !m.bot_account?) end end |
#prune(days, reason = nil) ⇒ Object
231 232 233 234 235 236 |
# File 'lib/mij-discord/data/server.rb', line 231 def prune(days, reason = nil) raise ArgumentError, 'Days must be between 1 and 30' unless days.between?(1, 30) response = MijDiscord::Core::API::Server.begin_prune(@bot.auth, @id, days, reason) JSON.parse(response)['pruned'] end |
#prune_count(days) ⇒ Object
224 225 226 227 228 229 |
# File 'lib/mij-discord/data/server.rb', line 224 def prune_count(days) raise ArgumentError, 'Days must be between 1 and 30' unless days.between?(1, 30) response = MijDiscord::Core::API::Server.prune_count(@bot.auth, @id, days) JSON.parse(response)['pruned'] end |
#region ⇒ Object
317 318 319 |
# File 'lib/mij-discord/data/server.rb', line 317 def region available_regions.find {|x| x.id == @region_id } end |
#role(id) ⇒ Object
164 165 166 |
# File 'lib/mij-discord/data/server.rb', line 164 def role(id) @cache.get_role(id) end |
#roles ⇒ Object
160 161 162 |
# File 'lib/mij-discord/data/server.rb', line 160 def roles @cache.list_roles end |
#search_messages(limit: 25, offset: 0, sort_by: nil, sort_asc: false, nsfw: true, around: 2, content: nil, author: nil, channel: nil, mentions: nil, author_is: nil, author_not: nil, has: nil, has_not: nil, before: nil, after: nil) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/mij-discord/data/server.rb', line 368 def (limit: 25, offset: 0, sort_by: nil, sort_asc: false, nsfw: true, around: 2, content: nil, author: nil, channel: nil, mentions: nil, author_is: nil, author_not: nil, has: nil, has_not: nil, before: nil, after: nil) = ? [*].map(&:to_id) : nil channel = channel ? [*channel].map(&:to_id) : nil mentions = mentions ? [*mentions].map(&:to_id) : nil has_not = has_not ? [*has_not].map {|x| "-#{x}" } : [] = ? [*].map {|x| "-#{x}" } : [] has = has_not | (has ? [*has].map(&:to_s) : []) = | ( ? [*].map(&:to_s) : []) before = before ? IDObject.synthesize(before) : nil after = after ? IDObject.synthesize(after) : nil = { limit: limit, offset: offset, sort_by: sort_by, sort_order: sort_asc ? 'asc' : 'desc', context_size: around, include_nsfw: nsfw, author_id: , author_type: , channel_id: channel, mentions: mentions, max_id: before, min_id: after, content: content, has: has, }.delete_if {|_,v| v.nil? } response = MijDiscord::Core::API::Server.(@bot.auth, @id, ) SearchResults.new(JSON.parse(response), @bot) end |
#set_afk_channel(channel, reason = nil) ⇒ Object Also known as: afk_channel=
344 345 346 347 |
# File 'lib/mij-discord/data/server.rb', line 344 def set_afk_channel(channel, reason = nil) (reason, afk_channel: channel.to_id) nil end |
#set_afk_timeout(timeout, reason = nil) ⇒ Object Also known as: afk_timeout=
351 352 353 354 |
# File 'lib/mij-discord/data/server.rb', line 351 def set_afk_timeout(timeout, reason = nil) (reason, afk_timeout: timeout) nil end |
#set_icon(icon, reason = nil) ⇒ Object Also known as: icon=
331 332 333 334 335 336 337 338 339 340 |
# File 'lib/mij-discord/data/server.rb', line 331 def set_icon(icon, reason = nil) if icon.respond_to?(:read) buffer = Base64.strict_encode64(icon.read) icon = "data:image/jpg;base64,#{buffer}" (reason, icon: icon) else (reason, icon: icon.to_s) end nil end |
#set_name(name, reason = nil) ⇒ Object Also known as: name=
303 304 305 306 |
# File 'lib/mij-discord/data/server.rb', line 303 def set_name(name, reason = nil) (reason, name: name) nil end |
#set_options(reason = nil, name: nil, region: nil, icon: nil, afk_channel: nil, afk_timeout: nil) ⇒ Object
297 298 299 300 301 |
# File 'lib/mij-discord/data/server.rb', line 297 def (reason = nil, name: nil, region: nil, icon: nil, afk_channel: nil, afk_timeout: nil) response = MijDiscord::Core::API::Server.update(@bot.auth, @id, name, region, icon, afk_channel&.to_id, afk_timeout, reason) @bot.cache.put_server(JSON.parse(response), update: true) end |
#set_owner(user, reason = nil) ⇒ Object Also known as: owner=
291 292 293 |
# File 'lib/mij-discord/data/server.rb', line 291 def set_owner(user, reason = nil) MijDiscord::Core::API::Server.transfer_ownership(@bot.auth, @id, user.to_id, reason) end |
#set_region(region, reason = nil) ⇒ Object Also known as: region=
321 322 323 324 325 326 327 |
# File 'lib/mij-discord/data/server.rb', line 321 def set_region(region, reason = nil) region = region.to_s raise ArgumentError, 'Invalid region' unless available_regions.find {|x| x.id == region } (reason, region: region) nil end |
#text_channels ⇒ Object
238 239 240 |
# File 'lib/mij-discord/data/server.rb', line 238 def text_channels channels.select!(&:text?) end |
#unban(user, reason = nil) ⇒ Object
271 272 273 274 |
# File 'lib/mij-discord/data/server.rb', line 271 def unban(user, reason = nil) MijDiscord::Core::API::Server.unban_user(@bot.auth, @id, user.to_id, reason) nil end |
#update_data(data) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mij-discord/data/server.rb', line 97 def update_data(data) @name = data.fetch('name', @name) @region_id = data.fetch('region', @region_id) @icon_id = data.fetch('icon', @icon_id) @afk_timeout = data.fetch('afk_timeout', @afk_timeout) @embed_enabled = data.fetch('embed_enabled', @embed_enabled) @verification_level = VERIFICATION_LEVEL.fetch(data['verification_level'], @verification_level) @content_filter_level = CONTENT_FILTER_LEVEL.fetch(data['explicit_content_filter'], @content_filter_level) @default_notifications = DEFAULT_NOTIFICATIONS.fetch(data['default_message_notifications'], @default_notifications) begin if data.has_key?('afk_channel_id') id = data['afk_channel_id'].to_i @afk_channel = @bot.channel(id, self) end rescue MijDiscord::Errors::NoPermission @afk_channel = nil end end |
#update_emojis(data) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/mij-discord/data/server.rb', line 117 def update_emojis(data) @emojis = {} data['emojis'].each do |em| emoji = MijDiscord::Data::Emoji.new(em, @bot, self) @emojis[emoji.id] = emoji end end |
#update_members_chunk(data) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/mij-discord/data/server.rb', line 143 def update_members_chunk(data) return if @members_chunked.nil? @members_chunked += data.length data.each {|mb| @cache.put_member(mb) } @members_chunked = nil if @members_chunked == @members_init end |
#update_voice_state(data) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mij-discord/data/server.rb', line 125 def update_voice_state(data) user = @cache.get_member(data['user_id']) if (chan_id = data['channel_id']&.to_i) state = (@voice_states[user.id] ||= VoiceState.new(user)) channel = @cache.get_channel(chan_id) state.update_data(channel, data) if channel state else state = @voice_states.delete(user.id) state ||= VoiceState.new(user) state.update_data(nil, data) state end end |
#voice_channels ⇒ Object
242 243 244 |
# File 'lib/mij-discord/data/server.rb', line 242 def voice_channels channels.select!(&:voice?) end |
#voice_state(id) ⇒ Object
197 198 199 |
# File 'lib/mij-discord/data/server.rb', line 197 def voice_state(id) @voice_states[id&.to_id] end |
#voice_states ⇒ Object
193 194 195 |
# File 'lib/mij-discord/data/server.rb', line 193 def voice_states @voice_states.values end |