Guest User

Untitled

a guest
May 27th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #-- vim:sw=2:et
  2. #++
  3. #
  4. # :title: Bored plugin
  5. #
  6. # Author:: David Gadling <dave@toasterwaffles.com>
  7. # Copyright:: (C) 2008 David Gadling
  8. #
  9. # I'm bored, give me a link to something interesting
  10.  
  11. class BoredPlugin < Plugin
  12.  
  13. def help(plugin, topic="")
  14. "bored plugin: figure out something fun to do"
  15. end
  16.  
  17. def addItem(m, params)
  18. toAdd = params[:item]
  19.  
  20. items = @registry[:items] || Array.new
  21.  
  22. if items.include?(toAdd)
  23. m.reply("I already have #{toAdd}")
  24. return
  25. end
  26.  
  27. items.push(toAdd.to_s)
  28. @registry[:items] = items
  29.  
  30. m.reply("Added item #{items.length}")
  31. end
  32.  
  33. def delItem(m, params)
  34. victim = params[:id].to_s.to_i-1
  35. items = @registry[:items] || Array.new
  36.  
  37. if items.length == 0
  38. m.reply("Nothing to delete!")
  39. return
  40. end
  41.  
  42. if items.length < victim
  43. m.reply("I don't have anything at index #{victim+1}")
  44. return
  45. end
  46.  
  47. deleted = items.delete_at(victim)
  48.  
  49. @registry[:items] = items
  50. m.reply("Deleted item #{victim+1}, #{deleted}")
  51. end
  52.  
  53. def getItem(m, params)
  54. items = @registry[:items] || Array.new
  55.  
  56. if items.length == 0
  57. m.reply("I can't help, I don't have any advice")
  58. return
  59. end
  60.  
  61. victim = rand(items.length)
  62. m.reply("#{m.sourcenick}, #{items[victim]}")
  63. end
  64.  
  65. def getCount(m, params)
  66. m.reply("I have #{@registry[:items].length} entries")
  67. end
  68.  
  69. def getList(m, params)
  70. items = @registry[:items]
  71.  
  72. min = 0
  73. max = items.length-1
  74. shortStyle = false
  75. if params[:range] != nil
  76. range = params[:range].to_s.split("-")
  77. min = range[0].to_i-1
  78. if range.length > 1
  79. max = range[1].to_i-1
  80. end
  81. else
  82. shortStyle = true
  83. end
  84.  
  85. if shortStyle == true
  86. m.reply(items.join(", "))
  87. else
  88. min.upto(max) do |i|
  89. m.reply("[#{i+1}]: #{items[i]}")
  90. end
  91. end
  92. end
  93. end
  94.  
  95. plugin = BoredPlugin.new
  96. plugin.map 'bored add *item', :action => :addItem
  97. plugin.map 'bored del :id', :action => :delItem, :auth_path => ':del'
  98. plugin.map 'bored count', :action => :getCount
  99. plugin.map 'bored list :range', :action => :getList, :requiremens => {:range => /^\d+-\d*/}
  100. plugin.map 'bored list', :action => :getList
  101. plugin.map "I'm bored", :action => :getItem
  102.  
  103. plugin.default_auth('bored::del', false)
Add Comment
Please, Sign In to add comment