app.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. require './app/commands/base_command.rb'
  2. class ApplicationCommand < BaseCommand
  3. def self.opts
  4. {
  5. usage: {
  6. type: "Specifies which type of form you're looking for. Options include:" +
  7. " character, and landmark. If omitted, defaults to characters",
  8. name: "Searches for a character or landmark belonging to the user by name",
  9. flag: "If a character is being updated, they may be flagged as active, archived, or deleted"
  10. }
  11. }
  12. end
  13. def self.cmd
  14. desc = "Create new or edit your existing applications for characters " +
  15. "and landmarks!"
  16. @cmd ||= Command.new(:app, desc, opts) do |event, type, name, status|
  17. # Save the requester of the app
  18. author = event.author
  19. # Determine type, if specified
  20. case type
  21. # Landmark App
  22. when /landmarks?/i
  23. # Find the Landmark, if specified
  24. landmark = Landmark.restricted_find(name, author) if name
  25. # Create a request confirmation form
  26. send_forms(author, :landmark, landmark)
  27. when /characters?/i
  28. character_form(event, author, name, status)
  29. # Default to Character App, if a type isn't specified
  30. else
  31. character_form(event, author, type, name)
  32. end
  33. rescue ActiveRecord::RecordNotFound => e
  34. error_embed("Record not Found!", e.message)
  35. rescue StandardError => e
  36. error_embed(e.message)
  37. end
  38. end
  39. # -- Method to create replies for form requests
  40. def self.send_forms(author, app_type, model=nil)
  41. # To be shown as confirmation
  42. request_embed = model ?
  43. edit_app_request(author, model.name) :
  44. new_app_request(author, app_type)
  45. # To be DM'd to the user
  46. form_embed = model ?
  47. edit_app_form(author, model) :
  48. new_app_form(author, app_type)
  49. phone = model ? [] : [Emoji::PHONE]
  50. [ BotResponse.new(embed: request_embed),
  51. BotResponse.new(destination: author.dm, embed: form_embed, reactions: phone) ]
  52. end
  53. def self.character_form(event, author, name=nil, status=nil)
  54. character = Character.restricted_find(name, author) if name
  55. case status
  56. # Move character to archives
  57. when /inactive/i, /archived?/i
  58. # Check if character is in team
  59. in_team = CharTeam.where(active: true, char_id: character.id)
  60. if in_team
  61. teams = Team.where(id: in_team.map(&:team_id)).map(&:name)
  62. embed = team_alert(character, teams)
  63. BotResponse.new(embed: embed, reactions: Emoji::REQUEST)
  64. else
  65. character.update(active: 'Archived')
  66. character.reload
  67. embed = success_embed("Successfully archived #{character.name}")
  68. [ BotResponse.new(destination: ENV['APP_CH'], embed: embed),
  69. BotREsponse.new(embed: embed) ]
  70. end
  71. # Move character to active
  72. when /active/i
  73. # Ensure character is not active or npc
  74. if character.active == 'Active' || character.active == 'NPC'
  75. return error_embed("#{character.name} is #{character.active}")
  76. end
  77. # Determine if the user has room for another active character
  78. # Add 2 if user is a nitro booster, 1 if not
  79. user = User.find(character.user_id)
  80. cap = user.allowed_chars(event.server.member(user.id.to_i))
  81. current = Character.where(user_id: user.id, active: 'Active').count
  82. # If they have at least 1 open slot
  83. if cap - current > 0
  84. # Submit reactivation application
  85. embed = character_embed(character: character, event: event)
  86. embed.author = { name: "Reactivation Application" }
  87. [ BotResponse.new(destination: ENV['APP_CH'], embed: embed, reactions: Emoji::APPLICATION),
  88. BotResponse.new(embed: success_embed("Successfully requested #{character.name} to be reactivated!"))]
  89. # If they have no open slots
  90. else
  91. # Error
  92. error_embed("You can't do that", "You have too many active characters!")
  93. end
  94. # Delete* character -- not actually deleted
  95. when /deleted?/i
  96. # Check if character is in team
  97. in_team = CharTeam.where(active: true, char_id: character.id)
  98. if in_team
  99. # Fetch list of teams the character is in, and leave each one
  100. teams = Team.where(id: in_team.map(&:team_id))
  101. teams.each{ |t| t.leave(character) }
  102. end
  103. # Update character
  104. character.update(active: 'Deleted')
  105. character.reload
  106. # Create alert message
  107. embed = Embed.new(
  108. title: "Deleted #{character.name}",
  109. description: "This character can only be recovered by an admin"
  110. )
  111. # Reply
  112. [ BotResponse.new(destination: ENV['APP_CH'], embed: embed),
  113. BotREsponse.new(embed: embed) ]
  114. when /legend/i, /legendary/i
  115. return command_error("Uknown Status", ApplicationCommand) unless Util::Roles.admin?(author)
  116. # Add the flag to the character
  117. character.update(special: 'legend')
  118. character.reload
  119. success_embed("Updated #{character.name} to be #{status}")
  120. when /guild/i, /employee/i
  121. return command_error("Uknown Status", ApplicationCommand) unless Util::Roles.admin?(author)
  122. # Add the flag to the character
  123. character.update(special: 'guild')
  124. character.reload
  125. success_embed("Updated #{character.name} to be #{status}")
  126. when /nil/i, /null/i, /none/i
  127. return command_error("Uknown Status", ApplicationCommand) unless Util::Roles.admin?(author)
  128. # Remove flag from the character
  129. character.update(special: nil)
  130. character.reload
  131. success_embed("Updated #{character.name} to be #{status}")
  132. # New or edit character form
  133. when nil
  134. send_forms(author, :character, character)
  135. # Uknown status
  136. else
  137. command_error("Uknown Status", ApplicationCommand)
  138. end
  139. end
  140. def self.example_command(event=nil)
  141. case ['', 'type', 'name', 'flag'].sample
  142. when ''
  143. []
  144. when 'type'
  145. [['character', 'landmark'].sample]
  146. when 'name'
  147. case ['', 'character', 'landmark'].sample
  148. when 'landmark'
  149. ['landmark', Landmark.order('RANDOM()').first.name]
  150. when 'character'
  151. ['character',
  152. Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  153. else
  154. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  155. end
  156. when 'flag'
  157. case ['', 'character'].sample
  158. when ''
  159. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name,
  160. ['active', 'inactive', 'archive', 'delete'].sample]
  161. when 'character'
  162. ['character',
  163. Character.where.not(active: 'Deleted').order('RANDOM()').first.name,
  164. ['active', 'inactive', 'archive', 'delete'].sample]
  165. end
  166. end
  167. end
  168. def self.admin_opts
  169. {
  170. usage: {
  171. id: "Character ID, name is functional if the character is yours",
  172. flag: "Flags: Active, Archived, Deleted, Legend, Guild, None"
  173. }
  174. }
  175. end
  176. end