_id
stringlengths
2
6
title
stringlengths
9
130
partition
stringclasses
3 values
text
stringlengths
66
10.5k
language
stringclasses
1 value
meta_information
dict
q12300
Mongoid.Config.connect_to
train
def connect_to(name, options = { read: { mode: :primary }}) self.clients = { default: { database: name, hosts: [ "localhost:27017" ], options: options } } end
ruby
{ "resource": "" }
q12301
Mongoid.Config.load!
train
def load!(path, environment = nil) settings = Environment.load_yaml(path, environment) if settings.present? Clients.disconnect Clients.clear load_configuration(settings) end settings end
ruby
{ "resource": "" }
q12302
Mongoid.Config.register_model
train
def register_model(klass) LOCK.synchronize do models.push(klass) unless models.include?(klass) end end
ruby
{ "resource": "" }
q12303
Mongoid.Config.load_configuration
train
def load_configuration(settings) configuration = settings.with_indifferent_access self.options = configuration[:options] self.clients = configuration[:clients] set_log_levels end
ruby
{ "resource": "" }
q12304
Mongoid.Config.truncate!
train
def truncate! Clients.default.database.collections.each do |collection| collection.find.delete_many end and true end
ruby
{ "resource": "" }
q12305
Mongoid.Config.options=
train
def options=(options) if options options.each_pair do |option, value| Validators::Option.validate(option) send("#{option}=", value) end end end
ruby
{ "resource": "" }
q12306
Clearance.Authorization.deny_access
train
def deny_access(flash_message = nil) respond_to do |format| format.any(:js, :json, :xml) { head :unauthorized } format.any { redirect_request(flash_message) } end end
ruby
{ "resource": "" }
q12307
Clearance.RackSession.call
train
def call(env) session = Clearance::Session.new(env) env[:clearance] = session response = @app.call(env) session.add_cookie_to_headers response[1] response end
ruby
{ "resource": "" }
q12308
Discordrb.User.send_file
train
def send_file(file, caption = nil, filename: nil, spoiler: nil) pm.send_file(file, caption: caption, filename: filename, spoiler: spoiler) end
ruby
{ "resource": "" }
q12309
Discordrb.User.update_presence
train
def update_presence(data) @status = data['status'].to_sym if data['game'] game = data['game'] @game = game['name'] @stream_url = game['url'] @stream_type = game['type'] else @game = @stream_url = @stream_type = nil end end
ruby
{ "resource": "" }
q12310
Discordrb.PermissionCalculator.permission?
train
def permission?(action, channel = nil) # If the member is the server owner, it irrevocably has all permissions. return true if owner? # First, check whether the user has Manage Roles defined. # (Coincidentally, Manage Permissions is the same permission as Manage Roles, and a # Manage Permissions deny overwrite will override Manage Roles, so we can just check for # Manage Roles once and call it a day.) return true if defined_permission?(:administrator, channel) # Otherwise, defer to defined_permission defined_permission?(action, channel) end
ruby
{ "resource": "" }
q12311
Discordrb.Channel.category=
train
def category=(channel) channel = @bot.channel(channel) raise ArgumentError, 'Cannot set parent category to a channel that isn\'t a category' unless channel.category? update_channel_data(parent_id: channel.id) end
ruby
{ "resource": "" }
q12312
Discordrb.Channel.sort_after
train
def sort_after(other = nil, lock_permissions = false) raise TypeError, 'other must be one of Channel, NilClass, or #resolve_id' unless other.is_a?(Channel) || other.nil? || other.respond_to?(:resolve_id) other = @bot.channel(other.resolve_id) if other # Container for the API request payload move_argument = [] if other raise ArgumentError, 'Can only sort a channel after a channel of the same type!' unless other.category? || (@type == other.type) raise ArgumentError, 'Can only sort a channel after a channel in the same server!' unless other.server == server # Store `others` parent (or if `other` is a category itself) parent = if category? && other.category? # If we're sorting two categories, there is no new parent nil elsif other.category? # `other` is the category this channel will be moved into other else # `other`'s parent is the category this channel will be # moved into (if it exists) other.parent end end # Collect and sort the IDs within the context (category or not) that we # need to form our payload with ids = if parent parent.children else @server.channels.reject(&:parent_id).select { |c| c.type == @type } end.sort_by(&:position).map(&:id) # Move our channel ID after the target ID by deleting it, # getting the index of `other`, and inserting it after. ids.delete(@id) if ids.include?(@id) index = other ? (ids.index { |c| c == other.id } || -1) + 1 : 0 ids.insert(index, @id) # Generate `move_argument`, making the positions in order from how # we have sorted them in the above logic ids.each_with_index do |id, pos| # These keys are present in each element hash = { id: id, position: pos } # Conditionally add `lock_permissions` and `parent_id` if we're # iterating past ourself if id == @id hash[:lock_permissions] = true if lock_permissions hash[:parent_id] = parent.nil? ? nil : parent.id end # Add it to the stack move_argument << hash end API::Server.update_channel_positions(@bot.token, @server.id, move_argument) end
ruby
{ "resource": "" }
q12313
Discordrb.Channel.nsfw=
train
def nsfw=(nsfw) raise ArgumentError, 'nsfw value must be true or false' unless nsfw.is_a?(TrueClass) || nsfw.is_a?(FalseClass) update_channel_data(nsfw: nsfw) end
ruby
{ "resource": "" }
q12314
Discordrb.Channel.send_temporary_message
train
def send_temporary_message(content, timeout, tts = false, embed = nil) @bot.send_temporary_message(@id, content, timeout, tts, embed) end
ruby
{ "resource": "" }
q12315
Discordrb.Channel.send_embed
train
def send_embed(message = '', embed = nil) embed ||= Discordrb::Webhooks::Embed.new yield(embed) if block_given? send_message(message, false, embed) end
ruby
{ "resource": "" }
q12316
Discordrb.Channel.send_file
train
def send_file(file, caption: nil, tts: false, filename: nil, spoiler: nil) @bot.send_file(@id, file, caption: caption, tts: tts, filename: filename, spoiler: spoiler) end
ruby
{ "resource": "" }
q12317
Discordrb.Channel.delete_overwrite
train
def delete_overwrite(target, reason = nil) raise 'Tried deleting a overwrite for an invalid target' unless target.is_a?(Member) || target.is_a?(User) || target.is_a?(Role) || target.is_a?(Profile) || target.is_a?(Recipient) || target.respond_to?(:resolve_id) API::Channel.delete_permission(@bot.token, @id, target.resolve_id, reason) end
ruby
{ "resource": "" }
q12318
Discordrb.Channel.update_from
train
def update_from(other) @topic = other.topic @name = other.name @position = other.position @topic = other.topic @recipients = other.recipients @bitrate = other.bitrate @user_limit = other.user_limit @permission_overwrites = other.permission_overwrites @nsfw = other.nsfw @parent_id = other.parent_id @rate_limit_per_user = other.rate_limit_per_user end
ruby
{ "resource": "" }
q12319
Discordrb.Channel.users
train
def users if text? @server.online_members(include_idle: true).select { |u| u.can_read_messages? self } elsif voice? @server.voice_states.map { |id, voice_state| @server.member(id) if !voice_state.voice_channel.nil? && voice_state.voice_channel.id == @id }.compact end end
ruby
{ "resource": "" }
q12320
Discordrb.Channel.history
train
def history(amount, before_id = nil, after_id = nil, around_id = nil) logs = API::Channel.messages(@bot.token, @id, amount, before_id, after_id, around_id) JSON.parse(logs).map { |message| Message.new(message, @bot) } end
ruby
{ "resource": "" }
q12321
Discordrb.Channel.history_ids
train
def history_ids(amount, before_id = nil, after_id = nil, around_id = nil) logs = API::Channel.messages(@bot.token, @id, amount, before_id, after_id, around_id) JSON.parse(logs).map { |message| message['id'].to_i } end
ruby
{ "resource": "" }
q12322
Discordrb.Channel.load_message
train
def load_message(message_id) response = API::Channel.message(@bot.token, @id, message_id) Message.new(JSON.parse(response), @bot) rescue RestClient::ResourceNotFound nil end
ruby
{ "resource": "" }
q12323
Discordrb.Channel.pins
train
def pins msgs = API::Channel.pinned_messages(@bot.token, @id) JSON.parse(msgs).map { |msg| Message.new(msg, @bot) } end
ruby
{ "resource": "" }
q12324
Discordrb.Channel.prune
train
def prune(amount, strict = false, &block) raise ArgumentError, 'Can only delete between 1 and 100 messages!' unless amount.between?(1, 100) messages = if block_given? history(amount).select(&block).map(&:id) else history_ids(amount) end case messages.size when 0 0 when 1 API::Channel.delete_message(@bot.token, @id, messages.first) 1 else bulk_delete(messages, strict) end end
ruby
{ "resource": "" }
q12325
Discordrb.Channel.delete_messages
train
def delete_messages(messages, strict = false) raise ArgumentError, 'Can only delete between 2 and 100 messages!' unless messages.count.between?(2, 100) messages.map!(&:resolve_id) bulk_delete(messages, strict) end
ruby
{ "resource": "" }
q12326
Discordrb.Channel.make_invite
train
def make_invite(max_age = 0, max_uses = 0, temporary = false, unique = false, reason = nil) response = API::Channel.create_invite(@bot.token, @id, max_age, max_uses, temporary, unique, reason) Invite.new(JSON.parse(response), @bot) end
ruby
{ "resource": "" }
q12327
Discordrb.Channel.create_group
train
def create_group(user_ids) raise 'Attempted to create group channel on a non-pm channel!' unless pm? response = API::Channel.create_group(@bot.token, @id, user_ids.shift) channel = Channel.new(JSON.parse(response), @bot) channel.add_group_users(user_ids) end
ruby
{ "resource": "" }
q12328
Discordrb.Channel.add_group_users
train
def add_group_users(user_ids) raise 'Attempted to add a user to a non-group channel!' unless group? user_ids = [user_ids] unless user_ids.is_a? Array user_ids.each do |user_id| API::Channel.add_group_user(@bot.token, @id, user_id.resolve_id) end self end
ruby
{ "resource": "" }
q12329
Discordrb.Channel.remove_group_users
train
def remove_group_users(user_ids) raise 'Attempted to remove a user from a non-group channel!' unless group? user_ids = [user_ids] unless user_ids.is_a? Array user_ids.each do |user_id| API::Channel.remove_group_user(@bot.token, @id, user_id.resolve_id) end self end
ruby
{ "resource": "" }
q12330
Discordrb.Channel.webhooks
train
def webhooks raise 'Tried to request webhooks from a non-server channel' unless server webhooks = JSON.parse(API::Channel.webhooks(@bot.token, @id)) webhooks.map { |webhook_data| Webhook.new(webhook_data, @bot) } end
ruby
{ "resource": "" }
q12331
Discordrb.Channel.invites
train
def invites raise 'Tried to request invites from a non-server channel' unless server invites = JSON.parse(API::Channel.invites(@bot.token, @id)) invites.map { |invite_data| Invite.new(invite_data, @bot) } end
ruby
{ "resource": "" }
q12332
Discordrb.Channel.add_recipient
train
def add_recipient(recipient) raise 'Tried to add recipient to a non-group channel' unless group? raise ArgumentError, 'Tried to add a non-recipient to a group' unless recipient.is_a?(Recipient) @recipients << recipient end
ruby
{ "resource": "" }
q12333
Discordrb.Channel.remove_recipient
train
def remove_recipient(recipient) raise 'Tried to remove recipient from a non-group channel' unless group? raise ArgumentError, 'Tried to remove a non-recipient from a group' unless recipient.is_a?(Recipient) @recipients.delete(recipient) end
ruby
{ "resource": "" }
q12334
Discordrb.Channel.bulk_delete
train
def bulk_delete(ids, strict = false) min_snowflake = IDObject.synthesise(Time.now - TWO_WEEKS) ids.reject! do |e| next unless e < min_snowflake message = "Attempted to bulk_delete message #{e} which is too old (min = #{min_snowflake})" raise ArgumentError, message if strict Discordrb::LOGGER.warn(message) true end API::Channel.bulk_delete_messages(@bot.token, @id, ids) ids.size end
ruby
{ "resource": "" }
q12335
Discordrb.EventContainer.include_events
train
def include_events(container) handlers = container.instance_variable_get '@event_handlers' return unless handlers @event_handlers ||= {} @event_handlers.merge!(handlers) { |_, old, new| old + new } end
ruby
{ "resource": "" }
q12336
Discordrb.Webhook.update
train
def update(data) # Only pass a value for avatar if the key is defined as sending nil will delete the data[:avatar] = avatarise(data[:avatar]) if data.key?(:avatar) data[:channel_id] = data[:channel].resolve_id data.delete(:channel) update_webhook(data) end
ruby
{ "resource": "" }
q12337
Discordrb.Webhook.delete
train
def delete(reason = nil) if token? API::Webhook.token_delete_webhook(@token, @id, reason) else API::Webhook.delete_webhook(@bot.token, @id, reason) end end
ruby
{ "resource": "" }
q12338
Discordrb::Webhooks.Embed.colour=
train
def colour=(value) if value.nil? @colour = nil elsif value.is_a? Integer raise ArgumentError, 'Embed colour must be 24-bit!' if value >= 16_777_216 @colour = value elsif value.is_a? String self.colour = value.delete('#').to_i(16) elsif value.is_a? Array raise ArgumentError, 'Colour tuple must have three values!' if value.length != 3 self.colour = value[0] << 16 | value[1] << 8 | value[2] else self.colour = value.to_i end end
ruby
{ "resource": "" }
q12339
Discordrb::Webhooks.Embed.add_field
train
def add_field(name: nil, value: nil, inline: nil) self << EmbedField.new(name: name, value: value, inline: inline) end
ruby
{ "resource": "" }
q12340
Discordrb::Light.LightBot.profile
train
def profile response = Discordrb::API::User.profile(@token) LightProfile.new(JSON.parse(response), self) end
ruby
{ "resource": "" }
q12341
Discordrb::Light.LightBot.connections
train
def connections response = Discordrb::API::User.connections(@token) JSON.parse(response).map { |e| Connection.new(e, self) } end
ruby
{ "resource": "" }
q12342
Discordrb.Bot.find_emoji
train
def find_emoji(name) LOGGER.out("Resolving emoji #{name}") emoji.find { |element| element.name == name } end
ruby
{ "resource": "" }
q12343
Discordrb.Bot.bot_application
train
def bot_application return unless @type == :bot response = API.oauth_application(token) Application.new(JSON.parse(response), self) end
ruby
{ "resource": "" }
q12344
Discordrb.Bot.accept_invite
train
def accept_invite(invite) resolved = invite(invite).code API::Invite.accept(token, resolved) end
ruby
{ "resource": "" }
q12345
Discordrb.Bot.send_message
train
def send_message(channel, content, tts = false, embed = nil) channel = channel.resolve_id debug("Sending message to #{channel} with content '#{content}'") response = API::Channel.create_message(token, channel, content, tts, embed ? embed.to_hash : nil) Message.new(JSON.parse(response), self) end
ruby
{ "resource": "" }
q12346
Discordrb.Bot.send_temporary_message
train
def send_temporary_message(channel, content, timeout, tts = false, embed = nil) Thread.new do Thread.current[:discordrb_name] = "#{@current_thread}-temp-msg" message = send_message(channel, content, tts, embed) sleep(timeout) message.delete end nil end
ruby
{ "resource": "" }
q12347
Discordrb.Bot.send_file
train
def send_file(channel, file, caption: nil, tts: false, filename: nil, spoiler: nil) if file.respond_to?(:read) if spoiler filename ||= File.basename(file.path) filename = 'SPOILER_' + filename unless filename.start_with? 'SPOILER_' end # https://github.com/rest-client/rest-client/blob/v2.0.2/lib/restclient/payload.rb#L160 file.define_singleton_method(:original_filename) { filename } if filename end channel = channel.resolve_id response = API::Channel.upload_file(token, channel, file, caption: caption, tts: tts) Message.new(JSON.parse(response), self) end
ruby
{ "resource": "" }
q12348
Discordrb.Bot.create_server
train
def create_server(name, region = :'eu-central') response = API::Server.create(token, name, region) id = JSON.parse(response)['id'].to_i sleep 0.1 until @servers[id] server = @servers[id] debug "Successfully created server #{server.id} with name #{server.name}" server end
ruby
{ "resource": "" }
q12349
Discordrb.Bot.update_oauth_application
train
def update_oauth_application(name, redirect_uris, description = '', icon = nil) API.update_oauth_application(@token, name, redirect_uris, description, icon) end
ruby
{ "resource": "" }
q12350
Discordrb.Bot.parse_mentions
train
def parse_mentions(mentions, server = nil) array_to_return = [] # While possible mentions may be in message while mentions.include?('<') && mentions.include?('>') # Removing all content before the next possible mention mentions = mentions.split('<', 2)[1] # Locate the first valid mention enclosed in `<...>`, otherwise advance to the next open `<` next unless mentions.split('>', 2).first.length < mentions.split('<', 2).first.length # Store the possible mention value to be validated with RegEx mention = mentions.split('>', 2).first if /@!?(?<id>\d+)/ =~ mention array_to_return << user(id) unless user(id).nil? elsif /#(?<id>\d+)/ =~ mention array_to_return << channel(id, server) unless channel(id, server).nil? elsif /@&(?<id>\d+)/ =~ mention if server array_to_return << server.role(id) unless server.role(id).nil? else @servers.values.each do |element| array_to_return << element.role(id) unless element.role(id).nil? end end elsif /(?<animated>^[a]|^${0}):(?<name>\w+):(?<id>\d+)/ =~ mention array_to_return << (emoji(id) || Emoji.new({ 'animated' => !animated.nil?, 'name' => name, 'id' => id }, self, nil)) end end array_to_return end
ruby
{ "resource": "" }
q12351
Discordrb.Bot.update_status
train
def update_status(status, activity, url, since = 0, afk = false, activity_type = 0) gateway_check @activity = activity @status = status @streamurl = url type = url ? 1 : activity_type activity_obj = activity || url ? { 'name' => activity, 'url' => url, 'type' => type } : nil @gateway.send_status_update(status, since, activity_obj, afk) # Update the status in the cache profile.update_presence('status' => status.to_s, 'game' => activity_obj) end
ruby
{ "resource": "" }
q12352
Discordrb.Bot.add_await!
train
def add_await!(type, attributes = {}) raise "You can't await an AwaitEvent!" if type == Discordrb::Events::AwaitEvent timeout = attributes[:timeout] raise ArgumentError, 'Timeout must be a number > 0' if timeout&.is_a?(Numeric) && !timeout&.positive? mutex = Mutex.new cv = ConditionVariable.new response = nil block = lambda do |event| mutex.synchronize do response = event cv.signal end end handler = register_event(type, attributes, block) if timeout Thread.new do sleep timeout mutex.synchronize { cv.signal } end end mutex.synchronize { cv.wait(mutex) } remove_handler(handler) raise 'ConditionVariable was signaled without returning an event!' if response.nil? && timeout.nil? response end
ruby
{ "resource": "" }
q12353
Discordrb.Bot.prune_empty_groups
train
def prune_empty_groups @channels.each_value do |channel| channel.leave_group if channel.group? && channel.recipients.empty? end end
ruby
{ "resource": "" }
q12354
Discordrb.Bot.update_voice_state
train
def update_voice_state(data) @session_id = data['session_id'] server_id = data['guild_id'].to_i server = server(server_id) return unless server user_id = data['user_id'].to_i old_voice_state = server.voice_states[user_id] old_channel_id = old_voice_state.voice_channel.id if old_voice_state server.update_voice_state(data) old_channel_id end
ruby
{ "resource": "" }
q12355
Discordrb.Bot.update_voice_server
train
def update_voice_server(data) server_id = data['guild_id'].to_i channel = @should_connect_to_voice[server_id] debug("Voice server update received! chan: #{channel.inspect}") return unless channel @should_connect_to_voice.delete(server_id) debug('Updating voice server!') token = data['token'] endpoint = data['endpoint'] unless endpoint debug('VOICE_SERVER_UPDATE sent with nil endpoint! Ignoring') return end debug('Got data, now creating the bot.') @voices[server_id] = Discordrb::Voice::VoiceBot.new(channel, self, token, @session_id, endpoint, @should_encrypt_voice) end
ruby
{ "resource": "" }
q12356
Discordrb.Bot.create_channel
train
def create_channel(data) channel = Channel.new(data, self) server = channel.server # Handle normal and private channels separately if server server.add_channel(channel) @channels[channel.id] = channel elsif channel.pm? @pm_channels[channel.recipient.id] = channel elsif channel.group? @channels[channel.id] = channel end end
ruby
{ "resource": "" }
q12357
Discordrb.Bot.update_channel
train
def update_channel(data) channel = Channel.new(data, self) old_channel = @channels[channel.id] return unless old_channel old_channel.update_from(channel) end
ruby
{ "resource": "" }
q12358
Discordrb.Bot.delete_channel
train
def delete_channel(data) channel = Channel.new(data, self) server = channel.server # Handle normal and private channels separately if server @channels.delete(channel.id) server.delete_channel(channel.id) elsif channel.pm? @pm_channels.delete(channel.recipient.id) elsif channel.group? @channels.delete(channel.id) end end
ruby
{ "resource": "" }
q12359
Discordrb.Bot.add_recipient
train
def add_recipient(data) channel_id = data['channel_id'].to_i channel = self.channel(channel_id) recipient_user = ensure_user(data['user']) recipient = Recipient.new(recipient_user, channel, self) channel.add_recipient(recipient) end
ruby
{ "resource": "" }
q12360
Discordrb.Bot.add_guild_member
train
def add_guild_member(data) server_id = data['guild_id'].to_i server = self.server(server_id) member = Member.new(data, server, self) server.add_member(member) end
ruby
{ "resource": "" }
q12361
Discordrb.Bot.update_guild_member
train
def update_guild_member(data) server_id = data['guild_id'].to_i server = self.server(server_id) member = server.member(data['user']['id'].to_i) member.update_roles(data['roles']) member.update_nick(data['nick']) end
ruby
{ "resource": "" }
q12362
Discordrb.Bot.delete_guild_member
train
def delete_guild_member(data) server_id = data['guild_id'].to_i server = self.server(server_id) user_id = data['user']['id'].to_i server.delete_member(user_id) rescue Discordrb::Errors::NoPermission Discordrb::LOGGER.warn("delete_guild_member attempted to access a server for which the bot doesn't have permission! Not sure what happened here, ignoring") end
ruby
{ "resource": "" }
q12363
Discordrb.Bot.update_guild_role
train
def update_guild_role(data) role_data = data['role'] server_id = data['guild_id'].to_i server = @servers[server_id] new_role = Role.new(role_data, self, server) role_id = role_data['id'].to_i old_role = server.roles.find { |r| r.id == role_id } old_role.update_from(new_role) end
ruby
{ "resource": "" }
q12364
Discordrb.Bot.create_guild_role
train
def create_guild_role(data) role_data = data['role'] server_id = data['guild_id'].to_i server = @servers[server_id] new_role = Role.new(role_data, self, server) existing_role = server.role(new_role.id) if existing_role existing_role.update_from(new_role) else server.add_role(new_role) end end
ruby
{ "resource": "" }
q12365
Discordrb.Bot.delete_guild_role
train
def delete_guild_role(data) role_id = data['role_id'].to_i server_id = data['guild_id'].to_i server = @servers[server_id] server.delete_role(role_id) end
ruby
{ "resource": "" }
q12366
Discordrb.Bot.update_guild_emoji
train
def update_guild_emoji(data) server_id = data['guild_id'].to_i server = @servers[server_id] server.update_emoji_data(data) end
ruby
{ "resource": "" }
q12367
Discordrb.Cache.voice_regions
train
def voice_regions return @voice_regions unless @voice_regions.empty? regions = JSON.parse API.voice_regions(token) regions.each do |data| @voice_regions[data['id']] = VoiceRegion.new(data) end @voice_regions end
ruby
{ "resource": "" }
q12368
Discordrb.Cache.channel
train
def channel(id, server = nil) id = id.resolve_id raise Discordrb::Errors::NoPermission if @restricted_channels.include? id debug("Obtaining data for channel with id #{id}") return @channels[id] if @channels[id] begin begin response = API::Channel.resolve(token, id) rescue RestClient::ResourceNotFound return nil end channel = Channel.new(JSON.parse(response), self, server) @channels[id] = channel rescue Discordrb::Errors::NoPermission debug "Tried to get access to restricted channel #{id}, blacklisting it" @restricted_channels << id raise end end
ruby
{ "resource": "" }
q12369
Discordrb.Cache.user
train
def user(id) id = id.resolve_id return @users[id] if @users[id] LOGGER.out("Resolving user #{id}") begin response = API::User.resolve(token, id) rescue RestClient::ResourceNotFound return nil end user = User.new(JSON.parse(response), self) @users[id] = user end
ruby
{ "resource": "" }
q12370
Discordrb.Cache.server
train
def server(id) id = id.resolve_id return @servers[id] if @servers[id] LOGGER.out("Resolving server #{id}") begin response = API::Server.resolve(token, id) rescue Discordrb::Errors::NoPermission return nil end server = Server.new(JSON.parse(response), self) @servers[id] = server end
ruby
{ "resource": "" }
q12371
Discordrb.Cache.member
train
def member(server_or_id, user_id) server_id = server_or_id.resolve_id user_id = user_id.resolve_id server = server_or_id.is_a?(Server) ? server_or_id : self.server(server_id) return server.member(user_id) if server.member_cached?(user_id) LOGGER.out("Resolving member #{server_id} on server #{user_id}") begin response = API::Server.resolve_member(token, server_id, user_id) rescue RestClient::ResourceNotFound return nil end member = Member.new(JSON.parse(response), server, self) server.cache_member(member) end
ruby
{ "resource": "" }
q12372
Discordrb.Cache.ensure_user
train
def ensure_user(data) if @users.include?(data['id'].to_i) @users[data['id'].to_i] else @users[data['id'].to_i] = User.new(data, self) end end
ruby
{ "resource": "" }
q12373
Discordrb.Cache.ensure_server
train
def ensure_server(data) if @servers.include?(data['id'].to_i) @servers[data['id'].to_i] else @servers[data['id'].to_i] = Server.new(data, self) end end
ruby
{ "resource": "" }
q12374
Discordrb.Cache.ensure_channel
train
def ensure_channel(data, server = nil) if @channels.include?(data['id'].to_i) @channels[data['id'].to_i] else @channels[data['id'].to_i] = Channel.new(data, self, server) end end
ruby
{ "resource": "" }
q12375
Discordrb.Cache.invite
train
def invite(invite) code = resolve_invite_code(invite) Invite.new(JSON.parse(API::Invite.resolve(token, code)), self) end
ruby
{ "resource": "" }
q12376
Discordrb.Cache.find_channel
train
def find_channel(channel_name, server_name = nil, type: nil) results = [] if /<#(?<id>\d+)>?/ =~ channel_name # Check for channel mentions separately return [channel(id)] end @servers.values.each do |server| server.channels.each do |channel| results << channel if channel.name == channel_name && (server_name || server.name) == server.name && (!type || (channel.type == type)) end end results end
ruby
{ "resource": "" }
q12377
Discordrb.Cache.find_user
train
def find_user(username, discrim = nil) users = @users.values.find_all { |e| e.username == username } return users.find { |u| u.discrim == discrim } if discrim users end
ruby
{ "resource": "" }
q12378
Discordrb.Message.edit
train
def edit(new_content, new_embed = nil) response = API::Channel.edit_message(@bot.token, @channel.id, @id, new_content, [], new_embed ? new_embed.to_hash : nil) Message.new(JSON.parse(response), @bot) end
ruby
{ "resource": "" }
q12379
Discordrb.Message.create_reaction
train
def create_reaction(reaction) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) API::Channel.create_reaction(@bot.token, @channel.id, @id, reaction) nil end
ruby
{ "resource": "" }
q12380
Discordrb.Message.reacted_with
train
def reacted_with(reaction, limit: 100) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) paginator = Paginator.new(limit, :down) do |last_page| after_id = last_page.last.id if last_page last_page = JSON.parse(API::Channel.get_reactions(@bot.token, @channel.id, @id, reaction, nil, after_id, limit)) last_page.map { |d| User.new(d, @bot) } end paginator.to_a end
ruby
{ "resource": "" }
q12381
Discordrb.Message.delete_reaction
train
def delete_reaction(user, reaction) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) API::Channel.delete_user_reaction(@bot.token, @channel.id, @id, reaction, user.resolve_id) end
ruby
{ "resource": "" }
q12382
Discordrb.Message.delete_own_reaction
train
def delete_own_reaction(reaction) reaction = reaction.to_reaction if reaction.respond_to?(:to_reaction) API::Channel.delete_own_reaction(@bot.token, @channel.id, @id, reaction) end
ruby
{ "resource": "" }
q12383
Discordrb.AuditLogs.process_users
train
def process_users(users) users.each do |element| user = User.new(element, @bot) @users[user.id] = user end end
ruby
{ "resource": "" }
q12384
Discordrb.AuditLogs.process_webhooks
train
def process_webhooks(webhooks) webhooks.each do |element| webhook = Webhook.new(element, @bot) @webhooks[webhook.id] = webhook end end
ruby
{ "resource": "" }
q12385
Discordrb::Commands.CommandChain.execute
train
def execute(event) old_chain = @chain @bot.debug 'Executing bare chain' result = execute_bare(event) @chain_args ||= [] @bot.debug "Found chain args #{@chain_args}, preliminary result #{result}" @chain_args.each do |arg| case arg.first when 'repeat' new_result = '' executed_chain = divide_chain(old_chain).last arg[1].to_i.times do chain_result = CommandChain.new(executed_chain, @bot).execute(event) new_result += chain_result if chain_result end result = new_result # TODO: more chain arguments end end result end
ruby
{ "resource": "" }
q12386
Discordrb.Role.update_from
train
def update_from(other) @permissions = other.permissions @name = other.name @hoist = other.hoist @colour = other.colour @position = other.position @managed = other.managed end
ruby
{ "resource": "" }
q12387
Discordrb.Role.update_data
train
def update_data(new_data) @name = new_data[:name] || new_data['name'] || @name @hoist = new_data['hoist'] unless new_data['hoist'].nil? @hoist = new_data[:hoist] unless new_data[:hoist].nil? @colour = new_data[:colour] || (new_data['color'] ? ColourRGB.new(new_data['color']) : @colour) end
ruby
{ "resource": "" }
q12388
Discordrb.Role.packed=
train
def packed=(packed, update_perms = true) update_role_data(permissions: packed) @permissions.bits = packed if update_perms end
ruby
{ "resource": "" }
q12389
Discordrb.Role.sort_above
train
def sort_above(other = nil) other = @server.role(other.resolve_id) if other roles = @server.roles.sort_by(&:position) roles.delete_at(@position) index = other ? roles.index { |role| role.id == other.id } + 1 : 1 roles.insert(index, self) updated_roles = roles.map.with_index { |role, position| { id: role.id, position: position } } @server.update_role_positions(updated_roles) index end
ruby
{ "resource": "" }
q12390
Discordrb.Member.set_roles
train
def set_roles(role, reason = nil) role_ids = role_id_array(role) API::Server.update_member(@bot.token, @server.id, @user.id, roles: role_ids, reason: reason) end
ruby
{ "resource": "" }
q12391
Discordrb.Member.modify_roles
train
def modify_roles(add, remove, reason = nil) add_role_ids = role_id_array(add) remove_role_ids = role_id_array(remove) old_role_ids = @roles.map(&:id) new_role_ids = (old_role_ids - remove_role_ids + add_role_ids).uniq API::Server.update_member(@bot.token, @server.id, @user.id, roles: new_role_ids, reason: reason) end
ruby
{ "resource": "" }
q12392
Discordrb.Member.add_role
train
def add_role(role, reason = nil) role_ids = role_id_array(role) if role_ids.count == 1 API::Server.add_member_role(@bot.token, @server.id, @user.id, role_ids[0], reason) else old_role_ids = @roles.map(&:id) new_role_ids = (old_role_ids + role_ids).uniq API::Server.update_member(@bot.token, @server.id, @user.id, roles: new_role_ids, reason: reason) end end
ruby
{ "resource": "" }
q12393
Discordrb.Member.remove_role
train
def remove_role(role, reason = nil) role_ids = role_id_array(role) if role_ids.count == 1 API::Server.remove_member_role(@bot.token, @server.id, @user.id, role_ids[0], reason) else old_role_ids = @roles.map(&:id) new_role_ids = old_role_ids.reject { |i| role_ids.include?(i) } API::Server.update_member(@bot.token, @server.id, @user.id, roles: new_role_ids, reason: reason) end end
ruby
{ "resource": "" }
q12394
Discordrb.Member.set_nick
train
def set_nick(nick, reason = nil) # Discord uses the empty string to signify 'no nickname' so we convert nil into that nick ||= '' if @user.current_bot? API::User.change_own_nickname(@bot.token, @server.id, nick, reason) else API::Server.update_member(@bot.token, @server.id, @user.id, nick: nick, reason: nil) end end
ruby
{ "resource": "" }
q12395
Discordrb.Member.update_roles
train
def update_roles(role_ids) @roles = [] role_ids.each do |id| # It is posible for members to have roles that do not exist # on the server any longer. See https://github.com/meew0/discordrb/issues/371 role = @server.role(id) @roles << role if role end end
ruby
{ "resource": "" }
q12396
Discordrb::Commands.CommandBot.execute_command
train
def execute_command(name, event, arguments, chained = false, check_permissions = true) debug("Executing command #{name} with arguments #{arguments}") return unless @commands command = @commands[name] command = command.aliased_command if command.is_a?(CommandAlias) return unless !check_permissions || channels?(event.channel, @attributes[:channels]) || (command && !command.attributes[:channels].nil?) unless command event.respond @attributes[:command_doesnt_exist_message].gsub('%command%', name.to_s) if @attributes[:command_doesnt_exist_message] return end return unless !check_permissions || channels?(event.channel, command.attributes[:channels]) arguments = arg_check(arguments, command.attributes[:arg_types], event.server) if check_permissions if (check_permissions && permission?(event.author, command.attributes[:permission_level], event.server) && required_permissions?(event.author, command.attributes[:required_permissions], event.channel) && required_roles?(event.author, command.attributes[:required_roles]) && allowed_roles?(event.author, command.attributes[:allowed_roles])) || !check_permissions event.command = command result = command.call(event, arguments, chained, check_permissions) stringify(result) else event.respond command.attributes[:permission_message].gsub('%name%', name.to_s) if command.attributes[:permission_message] nil end rescue Discordrb::Errors::NoPermission event.respond @attributes[:no_permission_message] unless @attributes[:no_permission_message].nil? raise end
ruby
{ "resource": "" }
q12397
Discordrb::Commands.CommandBot.simple_execute
train
def simple_execute(chain, event) return nil if chain.empty? args = chain.split(' ') execute_command(args[0].to_sym, event, args[1..-1]) end
ruby
{ "resource": "" }
q12398
Discordrb::Commands.CommandBot.permission?
train
def permission?(user, level, server) determined_level = if user.webhook? || server.nil? 0 else user.roles.reduce(0) do |memo, role| [@permissions[:roles][role.id] || 0, memo].max end end [@permissions[:users][user.id] || 0, determined_level].max >= level end
ruby
{ "resource": "" }
q12399
Discordrb::Commands.CommandBot.create_message
train
def create_message(data) message = Discordrb::Message.new(data, self) return message if message.from_bot? && !@should_parse_self return message if message.webhook? && !@attributes[:webhook_commands] unless message.author Discordrb::LOGGER.warn("Received a message (#{message.inspect}) with nil author! Ignoring, please report this if you can") return end event = CommandEvent.new(message, self) chain = trigger?(message) return message unless chain # Don't allow spaces between the prefix and the command if chain.start_with?(' ') && !@attributes[:spaces_allowed] debug('Chain starts with a space') return message end if chain.strip.empty? debug('Chain is empty') return message end execute_chain(chain, event) # Return the message so it doesn't get parsed again during the rest of the dispatch handling message end
ruby
{ "resource": "" }