bot.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. command_list = []
  44. # Commands: structure basic bot commands here
  45. command_list.push(["pkmn-hello","a simple test command to make the bot say hi."])
  46. hello = Command.new(:hello) 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. help = Command.new(:help) do |event,extra|
  64. user = event.author.nickname || event.author.name
  65. fields = []
  66. command_list.each do |item|
  67. fields.push({name: item[0], value: item[1]})
  68. end
  69. Embed.new(
  70. color: "#73FE49",
  71. title: "List of Character Commands",
  72. description: "Basic list of commands and what they do!",
  73. fields: fields
  74. )
  75. end
  76. command_list.push(["pkmn-matchup <type>","Shows the types that are strong and weak to the given type."])
  77. matchup = Command.new(:matchup) 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. command_list.insert(0,["pkmn-app <name>","Starts the process for a new character appication or to edit an existing one. Dont worry. Any other commands needed for this will be listed in the responces!"])
  87. app = Command.new(:app) do |event, name|
  88. user = event.author
  89. user_channel = event.author.dm
  90. if name
  91. if character = Character.where(user_id: user.id).find_by(name: name)
  92. edit_url = APP_FORM + character.edit_url
  93. embed = edit_app_embed(event, edit_url, name)
  94. bot.send_message(user_channel.id, "", false, embed)
  95. else
  96. app_not_found_embed(event, name)
  97. end
  98. else
  99. embed = new_app_embed(event)
  100. bot.send_message(user_channel.id, "", false, embed)
  101. end
  102. end
  103. # ---
  104. commands = [
  105. app,
  106. hello,
  107. help,
  108. matchup
  109. ]
  110. # This will trigger on every message sent in discord
  111. bot.message do |event|
  112. content = event.message.content
  113. if (match = /^pkmn-(\w+)/.match(content))
  114. command = match[1]
  115. if cmd = commands.detect { |c| c.name == command.to_sym }
  116. reply = cmd.call(content, event)
  117. if reply.is_a? Embed
  118. event.send_embed("", reply)
  119. elsif reply
  120. event.respond(reply)
  121. else
  122. event.respond("Something went wrong!")
  123. end
  124. end
  125. end
  126. if event.author.id == APP_BOT
  127. Character.check_user(event)
  128. end
  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. if event.message.author.id == APP_BOT
  137. maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  138. maj = 1
  139. if event.message.reacted_with(Emoji::Y).count > maj
  140. params = content.split("\n")
  141. uid = UID.match(content)
  142. member = event.server.member(uid[1])
  143. character = CharacterController.edit_character(params)
  144. image_url = ImageController.edit_images(content, character.id)
  145. embed = character_embed(character, image_url, member)
  146. if embed
  147. event.message.delete
  148. bot.send_message(
  149. CHAR_CHANNEL,
  150. "Character Approved!",
  151. false,
  152. embed
  153. )
  154. else
  155. event.respond("Something went wrong")
  156. end
  157. elsif event.message.reacted_with(Emoji::N).count > maj
  158. embed = reject_char_embed(content)
  159. reject_app(event, embed)
  160. end
  161. end
  162. if event.message.from_bot? && content.match(/\_New\sCharacter\sApplication\_/)
  163. if event.message.reacted_with(Emoji::CHECK).count > 1
  164. user_id = UID.match(content)
  165. member = event.server.member(user_id[1])
  166. embed = message_user_embed(event)
  167. event.message.delete
  168. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  169. user_channel = member.dm
  170. bot.send_message(user_channel.id, "", false, embed)
  171. elsif event.message.reacted_with(Emoji::CROSS).count > 1
  172. event.message.delete
  173. elsif event.message.reacted_with(Emoji::CRAYON).count > 1
  174. embed = self_edit_embed(content)
  175. event.message.delete
  176. bot.send_temporary_message(event.channel.id, "", 35, false, embed)
  177. end
  178. end
  179. end
  180. # This will trigger when any reaction is removed in discord
  181. bot.reaction_remove do |event|
  182. end
  183. # This will trigger when a member is updated
  184. bot.member_update do |event|
  185. end
  186. # This will trigger when anyone joins the server
  187. bot.member_join do |event|
  188. end
  189. # This will trigger when anyone leaves the server
  190. bot.member_leave do |event|
  191. end
  192. # This will trigger when anyone is banned from the server
  193. bot.user_ban do |event|
  194. end
  195. # This will trigger when anyone is un-banned from the server
  196. bot.user_unban do |event|
  197. end
  198. bot.run