Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'discordrb'
  5. require 'yaml'
  6. require './random.rb'
  7.  
  8.  
  9. tempyaml = YAML.load_file 'quotes.yml'
  10. r = Rand.new(tempyaml.size)
  11. d20 = Rand.new(19)
  12.  
  13.  
  14. bot = Discordrb::Commands::CommandBot.new token: 'SECRET GO HERE LOL', client_id: 272095539443662849, prefix: '!'
  15.  
  16.  
  17. #puts "This bot's invite URL is #{bot.invite_url}."
  18. #puts 'Click on it to invite it to your server.'
  19.  
  20. bot.command(:quote, min_args: 0, max_args: 1) do |event, args|
  21. yml = YAML.load_file 'quotes.yml'
  22. r.size = yml.size
  23. if args.nil?
  24. num = r.next
  25. else
  26. num = args.to_i
  27. if num < 0
  28. num = yml.count + num
  29. end
  30. end
  31. response = yml[num]
  32. if response.nil?
  33. event.respond "I was unable to find a quote with ID #{num}, therefore I am generating a random quote instead."
  34. num = r.next
  35. response = yml[num]
  36. end
  37. event.respond "Quote ID #{num}: #{response}"
  38. end
  39.  
  40.  
  41.  
  42. bot.command(:addquote, min_args: 1) do |event, args|
  43. yml = YAML.load_file 'quotes.yml'
  44. size = yml.size
  45. File.open("quotes.yml", "w") do |file|
  46. yml[size] = query
  47. file.write(yml.to_yaml)
  48. end
  49. r.size = yml.size
  50. event.respond "Quote had been added as Quote ID #{size}!"
  51. end
  52.  
  53. bot.command(:quoteadd, min_args: 1) do |event, args|
  54. yml = YAML.load_file 'quotes.yml'
  55. size = yml.size
  56. File.open("quotes.yml", "w") do |file|
  57. yml[size] = query
  58. file.write(yml.to_yaml)
  59. end
  60. r.size = yml.size
  61. event.respond "Quote had been added as Quote ID #{size}!"
  62. end
  63.  
  64.  
  65. bot.command(:grepquote, min_args: 1) do |event, args|
  66. yml = YAML.load_file 'quotes.yml'
  67. cnt = 0
  68. yml.each_pair do |key, value|
  69. if value.downcase =~ /#{args.downcase}/
  70. cnt += 1
  71. end
  72. end
  73. if cnt == 0
  74. event.respond "No matches found for #{args}, try again."
  75. elsif cnt < 10
  76. yml.each_pair do |key, value|
  77. if value.downcase =~ /#{args.downcase}/
  78. event.respond "Match Found. ID: #{key}, Quote: #{value}"
  79. end
  80. end
  81. else
  82. event.respond "More then 10 matches found, please limit your search further."
  83. end
  84. end
  85.  
  86. bot.command(:reparsequotedb) do |event|
  87. yml = YAML.load_file 'quotes.yml'
  88. yml2 = {}
  89. File.open("quotes.yml", "w") do |file|
  90. yml.keys.each_with_index do |var,i|
  91. yml2[i] = yml[var]
  92. end
  93. file.write(yml2.to_yaml)
  94. end
  95. r.size = yml.size
  96. event.respond "Reparsed DB"
  97. end
  98.  
  99. bot.run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement