bot.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. require 'bundler'
  2. require 'erb'
  3. require 'yaml'
  4. require 'json'
  5. require 'terminal-table'
  6. BOT_ENV = ENV.fetch('BOT_ENV') { 'development' }
  7. Bundler.require(:default, BOT_ENV)
  8. require 'active_record'
  9. # Constants: such as roles and channel ids
  10. # Users
  11. APP_BOT = 627702340018896896
  12. # Roles
  13. ADMINS = 308250685554556930
  14. # Channels
  15. CHAR_CHANNEL = 594244240020865035
  16. # Images
  17. HAP_ROTOM = "https://static.pokemonpets.com/images/monsters-images-800-800/479-Rotom.png"
  18. # URLs
  19. APP_FORM = "https://docs.google.com/forms/d/e/1FAIpQLSfryXixX3aKBNQxZT8xOfWzuF02emkJbqJ1mbMGxZkwCvsjyA/viewform"
  20. # Regexes
  21. UID = /<@([0-9]+)>/
  22. EDIT_URL = /Edit\sKey\s\(ignore\):\s([\s\S]*)/
  23. NEW_APP = /\_New\sCharacter\sApplication\_:\s(.*)/
  24. # ---
  25. Dotenv.load if BOT_ENV != 'production'
  26. db_yml = File.open('config/database.yml') do |erb|
  27. ERB.new(erb.read).result
  28. end
  29. db_config = YAML.safe_load(db_yml)[BOT_ENV]
  30. ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
  31. ActiveRecord::Base.establish_connection(
  32. adapter: 'postgresql',
  33. host: db_config.fetch('host') { 'localhost' },
  34. database: db_config['database'],
  35. user: db_config['user'],
  36. password: db_config['password']
  37. )
  38. Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
  39. Dir['/lib/*.rb'].each { |f| require f }
  40. token = ENV['DISCORD_BOT_TOKEN']
  41. bot = Discordrb::Bot.new(token: token)
  42. # Methods: define basic methods here
  43. # ---
  44. # Commands: structure basic bot commands here
  45. commands = []
  46. hello = Command.new(:hello, "Says hello!\nGreat for testing if the bot is responsive") do |event|
  47. user = event.author.nickname || event.author.name
  48. greetings = [
  49. "Hi there, #{user}",
  50. "Greetings #{user}, you lovable bum",
  51. "Alola, #{user}",
  52. "Hello, #{user}! The Guildmasters have been waiting",
  53. "#{user} would like to battle!"
  54. ]
  55. Embed.new(
  56. description: greetings.sample,
  57. color: event.author.color.combined,
  58. thumbnail: {
  59. url: HAP_ROTOM
  60. }
  61. )
  62. end
  63. opts = { "" => "displays a list of all commands", "command" => "displays info and usage for specified command" }
  64. help = Command.new(:help, "Displays help information for the commands", opts) do |event, command|
  65. short = /pkmn-(\w+)/.match(command) if command
  66. cmd = short ? short[1] : command if command
  67. cmd = commands.detect { |c| c.name == cmd.to_sym } if cmd
  68. if command && cmd
  69. command_embed(cmd)
  70. elsif !command
  71. all_commands_embed(commands)
  72. else
  73. command_error_embed("Command not found!", help)
  74. end
  75. end
  76. opts = { "type" => "" }
  77. matchup = Command.new(:matchup, "Displays a chart of effectiveness for the given type", opts) do |event, type|
  78. channel = event.channel.id
  79. file = "images/Type #{type.capitalize}.png"
  80. if File.exists?(file)
  81. bot.send_file(channel, File.open(file, 'r'))
  82. else
  83. bot.respond("I do not know this pokemon type! Please try again!")
  84. end
  85. end
  86. opts = { "" => "starts a new app", "name" => "edits an existing app", "name | (in)active" => "sets app to active or inactive" }
  87. app = Command.new(:app, "Everything to do with character applications", opts) do |event, name, status|
  88. user = event.author
  89. user_channel = event.author.dm
  90. character = Character.where(user_id: user.id).find_by(name: name) if name
  91. active = status.match(/(in)?active/i) if status
  92. if status && active && character
  93. character.update!(active: active[0].capitalize)
  94. character.reload
  95. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  96. elsif name && character && !status
  97. edit_url = APP_FORM + character.edit_url
  98. embed = edit_app_embed(event, edit_url, name)
  99. bot.send_message(user_channel.id, "", false, embed)
  100. elsif !name && !status
  101. embed = new_app_embed(event, name)
  102. bot.send_message(user_channel.id, "", false, embed)
  103. else
  104. command_error_embed("There was an error processing your application!", app)
  105. end
  106. end
  107. # ---
  108. commands = [
  109. hello,
  110. matchup,
  111. app,
  112. help
  113. ]
  114. # This will trigger on every message sent in discord
  115. bot.message do |event|
  116. content = event.message.content
  117. author = event.author.id
  118. command = /^pkmn-(\w+)/.match(content)
  119. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  120. reply = cmd.call(content, event) if cmd
  121. case reply
  122. when Embed
  123. event.send_embed("", reply)
  124. when String
  125. event.respond(reply)
  126. end
  127. event.send_embed("", error_embed("Command not found!")) if command && !cmd
  128. Character.check_user(event) if author == APP_BOT
  129. end
  130. # This will trigger when a dm is sent to the bot from a user
  131. bot.pm do |event|
  132. end
  133. # This will trigger when any reaction is added in discord
  134. bot.reaction_add do |event|
  135. content = event.message.content
  136. reactions = event.message.reactions
  137. maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  138. maj = 1
  139. form =
  140. case
  141. when event.message.author.id == APP_BOT
  142. :character_application
  143. when event.message.from_bot? && content.match(NEW_APP)
  144. :character_rejection
  145. end
  146. vote =
  147. case
  148. when reactions[Emoji::Y].present? && reactions[Emoji::Y].count > maj
  149. :yes
  150. when reactions[Emoji::N].present? && reactions[Emoji::N].count > maj
  151. :no
  152. when reactions[Emoji::CHECK].present? && reactions[Emoji::CHECK].count > 1
  153. :check
  154. when reactions[Emoji::CROSS].present? && reactions[Emoji::CROSS].count > 1
  155. :cross
  156. when reactions[Emoji::CRAYON].present? && reactions[Emoji::CRAYON].count > 1
  157. :crayon
  158. end
  159. case [form, vote]
  160. when [:character_application, :yes]
  161. params = content.split("\n")
  162. uid = UID.match(content)
  163. member = event.server.member(uid[1])
  164. character = CharacterController.edit_character(params)
  165. image_url = ImageController.edit_images(content, character.id)
  166. embed = character_embed(character, image_url, member) if character
  167. bot.send_message(
  168. CHAR_CHANNEL,
  169. "Good news, <@#{member.id}>! Your character was approved",
  170. false,
  171. embed
  172. ) if embed
  173. event.message.delete if embed
  174. event.respond("", admin_error_embed("Something went wrong when saving application")) unless embed
  175. when [:character_application, :no]
  176. reject_app(event, reject_char_embed(content))
  177. when [:character_rejection, :check]
  178. member = event.server.member(UID.match(content)[1])
  179. embed = message_user_embed(event)
  180. event.message.delete
  181. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  182. bot.send_message(member.dm.id, "", false, embed)
  183. when [:character_rejection, :cross]
  184. event.message.delete
  185. when [:character_rejection, :crayon]
  186. event.message.delete
  187. bot.send_temporary_message(event.channel.id, "", 35, false, self_edit_embed(content))
  188. end
  189. end
  190. # This will trigger when any reaction is removed in discord
  191. bot.reaction_remove do |event|
  192. end
  193. # This will trigger when a member is updated
  194. bot.member_update do |event|
  195. end
  196. # This will trigger when anyone joins the server
  197. bot.member_join do |event|
  198. end
  199. # This will trigger when anyone leaves the server
  200. bot.member_leave do |event|
  201. end
  202. # This will trigger when anyone is banned from the server
  203. bot.user_ban do |event|
  204. end
  205. # This will trigger when anyone is un-banned from the server
  206. bot.user_unban do |event|
  207. end
  208. bot.run