Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.98 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.failverse.com"
  16.     c.channels = ["#TMH"]
  17.     c.password = "omgzpassword"
  18.     c.user = "Superbot"
  19.     c.nick = "Superbot"
  20.     c.port = "6697"
  21.     c.ssl.use = "true"
  22.  
  23.   end
  24.  
  25.   on :message, /^!nick ?(.*)/ do |m, query|  #match !nick <anything>(including nothing)
  26.         yml = YAML.load_file 'nicks.yml'
  27.     if ! query.empty?       #Make sure they gave us a nick
  28.             if yml[query.downcase].nil?     #We need to downcase so that it matches the yaml, this makes our life easier.
  29.                     m.reply "Nick #{query} does not exist"
  30.             else
  31.                     m.reply "#{query} is #{ yml[query.downcase]}"
  32.         end
  33.     else
  34.         m.reply "Usage: !nick <nickname>. This displays the name associated with the IRC nickname"
  35.         end
  36.  
  37.   end
  38.  
  39.   on :message, /^!help ?(.*)?/ do |m, query|    #Match !help, including it by itself
  40.     if query == "nick"
  41.                 m.reply "Usage: !nick <nickname>. This displays the name associated with the IRC nickname"
  42.         elsif query == "register"
  43.                 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."
  44.         elsif query == "op"
  45.                 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."
  46.     elsif query == "issue"
  47.          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"
  48.     elsif query == "issueadm"
  49.         m.reply "Usage: !issueadm <command> <issueid> [<notename> <notes>]. current commands: Addnote, Addissue, Delnote, Delissue. See !issueadm help <command> for more information on usage."
  50.         else
  51.                 m.reply "Current commands are !help, !nick, !register, !op, !issue, !issueadm. Use !help nick/register/op/issue/issueadm for more information."
  52.         end
  53.  
  54.   end
  55.  
  56.  
  57.  
  58.  
  59.   on :message, /^!register ?(.*)/ do |m, query|
  60.         yml = YAML.load_file 'nicks.yml'
  61.     array = query.split     #This splits every word of the query into part of an array, this makes it easier to work with.
  62.     if ! array[0].nil? && ! array[1].nil?       #We want to make sure that they gave us the correct syntax
  63.             if array[0].downcase != m.user.nick.downcase
  64.                     m.reply "#{m.user.nick}, You must be currently using the nickname you wish to register."
  65.         elsif   yml.has_key? array[0].downcase
  66.             m.reply "#{array[0]} is already registered, you derp"
  67.             elsif ! yml.has_key? array[0].downcase
  68.                     File.open("nicks.yml", "w") do |file|
  69.                         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.
  70.                         file.write(yml.to_yaml)
  71.             end
  72.                     m.reply "#{query} registered as #{array[1..array.count].join(' ')}!"
  73.        
  74.         else
  75.             m.reply "OOPS Something dun fucked up"          #We should never see this, but we want to know if we do.
  76.             end
  77.     else
  78.         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."
  79.     end
  80.   end
  81.  
  82.   on :message, /^!op (.*)/ do |m, query|
  83.     array = query.split
  84.     puts "#{array[0]} #{array[1]}"
  85.     yml = YAML.load_file 'ops.yml'
  86.     if ! array[0].nil? && ! array[1].nil?
  87.         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
  88.             bot.channels[0].op(m.user)
  89.         else
  90.             m.reply "Username/Password Mismatch"        #We don't really care what went wrong with their user or pass, just that something did.
  91.         end
  92.     else
  93.         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."
  94.     end
  95.   end
  96.  
  97.  
  98.   on :message, /^!issue[^adm] ?(.*)/ do |m, query|      #[^adm] is there so !issueadm does not match.
  99.     array = query.split
  100.     if ! array[0].nil?
  101.         if array[0].downcase == "help"
  102.             if ! array[1].nil?
  103.                 if array[1].downcase == "list"
  104.                     m.reply "Usage: !issue list shows the IDs of all current issues. You can use these numbers with !issue id to get more information"
  105.                 elsif array[1].downcase == "id"
  106.                     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"
  107.                 else
  108.                                 m.reply "Usage: !issue shows the current issues we are having. Subcommands: List, ID. Use !help <subcommand> for more information. See Also: !issueadm help"
  109.                 end
  110.             else
  111.                 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"
  112.             end
  113.         elsif array[0].downcase == "list"
  114.             yml = YAML.load_file 'list.yml'
  115.             yml.keys.sort.each do |var|         #Basic for/each loop so the bot can give us the entire list.
  116.                 m.reply "#{var}: #{yml[var]}"      
  117.             end
  118.    
  119.         elsif array[0].downcase == "id"
  120.             yml = YAML.load_file 'issues.yml'
  121.             if ! yml.has_key? array[1].to_i
  122.                 m.reply "ID #{array[1]} does not exist, use !issue list to get the current IDs of issues."
  123.             else
  124.                 m.reply "Issue #{array[1]}:"
  125.                 yml[array[1].to_i].keys.each do |var|
  126.                     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
  127.                 end
  128.        
  129.             end
  130.          else
  131.                     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"
  132.         end
  133.     else
  134.         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"
  135.     end
  136.   end
  137.  
  138.  
  139.   on :message, /^!issueadm ?(.*)/ do |m, query|
  140.     array = query.split
  141.     yml = YAML.load_file 'issues.yml'
  142.     if ! array[0].nil?
  143.         if array[0].downcase == "help"
  144.             if ! array[1].nil?
  145.                 if array[1].downcase == "addnote"
  146.                     m.reply "Usage: !issueadm addnote <issueid> <notename> <note>. Please note that you can not use a noteid already in use for this issue."
  147.                 elsif array[1].downcase == "addissue"
  148.                     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"
  149.                 elsif array[1].downcase == "delnote"
  150.                     m.reply "Usage: !issueadm delnote <issueid> <notename>. Note you must be OPed to delete notes."
  151.                 elsif  array[1].downcase == "delissue"
  152.                     m.reply "Usage: !issueadm delissue <issueid>. Note you must be OPed to delete issues."
  153.                 end
  154.             else
  155.                 m.reply "Usage: !issueadm <command> <issueid> [<notename> <notes>]. current commands: Addnote, Addissue, Delnote, Delissue. See !issueadm help <command> for more information on usage."
  156.             end
  157.         elsif array[0].downcase == "addnote"
  158.             if ! array[1].nil? && ! array[2].nil? && ! array[3].nil?       
  159.                 if ! yml.has_key? array[1].to_i
  160.                     m.reply "Issue does not exist, use !issue list to get the issue ID."
  161.                 elsif yml[array[1].to_i].has_key? array[2].downcase
  162.                     m.reply "Note Name already exists, use !issue id <issueid> to see current notes."
  163.                 else
  164.                     File.open("issues.yml", "w") do |file|         
  165.                                         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
  166.                                         file.write(yml.to_yaml)
  167.                     end
  168.                     m.reply "Note #{array[2]} Added."
  169.                 end
  170.             else
  171.                 m.reply "Usage: !issueadm addnote <issueid> <notename> <note>. Please note that you can not use a noteid already in use for this issue."
  172.                         end
  173.         elsif array[0].downcase == "addissue"
  174.             if ! array[1].nil? && ! array[2].nil?
  175.                 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
  176.                     m.reply "Issue either already exists, you used a non-numerical ID, or you used an ID of 0."
  177.                 else
  178.                     yml2 = YAML.load_file 'list.yml'
  179.                     File.open("list.yml", "w") do |file|
  180.                         yml2[array[1].to_i] = array[2..array.count].join(' ')
  181.                         file.write(yml2.to_yaml)
  182.                     end
  183.                     File.open("issues.yml", "w") do |file|
  184.                                                 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.
  185.                                                 file.write(yml.to_yaml)
  186.                                         end
  187.                     m.reply "Issue #{array[1]} Added."
  188.                 end
  189.             else
  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.             end
  192.         elsif array[0].downcase == "delnote"
  193.             if ! bot.channels[0].opped? m.user.nick
  194.                 m.reply "You must be Opped in order to use this command."
  195.             else
  196.                 if ! array[1].nil? && ! array[2].nil?
  197.                     if ! yml.has_key? array[1].to_i
  198.                         m.reply "Issue does not exist, use !issue list to get the issue ID."
  199.                     elsif ! yml[array[1].to_i].has_key? array[2].downcase
  200.                         m.reply "Note does not exist, use !issue id <ID> to see the current notes."
  201.                     else
  202.                         File.open("issues.yml", "w") do |file|
  203.                                                     yml[array[1].to_i].delete(array[2].downcase)
  204.                                                     file.write(yml.to_yaml)
  205.                                             end
  206.                         m.reply "Note #{array[2]} has been deleted."
  207.                     end
  208.                 else   
  209.                     m.reply "Usage: !issueadm delnote <issueid> <notename>. Note you must be Opped to delete notes."
  210.                 end
  211.             end
  212.         elsif array[0].downcase == "delissue"
  213.             yml2 = YAML.load_file('list.yml')
  214.             if ! bot.channels[0].opped? m.user.nick
  215.                 m.reply "You must be Opped in order to use this command."
  216.             else
  217.                 if !array[1].nil?
  218.                     if ! yml.has_key? array[1].to_i and ! yml2.has_key? array[1].to_i
  219.                         m.reply "Issue ddoes not exist, use !issue list to get the issue ID."
  220.                     else
  221.                         File.open("issues.yml", "w") do |file|
  222.                             yml.delete(array[1].to_i)
  223.                             file.write(yml.to_yaml)
  224.                         end
  225.                         File.open("list.yml", "w") do |file|
  226.                             yml2.delete(array[1].to_i)
  227.                             file.write(yml2.to_yaml)
  228.                         end
  229.                         m.reply "Issue has been Deleted."
  230.                     end
  231.                 else
  232.                     m.reply "Usage: !issueadm delissue <issueid>. Note you must be OPed to delete issues."
  233.                 end
  234.             end
  235.          end   
  236.     end
  237.   end
  238. end
  239. bot.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement