image.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. require './app/commands/base_command.rb'
  2. class ImageCommand < BaseCommand
  3. def self.opts
  4. {
  5. usage: {
  6. name: "Searches for your character, by name",
  7. keyword: "Searches for your character's image, by title or keyword," +
  8. " or specifies the title if new image. If an image exists for the" +
  9. " character with that name, the new flag and url will overwrite it",
  10. flag: "Delete indicates you want to delete the specified image." +
  11. " With a new image, SFW or NSFW should be used to specify image rating",
  12. url: "The url to the new image. This must be a direct link, so check" +
  13. " for a .jpg/.png or similar. Animated gifs are allowed",
  14. }
  15. }
  16. end
  17. def self.cmd
  18. desc = "View, add and edit your characters' images. " +
  19. "Usable only in a direct message with R0ry!"
  20. @cmd ||= Command.new(:image, desc, opts) do |event, name, keyword, tag, url, id|
  21. # Save the character creator's User, and find the character
  22. user = id ? User.find(id) : User.find(event.author.id)
  23. character = Character.where(user_id: user.id).find_by('name ilike ?', name)
  24. # Determine action, and execute
  25. if url
  26. # Error if any fields are invalid
  27. valid =
  28. keyword && url && tag&.match(/n?sfw/i)
  29. raise 'Invalid Parameters!' unless valid
  30. # Create and submit image application
  31. embed = CharImage.to_form(
  32. char: character,
  33. keyword: keyword,
  34. category: tag,
  35. url: url,
  36. user_id: user.id
  37. )
  38. [
  39. BotResponse.new(
  40. destination: ENV['APP_CH'].to_i,
  41. embed: embed,
  42. reactions: Emoji::REQUEST
  43. ),
  44. BotResponse.new(
  45. embed: success_embed("Your image has been sumbitted for approval!")
  46. )
  47. ]
  48. elsif tag&.match(/delete/i)
  49. # Find character's image and destroy it
  50. CharImage.where(char_id: character.id).
  51. find_by('keyword ilike ?', keyword).destroy
  52. success_embed("Removed image: #{keyword}")
  53. elsif keyword
  54. # Find image, and display
  55. image = CharImage.where(char_id: character.id).
  56. find_by('keyword ilike ?', keyword)
  57. character_embed(
  58. character: character,
  59. event: event,
  60. section: :image,
  61. image: image
  62. )
  63. # Show all character images
  64. else
  65. image_list_embed(character, event)
  66. end
  67. rescue ActiveRecord::RecordNotFound => e
  68. error_embed("Record not Found!", e.message)
  69. rescue StandardError => e
  70. error_embed(e.message)
  71. end
  72. end
  73. def self.example_command(event=nil)
  74. kws = ['Fluffy', 'Pupper', 'Midnight Drink', 'On the prowl', 'Bork']
  75. image_url = "https://i.imgur.com/Xa9WgSn.jpg"
  76. case ['name', 'keyword', 'delete', 'update', 'url'].sample
  77. when 'name'
  78. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  79. when 'keyword'
  80. img = CharImage.where.not(keyword: 'Default').order('RANDOM()').first
  81. [Character.find(img.char_id).name, img.keyword]
  82. when 'delete'
  83. img = CharImage.where.not(keyword: 'Default').order('RANDOM()').first
  84. [Character.find(img.char_id).name, img.keyword, 'delete']
  85. when 'update'
  86. img = CharImage.where.not(keyword: 'Default').order('RANDOM()').first
  87. [Character.find(img.char_id).name, img.keyword, 'sfw', image_url]
  88. when 'url'
  89. char = Character.where.not(active: 'Deleted').order('RANDOM()').first.name
  90. [char, kws.sample, 'sfw', image_url]
  91. end
  92. end
  93. def admin_opts
  94. {
  95. usage: {
  96. name: "Character's name",
  97. keyword: "Image Keyword",
  98. tag: "SFW/NSFW or Delete",
  99. url: "Image URL",
  100. id: "Character's user id"
  101. }
  102. }
  103. end
  104. def self.restricted_to
  105. :pm
  106. end
  107. end