inv.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. require './app/commands/base_command.rb'
  2. class InventoryCommand < BaseCommand
  3. def self.opts
  4. {
  5. usage: {
  6. item: 'Searches for an item by name',
  7. amount: 'Specifies the amount of the item to add or remove',
  8. character: 'Searches for your character by the specified name'
  9. }
  10. }
  11. end
  12. def self.cmd
  13. desc = "Add and remove items from characters' inventories"
  14. @cmd ||= Command.new(:inv, desc, opts) do |event, item, amount, name|
  15. # Find character and item
  16. character = Character.restricted_find(name, event.author, ['Archived'])
  17. item = Item.find_by!('name ilike ?', item)
  18. # Return if amount is not a number
  19. raise 'Invalid Amount!' if amount.to_i == 0
  20. InventoryController.edit_inventory(item, amount.to_i, character, event)
  21. rescue ActiveRecord::RecordNotFound => e
  22. error_embed(e.message)
  23. rescue StandardError => e
  24. error_embed(e.message)
  25. end
  26. end
  27. def self.example_command(_event=nil)
  28. [
  29. Item.order('RANDOM()').first.name,
  30. ["-#{rand(1..5)}", rand(1..5)].sample,
  31. Character.where.not(active: 'Deleted').order('RANDOM()').first.name
  32. ]
  33. end
  34. end