bot.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. # ---
  87. commands = [
  88. hello,
  89. matchup,
  90. app
  91. ]
  92. # This will trigger on every message sent in discord
  93. bot.message do |event|
  94. content = event.message.content
  95. if (match = /^pkmn-(\w+)/.match(content))
  96. command = match[1]
  97. if cmd = commands.detect { |c| c.name == command.to_sym }
  98. reply = cmd.call(content, event)
  99. if reply.is_a? Embed
  100. event.send_embed("", reply)
  101. elsif reply
  102. event.respond(reply)
  103. else
  104. event.respond("Something went wrong!")
  105. end
  106. end
  107. end
  108. if event.author.id == APP_BOT
  109. Character.check_user(event)
  110. end
  111. end
  112. # This will trigger when a dm is sent to the bot from a user
  113. bot.pm do |event|
  114. end
  115. # This will trigger when any reaction is added in discord
  116. bot.reaction_add do |event|
  117. content = event.message.content
  118. if event.message.author.id == APP_BOT
  119. maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  120. maj = 1
  121. if event.message.reacted_with(Emoji::Y).count > maj
  122. params = content.split("\n")
  123. uid = UID.match(content)
  124. member = event.server.member(uid[1])
  125. character = CharacterController.edit_character(params)
  126. image_url = ImageController.edit_images(content, character.id)
  127. embed = character_embed(character, image_url, member)
  128. if embed
  129. event.message.delete
  130. bot.send_message(
  131. CHAR_CHANNEL,
  132. "Character Approved!",
  133. false,
  134. embed
  135. )
  136. else
  137. event.respond("Something went wrong")
  138. end
  139. elsif event.message.reacted_with(Emoji::N).count > maj
  140. embed = reject_char_embed(content)
  141. reject_app(event, embed)
  142. end
  143. end
  144. if event.message.from_bot? && content.match(/\_New\sCharacter\sApplication\_/)
  145. if event.message.reacted_with(Emoji::CHECK).count > 1
  146. user_id = UID.match(content)
  147. member = event.server.member(user_id[1])
  148. embed = message_user_embed(event)
  149. event.message.delete
  150. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  151. user_channel = member.dm
  152. bot.send_message(user_channel.id, "", false, embed)
  153. elsif event.message.reacted_with(Emoji::CROSS).count > 1
  154. event.message.delete
  155. elsif event.message.reacted_with(Emoji::CRAYON).count > 1
  156. embed = self_edit_embed(content)
  157. event.message.delete
  158. bot.send_temporary_message(event.channel.id, "", 35, false, embed)
  159. end
  160. end
  161. end
  162. # This will trigger when any reaction is removed in discord
  163. bot.reaction_remove do |event|
  164. end
  165. # This will trigger when a member is updated
  166. bot.member_update do |event|
  167. end
  168. # This will trigger when anyone joins the server
  169. bot.member_join do |event|
  170. end
  171. # This will trigger when anyone leaves the server
  172. bot.member_leave do |event|
  173. end
  174. # This will trigger when anyone is banned from the server
  175. bot.user_ban do |event|
  176. end
  177. # This will trigger when anyone is un-banned from the server
  178. bot.user_unban do |event|
  179. end
  180. bot.run