app.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 = if model
  43. edit_app_request(author, model.name)
  44. else
  45. new_app_request(author, app_type)
  46. end
  47. # To be DM'd to the user
  48. form_embed = if model
  49. edit_app_form(author, model)
  50. else
  51. new_app_form(author, app_type)
  52. end
  53. phone = model ? [] : [Emoji::PHONE]
  54. [BotResponse.new(embed: request_embed),
  55. BotResponse.new(destination: author.dm, embed: form_embed, reactions: phone)]
  56. end
  57. def self.character_form(event, author, name=nil, status=nil)
  58. character = Character.restricted_find(name, author) if name
  59. case status
  60. # Move character to archives
  61. when /inactive/i, /archived?/i
  62. # Check if character is in team
  63. in_team = CharTeam.where(active: true, char_id: character.id)
  64. if in_team.empty?
  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. else
  71. teams = Team.where(id: in_team.map(&:team_id)).map(&:name)
  72. embed = team_alert(character, teams)
  73. BotResponse.new(embed: embed, reactions: Emoji::REQUEST)
  74. end
  75. # Move character to active
  76. when /active/i
  77. # Ensure character is not active or npc
  78. if character.active == 'Active' || character.active == 'NPC'
  79. return error_embed("#{character.name} is #{character.active}")
  80. end
  81. # Determine if the user has room for another active character
  82. # Add 2 if user is a nitro booster, 1 if not
  83. user = User.find(character.user_id)
  84. cap = user.allowed_chars(event.server.member(user.id.to_i))
  85. current = Character.where(user_id: user.id, active: 'Active').count
  86. # If they have at least 1 open slot
  87. if cap - current > 0
  88. # Submit reactivation application
  89. embed = character_embed(character: character, event: event)
  90. embed.author = { name: 'Reactivation Application' }
  91. [BotResponse.new(destination: ENV['APP_CH'], embed: embed, reactions: Emoji::APPLICATION),
  92. BotResponse.new(embed: success_embed("Successfully requested #{character.name} to be reactivated!"))]
  93. # If they have no open slots
  94. else
  95. # Error
  96. error_embed("You can't do that", 'You have too many active characters!')
  97. end
  98. # Delete* character -- not actually deleted
  99. when /deleted?/i
  100. # Check if character is in team
  101. in_team = CharTeam.where(active: true, char_id: character.id)
  102. if in_team
  103. # Fetch list of teams the character is in, and leave each one
  104. teams = Team.where(id: in_team.map(&:team_id))
  105. teams.each { |t| t.leave(character) }
  106. end
  107. # Update character
  108. character.update(active: 'Deleted')
  109. character.reload
  110. # Create alert message
  111. embed = Embed.new(
  112. title: "Deleted #{character.name}",
  113. description: 'This character can only be recovered by an admin'
  114. )
  115. # Reply
  116. [BotResponse.new(destination: ENV['APP_CH'], embed: embed),
  117. BotResponse.new(embed: embed)]
  118. when /legend/i, /legendary/i
  119. return command_error('Uknown Status', ApplicationCommand) unless Util::Roles.admin?(author)
  120. # Add the flag to the character
  121. character.update(special: 'legend')
  122. character.reload
  123. success_embed("Updated #{character.name} to be #{status}")
  124. when /guild/i, /employee/i
  125. return command_error('Uknown Status', ApplicationCommand) unless Util::Roles.admin?(author)
  126. # Add the flag to the character
  127. character.update(special: 'guild')
  128. character.reload
  129. success_embed("Updated #{character.name} to be #{status}")
  130. when /nil/i, /null/i, /none/i
  131. return command_error('Uknown Status', ApplicationCommand) unless Util::Roles.admin?(author)
  132. # Remove flag from the character
  133. character.update(special: nil)
  134. character.reload
  135. success_embed("Updated #{character.name} to be #{status}")
  136. # New or edit character form
  137. when nil
  138. send_forms(author, :character, character)
  139. # Uknown status
  140. else
  141. command_error('Uknown Status', ApplicationCommand)
  142. end
  143. end
  144. def self.example_command(_event=nil)
  145. case ['', 'type', 'name', 'flag'].sample
  146. when ''
  147. []
  148. when 'type'
  149. [%w[character landmark].sample]
  150. when 'name'
  151. case ['', 'character', 'landmark'].sample
  152. when 'landmark'
  153. ['landmark', Landmark.order('RANDOM()').first.name]
  154. when 'character'
  155. ['character',
  156. Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  157. else
  158. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name]
  159. end
  160. when 'flag'
  161. case ['', 'character'].sample
  162. when ''
  163. [Character.where.not(active: 'Deleted').order('RANDOM()').first.name,
  164. %w[active inactive archive delete].sample]
  165. when 'character'
  166. ['character',
  167. Character.where.not(active: 'Deleted').order('RANDOM()').first.name,
  168. %w[active inactive archive delete].sample]
  169. end
  170. end
  171. end
  172. def self.admin_opts
  173. {
  174. usage: {
  175. id: 'Character ID, name is functional if the character is yours',
  176. flag: 'Flags: Active, Archived, Deleted, Legend, Guild, None'
  177. }
  178. }
  179. end
  180. end