team.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. require './app/commands/base_command.rb'
  2. class TeamCommand < BaseCommand
  3. def self.opts
  4. {
  5. usage: {
  6. team: "Searches for a team by it's name. If no team is specified, " +
  7. "R0ry will display a list of all teams.",
  8. action: "Accepts `create` to create a new team, " +
  9. "`update` inside a team's private channel to update team information, " +
  10. "`apply` to request for a character to join the team, and " +
  11. "`leave` to have a character leave the team",
  12. argument: "If creating or updating a team, this should be the description" +
  13. " for the team. If applying for or leaving a team, this should be your " +
  14. "character's name"
  15. }
  16. }
  17. end
  18. def self.cmd
  19. desc = "Rescue Teams: view, create, update, join or leave teams"
  20. @cmd ||= Command.new(:team, desc, opts) do |event, team_name, action, desc|
  21. # Save author
  22. author = event.author
  23. # --Execute--
  24. case action
  25. # New Team Request
  26. when /create/i
  27. raise 'Team Already Exists' if Team.find_by(name: team_name)
  28. reply = []
  29. # Request Team to Admin
  30. reply.push(BotResponse.new(
  31. destination: ENV['APP_CH'],
  32. embed: create_request(team_name, desc),
  33. reactions: Emoji::REQUEST
  34. ))
  35. # Sucess message to submitter
  36. reply.push(BotResponse.new(
  37. embed: success_embed("Your request for a new team has been submitted for approval!")
  38. ))
  39. # Existing Team Update
  40. when /update/i
  41. # Find appropriate team
  42. team = Team.find_by!(channel: event.channel.id)
  43. # Update team with new info
  44. team.update(name: team_name, description: desc)
  45. team.reload
  46. # Reply with new team info
  47. team_embed(team)
  48. # Team Join Request
  49. when /apply/i
  50. # Find appropriate team
  51. team = Team.find_by!('name ilike ?', team_name)
  52. # Find character, check if eligable
  53. char = Character.restricted_find(desc, author, ['Archived'])
  54. in_team = CharTeam.where(char_id: char.id).find_by(active: true)
  55. # Ensure if user is eligable
  56. if User.find(event.author.id).level < 5
  57. error_embed("You are not high enough level!")
  58. # Ensure team has open slots
  59. elsif CharTeam.where(team_id: team.id, active: true).count >= 6
  60. error_embed("#{team.name} is full!")
  61. # Ensure character is not in team or an admin
  62. elsif in_team && !Util::Roles.admin?(author)
  63. error_embed("#{char.name} is already in a team!")
  64. else
  65. # Character's creator [discord user]
  66. member = event.server.member(char.user_id.to_i)
  67. reply = []
  68. # Request message to team
  69. reply.push(BotResponse.new(
  70. destination: team.channel.to_i,
  71. embed: join_request(char, member),
  72. reactions: Emoji::REQUEST
  73. ))
  74. # Success message to sumbitter
  75. reply.push(BotResponse.new(
  76. embed: success_embed("Your request was sent to #{team.name}!")
  77. ))
  78. reply
  79. end
  80. # Member Leave Team
  81. when /leave/i
  82. # Find appropriate team
  83. team = Team.find_by!('name ilike ?', team_name)
  84. # Find character
  85. char = Character.restricted_find(desc, author, ['Archived'])
  86. # Remove character from team
  87. # Response indicates if user needs a role removed
  88. case team.leave(char)
  89. when Embed
  90. error_embed("Character not in Team!")
  91. when true
  92. user = event.server.member(char.user_id.to_i)
  93. user.remove_role(team.role.to_i)
  94. # Reply to alert team of member leave
  95. BotResponse.new(
  96. destination: team.channel.to_i,
  97. text: "#{char.name} has left the team"
  98. )
  99. when false
  100. # Reply to alert team of member leave
  101. BotResponse.new(
  102. destination: team.channel.to_i,
  103. text: "#{char.name} has left the team"
  104. )
  105. else
  106. error_embed("Something Went Wrong!")
  107. end
  108. # Disband Team
  109. when /archive/i, /disband/i
  110. # Find the team
  111. team = Team.find_by!(channel: event.channel.id)
  112. # Update team to inactive
  113. CharTeam.where(team_id: team.id).update(active: false)
  114. team.update(active: false)
  115. # Delete Role and archive channel
  116. event.server.role(team.role).delete('Archived Team')
  117. event.channel.category=(ENV['TEAM_ARCHIVES'])
  118. success_embed("Successfully Archived #{team.name}")
  119. # Display Team Info
  120. when nil
  121. team = Team.find_by!('name ilike ?', team_name) if team_name
  122. team_name ? team_embed(team) : teams_embed
  123. else
  124. command_error_embed("Could not process team request!", team)
  125. end
  126. rescue ActiveRecord::RecordNotFound => e
  127. error_embed("Record Not Found!", e.message)
  128. rescue StandardError => e
  129. error_embed(e.message)
  130. end
  131. end
  132. def self.example_command(event=nil)
  133. sample_team = [
  134. [ 'Hotel California', "You can checkout any time you like, but you can never leave~" ],
  135. [ 'The Jungle Team', "We've got fun and games. We've got everything you want!" ],
  136. [ 'Team Sudden Stop', "We are a great team full of many young adventures just looking for a g-" ],
  137. [ 'Little Nightmares', "Come play with us! *Forever, and ever and e v e r . . ." ],
  138. [ 'Puppo Pals', "Bork bork! Woof?" ]
  139. ].sample
  140. case ['', 'team', 'create', 'update', 'apply', 'leave'].sample
  141. when ''
  142. []
  143. when 'team'
  144. [Team.where(active: true).order('RANDOM()').first.name]
  145. when 'create'
  146. [sample_team[0], 'create', sample_team[1]]
  147. when 'update'
  148. [sample_team[0], 'update', sample_team[1]]
  149. when 'apply'
  150. [Team.where(active: true).order('RANDOM()').first.name,
  151. 'apply',
  152. Character.where(active: 'Active').order('RANDOM()').first.name]
  153. when 'leave'
  154. [Team.where(active: true).order('RANDOM()').first.name,
  155. 'leave',
  156. Character.where(active: 'Active').order('RANDOM()').first.name]
  157. end
  158. end
  159. def self.admin_opts
  160. {
  161. usage: {
  162. team: 'Team name, irrelevant, but must be done in team chat',
  163. action: 'Archive/Disband'
  164. }
  165. }
  166. end
  167. end