bot.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. # ---
  24. Dotenv.load if BOT_ENV != 'production'
  25. db_yml = File.open('config/database.yml') do |erb|
  26. ERB.new(erb.read).result
  27. end
  28. db_config = YAML.safe_load(db_yml)[BOT_ENV]
  29. ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
  30. ActiveRecord::Base.establish_connection(
  31. adapter: 'postgresql',
  32. host: db_config.fetch('host') { 'localhost' },
  33. database: db_config['database'],
  34. user: db_config['user'],
  35. password: db_config['password']
  36. )
  37. Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
  38. Dir['/lib/*.rb'].each { |f| require f }
  39. token = ENV['DISCORD_BOT_TOKEN']
  40. bot = Discordrb::Bot.new(token: token)
  41. # Methods: define basic methods here
  42. # ---
  43. # Commands: structure basic bot commands here
  44. hello = Command.new(:hello) do |event|
  45. user = event.author.nickname || event.author.name
  46. greetings = [
  47. "Hi there, #{user}",
  48. "Greetings #{user}, you lovable bum",
  49. "Alola, #{user}",
  50. "Hello, #{user}! The Guildmasters have been waiting",
  51. "#{user} would like to battle!"
  52. ]
  53. Embed.new(
  54. description: greetings.sample,
  55. color: event.author.color.combined,
  56. thumbnail: {
  57. url: HAP_ROTOM
  58. }
  59. )
  60. end
  61. matchup = Command.new(:matchup) do |event, type|
  62. channel = event.channel.id
  63. file = "images/Type #{type.capitalize}.png"
  64. if File.exists?(file)
  65. bot.send_file(channel, File.open(file, 'r'))
  66. else
  67. bot.respond("I do not know this pokemon type! Please try again!")
  68. end
  69. end
  70. app = Command.new(:app) do |event, name|
  71. user = event.author
  72. user_channel = event.author.dm
  73. if name
  74. if character = Character.where(user_id: user.id).find_by(name: name)
  75. edit_url = APP_FORM + character.edit_url
  76. embed = edit_app_embed(event, edit_url, name)
  77. bot.send_message(user_channel.id, "", false, embed)
  78. else
  79. app_not_found_embed(event, name)
  80. end
  81. else
  82. embed = new_app_embed(event)
  83. bot.send_message(user_channel.id, "", false, embed)
  84. end
  85. end
  86. poll = Command.new(:poll) do |event, options|
  87. new_poll_embed(event, options)
  88. end
  89. # ---
  90. commands = [
  91. hello,
  92. matchup,
  93. app,
  94. poll
  95. ]
  96. # This will trigger on every message sent in discord
  97. bot.message do |event|
  98. content = event.message.content
  99. if (match = /^pkmn-(\w+)/.match(content))
  100. command = match[1]
  101. if cmd = commands.detect { |c| c.name == command.to_sym }
  102. reply = cmd.call(content, event)
  103. if reply.is_a? Embed
  104. event.send_embed("", reply)
  105. elsif reply
  106. event.respond(reply)
  107. else
  108. event.respond("Something went wrong!")
  109. end
  110. end
  111. end
  112. if event.author.id == APP_BOT
  113. Character.check_user(event)
  114. end
  115. end
  116. # This will trigger when a dm is sent to the bot from a user
  117. bot.pm do |event|
  118. end
  119. # This will trigger when any reaction is added in discord
  120. bot.reaction_add do |event|
  121. content = event.message.content
  122. if event.message.author.id == APP_BOT
  123. maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  124. maj = 1
  125. if event.message.reacted_with(Emoji::Y).count > maj
  126. params = content.split("\n")
  127. uid = UID.match(content)
  128. member = event.server.member(uid[1])
  129. character = CharacterController.edit_character(params)
  130. image_url = ImageController.edit_images(content, character.id)
  131. embed = character_embed(character, image_url, member)
  132. if embed
  133. event.message.delete
  134. bot.send_message(
  135. CHAR_CHANNEL,
  136. "Character Approved!",
  137. false,
  138. embed
  139. )
  140. else
  141. event.respond("Something went wrong")
  142. end
  143. elsif event.message.reacted_with(Emoji::N).count > maj
  144. embed = reject_char_embed(content)
  145. reject_app(event, embed)
  146. end
  147. end
  148. if event.message.from_bot? && content.match(/\_New\sCharacter\sApplication\_/)
  149. if event.message.reacted_with(Emoji::CHECK).count > 1
  150. user_id = UID.match(content)
  151. member = event.server.member(user_id[1])
  152. embed = message_user_embed(event)
  153. event.message.delete
  154. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  155. user_channel = member.dm
  156. bot.send_message(user_channel.id, "", false, embed)
  157. elsif event.message.reacted_with(Emoji::CROSS).count > 1
  158. event.message.delete
  159. elsif event.message.reacted_with(Emoji::CRAYON).count > 1
  160. embed = self_edit_embed(content)
  161. event.message.delete
  162. bot.send_temporary_message(event.channel.id, "", 35, false, embed)
  163. end
  164. end
  165. end
  166. # This will trigger when any reaction is removed in discord
  167. bot.reaction_remove do |event|
  168. end
  169. # This will trigger when a member is updated
  170. bot.member_update do |event|
  171. end
  172. # This will trigger when anyone joins the server
  173. bot.member_join do |event|
  174. end
  175. # This will trigger when anyone leaves the server
  176. bot.member_leave do |event|
  177. end
  178. # This will trigger when anyone is banned from the server
  179. bot.user_ban do |event|
  180. end
  181. # This will trigger when anyone is un-banned from the server
  182. bot.user_unban do |event|
  183. end
  184. bot.run