<commit_before>require 'rest_client'
require 'nokogiri'
require 'open-uri'
require 'yaml'
require 'cgi'
require './parser.rb'

class String
  $owner = YAML.load_file('config/options.yaml')['owner']
  $ranks = YAML.load_file('config/ranks.yaml')
    
  def can(command)
    if (self.match(/\W\s*#{$owner}/i)) then return true end
    groups = {  
        'unranked' => 0,
        '+' => 1,
        '%' => 2,
        '@' => 3,
        '#' => 4,
        '&' => 4,
        '~' => 5,
        'off' => 6
    }
    rank = self[0]
    if (!$ranks.include? command or groups[$ranks[command]] == 'unranked') then return true end
    if (!groups.keys.include? rank) then rank = 'unranked' end
    if (groups[rank] >= groups[$ranks[command]])
      return true
    else
      return false
    end
  end
end

#Add your commands here
module Commands
  def sudo(target, user)
    return '' unless user.can('sudo')
    if target.tainted? 
      return ''
    else
      return eval(target)
    end
  end

  def last(target, user)
    return '' unless user.can('last')
    begin
      return "Last message of #{target} was \"#{ShowdownBot.messages[target][1][0]}\" at #{Time.at(ShowdownBot.messages[target][0].to_i)}."
    rescue
      return "I can't remember #{target}'s last message."
    end
  end

  def gsub(target, user)
    return '' unless user.can('gsub')
    begin
      looking_for_word = target.split(',')[0]
      replace_word = target.split(',')[1]
      return "What #{user} meant to say was: \"#{ShowdownBot.messages[user[1..-1]][1][0].gsub(looking_for_word,replace_word)}\""
    rescue
      return "You gotta say something first."
    end
  end

  def rank(target, user)
    return '' unless user.can('rank')
    if !can('rank', user) then return '' end
    if ($ranks[command].nil?) then command_rank = 'unranked' else command_rank = $ranks[command] end
    return "The current rank for #{command} is #{command_rank}."
  end

  def google(target, user)
    return '' unless user.can('google')
    url = "http://www.google.com/search?q=#{CGI.escape(target)}"
    data = Nokogiri::HTML(open(url)).at('h3.r')
    data_string = data.at('./following::div').children.first.text.gsub(/(CachedSimilar|Cached)/,'')
    text = data.text
    return "#{text} | #{data_string}"
  end

  def salt(target, user)
    return '' unless user.can('salt')
    return "#{target} is #{(Random.rand(0.0..100.0)).round(2)}% salty."
  end

  def reload(target, user)
    return '' unless user.can('reload')
    begin
      load './commands.rb'
      return "Commands reloaded."
    rescue
      return "Error."
    end
  end

  def fight(target, user)
    return '' unless user.can('fight')
    person1 = target.split(',')[0]
    person2 = target.split(',')[1]
    if Random.rand(0..1) == 1 then userpicked = person1 else userpicked = person2 end
    message = "Hmm, if #{person1} and #{person2} were to fight, #{userpicked} would have a #{Random.rand(0..100)}% chance of winning."
    return message
  end

  def set(target, user)
    return '' unless user.can('set')
    target = target.gsub(/ /,'').split(',')
    command = target[0]
    rank = target[1]
    ranks = ['unranked','+','%','@','#','&','~','off','']
    if (!ranks.include? rank) then return "Have to be one of the following ranks: #{settableranks.join(', ')}" end
    $ranks[command] = rank
    File.open('ranks.yaml','w') { |b| b.write($ranks.to_yaml)}
    return "The command #{command} is now set to #{rank}."
  end

  def about(target, user)
    return "/pm #{user}, **BlizzyBot** by: blizzardq. Made with Ruby #{RUBY_VERSION}"
  end

  def helix(target, user)
    return '' unless user.can('set')
    return File.readlines('data/helix.txt').sample
  end

  def pick(target, user)
    return '' unless user.can('pick')
    randompick = target.split(',').sample<commit_msg>Update about command.<commit_after>require 'rest_client'
require 'nokogiri'
require 'open-uri'
require 'yaml'
require 'cgi'
require './parser.rb'

class String
  $owner = YAML.load_file('config/options.yaml')['owner']
  $ranks = YAML.load_file('config/ranks.yaml')
    
  def can(command)
    if (self.match(/\W\s*#{$owner}/i)) then return true end
    groups = {  
        'unranked' => 0,
        '+' => 1,
        '%' => 2,
        '@' => 3,
        '#' => 4,
        '&' => 4,
        '~' => 5,
        'off' => 6
    }
    rank = self[0]
    if (!$ranks.include? command or groups[$ranks[command]] == 'unranked') then return true end
    if (!groups.keys.include? rank) then rank = 'unranked' end
    if (groups[rank] >= groups[$ranks[command]])
      return true
    else
      return false
    end
  end
end

#Add your commands here
module Commands
  def sudo(target, user)
    return '' unless user.can('sudo')
    if target.tainted? 
      return ''
    else
      return eval(target)
    end
  end

  def last(target, user)
    return '' unless user.can('last')
    begin
      return "Last message of #{target} was \"#{ShowdownBot.messages[target][1][0]}\" at #{Time.at(ShowdownBot.messages[target][0].to_i)}."
    rescue
      return "I can't remember #{target}'s last message."
    end
  end

  def gsub(target, user)
    return '' unless user.can('gsub')
    begin
      looking_for_word = target.split(',')[0]
      replace_word = target.split(',')[1]
      return "What #{user} meant to say was: \"#{ShowdownBot.messages[user[1..-1]][1][0].gsub(looking_for_word,replace_word)}\""
    rescue
      return "You gotta say something first."
    end
  end

  def rank(target, user)
    return '' unless user.can('rank')
    if !can('rank', user) then return '' end
    if ($ranks[command].nil?) then command_rank = 'unranked' else command_rank = $ranks[command] end
    return "The current rank for #{command} is #{command_rank}."
  end

  def google(target, user)
    return '' unless user.can('google')
    url = "http://www.google.com/search?q=#{CGI.escape(target)}"
    data = Nokogiri::HTML(open(url)).at('h3.r')
    data_string = data.at('./following::div').children.first.text.gsub(/(CachedSimilar|Cached)/,'')
    text = data.text
    return "#{text} | #{data_string}"
  end

  def salt(target, user)
    return '' unless user.can('salt')
    return "#{target} is #{(Random.rand(0.0..100.0)).round(2)}% salty."
  end

  def reload(target, user)
    return '' unless user.can('reload')
    begin
      load './commands.rb'
      return "Commands reloaded."
    rescue
      return "Error."
    end
  end

  def fight(target, user)
    return '' unless user.can('fight')
    person1 = target.split(',')[0]
    person2 = target.split(',')[1]
    if Random.rand(0..1) == 1 then userpicked = person1 else userpicked = person2 end
    message = "Hmm, if #{person1} and #{person2} were to fight, #{userpicked} would have a #{Random.rand(0..100)}% chance of winning."
    return message
  end

  def set(target, user)
    return '' unless user.can('set')
    target = target.gsub(/ /,'').split(',')
    command = target[0]
    rank = target[1]
    ranks = ['unranked','+','%','@','#','&','~','off','']
    if (!ranks.include? rank) then return "Have to be one of the following ranks: #{settableranks.join(', ')}" end
    $ranks[command] = rank
    File.open('ranks.yaml','w') { |b| b.write($ranks.to_yaml)}
    return "The command #{command} is now set to #{rank}."
  end

  def about(target, user)
    return "**BlizzyBot** : made by BlizzardQ. Made with Ruby #{RUBY_VERSION}"
  end

  def helix(target, user)
    return '' unless user.can('set')
    return File.readlines('data/helix.txt').sample
  end

  def pick(target, user)
    return '' unless user.can('pick')
    randompick = target.split(',').sample
{'fraction_non_alphanumeric': 0.17458447847140426, 'fraction_numerical': 0.011255071325742704, 'mean_word_length': 3.0369783412572637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2296710', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2595, 'n_words': 670}