Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.16 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3.  
  4. #Requires for different parts of the bot
  5. require 'rubygems'
  6. require 'cinch'
  7. require 'yaml'
  8. require 'digest/md5'
  9.  
  10. #create the bot object
  11. bot = Cinch::Bot.new do
  12.   configure do |c|
  13.  
  14.     #IRC configuration
  15.     c.server = "irc.OMGSERVER.com"
  16.     c.channels = ["#TMH"]
  17.     c.password = "omgpassword"
  18.     c.user = "Superbot"
  19.     c.nick = "Superbot"
  20.     c.port = "6697"
  21.     c.ssl.use = "true"
  22.  
  23.   end
  24.  
  25.  
  26.  
  27.   on :message, /^!help ?(.*)?/ do |m, query|    #Match !help, including it by itself
  28.         if query == "nick"
  29.                 m.reply "Usage: !nick <nickname>. This displays the name associated with the IRC nickname"
  30.         elsif query == "register"
  31.                 m.reply "Usage: !register <nickname> <name>. This registers you with this bot so the !nick command will work for others. Please note you must currently be using this nickname."
  32.         elsif query == "op"
  33.                 m.reply "Usage: !op <username> <password>. This will have the bot OP you. Note, this command works on any message but you should probably private message me so you don't tell everyone your password."
  34.         elsif query == "issue"
  35.                  m.reply "Usage: !issue shows the current issues we are having. Subcommands: List, ID. Use !issue help <subcommand> for more information. See Also: !issueadm help"
  36.         elsif query == "issueadm"
  37.                 m.reply "Usage: !issueadm <command> <issueid> [<notename> <notes>]. current commands: Addnote, Addissue, Delnote, Delissue. See !issueadm help <command> for more information on usage."
  38.     elsif query == "quote"
  39.         m.reply "Usage: !quote. This gives a random quote from the database."
  40.     elsif query == "addquote"
  41.         m.reply "Usage: !addquote <quote>. This adds a quote to the bot's database. One line only! I swear to god if I have to clean up quotes I'll hurt you."
  42.     elsif query == "reparsequotedb"
  43.         m.reply "Usage: !reparsequotedb. This reparses the quote DB in case you had to delete a quote."
  44.         else
  45.                 m.reply "Current commands are !help, !nick, !register, !op, !issue, !issueadm, !quote, !addquote, !reparsequotedb. Use !help <command> for more information."
  46.         end
  47.  
  48.   end
  49.  
  50.  
  51.  
  52.  
  53.   on :message, /^!quote/ do |m|
  54.     yml = YAML.load_file 'quotes.yml'
  55.     count = yml.count
  56.     m.reply yml[1+Random.rand(count)]
  57.   end
  58.  
  59.  
  60.   on :message, /^!addquote (.*)/ do |m, query|
  61.     yml = YAML.load_file 'quotes.yml'
  62.     count = yml.count
  63.     File.open("quotes.yml", "w") do |file|
  64.             yml[count + 1] = query
  65.                 file.write(yml.to_yaml)
  66.         end
  67.     m.reply "Quote had been added!"
  68.   end
  69.  
  70.   on :message,/!parsequotedb/ do |m|
  71.     yml = YAML.load_file 'quotes.yml'
  72.     yml2 = YAML.load_file 'empty.yml'
  73.     File.open("quotes.yml", "w") do |file|
  74.         yml.keys.each_with_index do |var,i|
  75.             yml2[i + 1] = yml[var]         
  76.         end
  77.         file.write(yml2.to_yaml)
  78.     end
  79.     m.reply "Reparsed DB"
  80.   end
  81.            
  82.        
  83.  
  84.  
  85.   on :message, /^!nick ?(.*)/ do |m, query|  #match !nick <anything>(including nothing)
  86.         yml = YAML.load_file 'nicks.yml'
  87.     if ! query.empty?       #Make sure they gave us a nick
  88.             if yml[query.downcase].nil?     #We need to downcase so that it matches the yaml, this makes our life easier.
  89.                     m.reply "Nick #{query} does not exist"
  90.             else
  91.                     m.reply "#{query} is #{ yml[query.downcase]}"
  92.         end
  93.     else
  94.         m.reply "Usage: !nick <nickname>. This displays the name associated with the IRC nickname"
  95.         end
  96.  
  97.   end
  98.  
  99.  
  100.  
  101.   on :message, /^!register ?(.*)/ do |m, query|
  102.         yml = YAML.load_file 'nicks.yml'
  103.     array = query.split     #This splits every word of the query into part of an array, this makes it easier to work with.
  104.     if ! array[0].nil? && ! array[1].nil?       #We want to make sure that they gave us the correct syntax
  105.             if array[0].downcase != m.user.nick.downcase
  106.                     m.reply "#{m.user.nick}, You must be currently using the nickname you wish to register."
  107.         elsif   yml.has_key? array[0].downcase
  108.             m.reply "#{array[0]} is already registered, you derp"
  109.             elsif ! yml.has_key? array[0].downcase
  110.                     File.open("nicks.yml", "w") do |file|
  111.                         yml[array[0].downcase] = array[1..array.count].join(' ') #This makes it so that if they used a multipart name (such as first last) it joins it together.
  112.                         file.write(yml.to_yaml)
  113.             end
  114.                     m.reply "#{query} registered as #{array[1..array.count].join(' ')}!"
  115.        
  116.         else
  117.             m.reply "OOPS Something dun fucked up"          #We should never see this, but we want to know if we do.
  118.             end
  119.     else
  120.         m.reply "Usage: !register <nickname> <name>. This registers you with this bot so the !nick command will work for others. Please note you must currently be using this nickname."
  121.     end
  122.   end
  123.  
  124.   on :message, /^!op (.*)/ do |m, query|
  125.     array = query.split
  126.     puts "#{array[0]} #{array[1]}"
  127.     yml = YAML.load_file 'ops.yml'
  128.     if ! array[0].nil? && ! array[1].nil?
  129.         if Digest::MD5.hexdigest(array[1]) ==  yml[array[0].downcase]   #Hash the password then check against the yaml to see if they supplied the correct one
  130.             bot.channels[0].op(m.user)
  131.         else
  132.             m.reply "Username/Password Mismatch"        #We don't really care what went wrong with their user or pass, just that something did.
  133.         end
  134.     else
  135.         m.reply "Usage: !op <username> <password>. This will have the bot OP you. Note, this command only works any message but you should probably private message me so you don't tell everyone your password."
  136.     end
  137.   end
  138.  
  139.  
  140.   on :message, /^!issue[^adm] ?(.*)/ do |m, query|      #[^adm] is there so !issueadm does not match.
  141.     array = query.split
  142.     if ! array[0].nil?
  143.         if array[0].downcase == "help"
  144.             if ! array[1].nil?
  145.                 if array[1].downcase == "list"
  146.                     m.reply "Usage: !issue list shows the IDs of all current issues. You can use these numbers with !issue id to get more information"
  147.                 elsif array[1].downcase == "id"
  148.                     m.reply "Usage: !issue id <id> Lists all notes regarding the issue of the ID specified. Use !issue list to get the current list of IDs"
  149.                 else
  150.                                 m.reply "Usage: !issue shows the current issues we are having. Subcommands: List, ID. Use !help <subcommand> for more information. See Also: !issueadm help"
  151.                 end
  152.             else
  153.                 m.reply "Usage: !issue shows the current issues we are having. Subcommands: List, ID. Use !issue help <subcommand> for more information. See Also: !issueadm help"
  154.             end
  155.         elsif array[0].downcase == "list"
  156.             yml = YAML.load_file 'list.yml'
  157.             yml.keys.sort.each do |var|         #Basic for/each loop so the bot can give us the entire list.
  158.                 m.reply "#{var}: #{yml[var]}"      
  159.             end
  160.    
  161.         elsif array[0].downcase == "id"
  162.             yml = YAML.load_file 'issues.yml'
  163.             if ! yml.has_key? array[1].to_i
  164.                 m.reply "ID #{array[1]} does not exist, use !issue list to get the current IDs of issues."
  165.             else
  166.                 m.reply "Issue #{array[1]}:"
  167.                 yml[array[1].to_i].keys.each do |var|
  168.                     m.reply "#{var}: #{yml[array[1].to_i][var]}"        #The bot will automatically split if the IRC character limit is reached, so we just need to fetch it
  169.                 end
  170.        
  171.             end
  172.          else
  173.                     m.reply "Usage: !issue shows the current issues we are having. Subcommands: List, ID. Use !issue help <subcommand> for more information. See Also: !issueadm help"
  174.         end
  175.     else
  176.         m.reply "Usage: !issue shows the current issues we are having. Subcommands: List, ID. Use !issue help <subcommand> for more information. See Also: !issueadm help"
  177.     end
  178.   end
  179.  
  180.  
  181.   on :message, /^!issueadm ?(.*)/ do |m, query|
  182.     array = query.split
  183.     yml = YAML.load_file 'issues.yml'
  184.     if ! array[0].nil?
  185.         if array[0].downcase == "help"
  186.             if ! array[1].nil?
  187.                 if array[1].downcase == "addnote"
  188.                     m.reply "Usage: !issueadm addnote <issueid> <notename> <note>. Please note that you can not use a noteid already in use for this issue."
  189.                 elsif array[1].downcase == "addissue"
  190.                     m.reply "Usage: !issueadm addissue <issueid> <issuename>. Please note, the issueid must be numerical and not aleady exist. The <issuename> will be added as the title"
  191.                 elsif array[1].downcase == "delnote"
  192.                     m.reply "Usage: !issueadm delnote <issueid> <notename>. Note you must be OPed to delete notes."
  193.                 elsif  array[1].downcase == "delissue"
  194.                     m.reply "Usage: !issueadm delissue <issueid>. Note you must be OPed to delete issues."
  195.                 end
  196.             else
  197.                 m.reply "Usage: !issueadm <command> <issueid> [<notename> <notes>]. current commands: Addnote, Addissue, Delnote, Delissue. See !issueadm help <command> for more information on usage."
  198.             end
  199.         elsif array[0].downcase == "addnote"
  200.             if ! array[1].nil? && ! array[2].nil? && ! array[3].nil?       
  201.                 if ! yml.has_key? array[1].to_i
  202.                     m.reply "Issue does not exist, use !issue list to get the issue ID."
  203.                 elsif yml[array[1].to_i].has_key? array[2].downcase
  204.                     m.reply "Note Name already exists, use !issue id <issueid> to see current notes."
  205.                 else
  206.                     File.open("issues.yml", "w") do |file|         
  207.                                         yml[array[1].to_i][array[2].to_s.downcase] = array[3..array.count].join(' ')  #Make sure to join it back together so the yaml is written correctly
  208.                                         file.write(yml.to_yaml)
  209.                     end
  210.                     m.reply "Note #{array[2]} Added."
  211.                 end
  212.             else
  213.                 m.reply "Usage: !issueadm addnote <issueid> <notename> <note>. Please note that you can not use a noteid already in use for this issue."
  214.                         end
  215.         elsif array[0].downcase == "addissue"
  216.             if ! array[1].nil? && ! array[2].nil?
  217.                 if array[1].to_i == 0 or yml.has_key? array[1].to_i # to_i returns 0 if there is no numerical value at the start, so we just don't allow 0 for the sake of laziness
  218.                     m.reply "Issue either already exists, you used a non-numerical ID, or you used an ID of 0."
  219.                 else
  220.                     yml2 = YAML.load_file 'list.yml'
  221.                     File.open("list.yml", "w") do |file|
  222.                         yml2[array[1].to_i] = array[2..array.count].join(' ')
  223.                         file.write(yml2.to_yaml)
  224.                     end
  225.                     File.open("issues.yml", "w") do |file|
  226.                                                 yml[array[1].to_i] = {"Title" => array[2..array.count].join(' ')}   #Make sure the issues yaml is written correctly so we can append to it.
  227.                                                 file.write(yml.to_yaml)
  228.                                         end
  229.                     m.reply "Issue #{array[1]} Added."
  230.                 end
  231.             else
  232.                  m.reply "Usage: !issueadm addissue <issueid> <issuename>. Please note, the issueid must be numerical and not aleady exist. The <issuename> will be added as the title"
  233.             end
  234.         elsif array[0].downcase == "delnote"
  235.             if ! bot.channels[0].opped? m.user.nick
  236.                 m.reply "You must be Opped in order to use this command."
  237.             else
  238.                 if ! array[1].nil? && ! array[2].nil?
  239.                     if ! yml.has_key? array[1].to_i
  240.                         m.reply "Issue does not exist, use !issue list to get the issue ID."
  241.                     elsif ! yml[array[1].to_i].has_key? array[2].downcase
  242.                         m.reply "Note does not exist, use !issue id <ID> to see the current notes."
  243.                     else
  244.                         File.open("issues.yml", "w") do |file|
  245.                                                     yml[array[1].to_i].delete(array[2].downcase)
  246.                                                     file.write(yml.to_yaml)
  247.                                             end
  248.                         m.reply "Note #{array[2]} has been deleted."
  249.                     end
  250.                 else   
  251.                     m.reply "Usage: !issueadm delnote <issueid> <notename>. Note you must be Opped to delete notes."
  252.                 end
  253.             end
  254.         elsif array[0].downcase == "delissue"
  255.             yml2 = YAML.load_file('list.yml')
  256.             if ! bot.channels[0].opped? m.user.nick
  257.                 m.reply "You must be Opped in order to use this command."
  258.             else
  259.                 if !array[1].nil?
  260.                     if ! yml.has_key? array[1].to_i and ! yml2.has_key? array[1].to_i
  261.                         m.reply "Issue ddoes not exist, use !issue list to get the issue ID."
  262.                     else
  263.                         File.open("issues.yml", "w") do |file|
  264.                             yml.delete(array[1].to_i)
  265.                             file.write(yml.to_yaml)
  266.                         end
  267.                         File.open("list.yml", "w") do |file|
  268.                             yml2.delete(array[1].to_i)
  269.                             file.write(yml2.to_yaml)
  270.                         end
  271.                         m.reply "Issue has been Deleted."
  272.                     end
  273.                 else
  274.                     m.reply "Usage: !issueadm delissue <issueid>. Note you must be OPed to delete issues."
  275.                 end
  276.             end
  277.          end   
  278.     end
  279.   end
  280. end
  281. bot.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement