bot.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. opts = { "question | option1, option2, etc" => "Creates a poll for the specified question with the given options"}
  108. poll = Command.new(:poll, "Creates a dynamic poll in any channel", opts) do |event, question, options|
  109. options_array = options.split(/\s?,\s?/) if options
  110. new_poll_embed(event, question, options_array) if options
  111. command_error_embed("There was an error creating your poll!", poll) unless question && options
  112. end
  113. opts = { "participants" => "May accept Everyone, Here, or a comma seperated list of names"}
  114. raffle = Command.new(:raffle, "Creates a raffle and picks a winner", opts) do |event, participant|
  115. user_channel = event.author.dm
  116. participants =
  117. case participant
  118. when /^everyone$/i
  119. event.server.members
  120. when /^here$/i
  121. event.message.channel.users
  122. else
  123. participant.split(/\s?,\s?/)
  124. end
  125. winner = participants.sample
  126. winner_name =
  127. case winner
  128. when String
  129. winner
  130. else
  131. winner.nickname || winner.username
  132. end
  133. if winner_name
  134. new_generic_embed(event, "Raffle Results!", "Winner: " + winner_name)
  135. else
  136. command_error_embed("There was an error creating your raffle!", raffle)
  137. end
  138. end
  139. # ---
  140. commands = [
  141. hello,
  142. matchup,
  143. app,
  144. help,
  145. poll,
  146. raffle
  147. ]
  148. # This will trigger on every message sent in discord
  149. bot.message do |event|
  150. content = event.message.content
  151. author = event.author.id
  152. command = /^pkmn-(\w+)/.match(content)
  153. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  154. reply = cmd.call(content, event) if cmd
  155. case reply
  156. when Embed
  157. event.send_embed("", reply)
  158. when String
  159. event.respond(reply)
  160. end
  161. event.send_embed("", error_embed("Command not found!")) if command && !cmd
  162. Character.check_user(event) if author == APP_BOT
  163. end
  164. # This will trigger when a dm is sent to the bot from a user
  165. bot.pm do |event|
  166. end
  167. # This will trigger when any reaction is added in discord
  168. bot.reaction_add do |event|
  169. content = event.message.content
  170. reactions = event.message.reactions
  171. maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  172. maj = 1
  173. form =
  174. case
  175. when event.message.author.id == APP_BOT
  176. :character_application
  177. when event.message.from_bot? && content.match(NEW_APP)
  178. :character_rejection
  179. end
  180. vote =
  181. case
  182. when reactions[Emoji::Y].present? && reactions[Emoji::Y].count > maj
  183. :yes
  184. when reactions[Emoji::N].present? && reactions[Emoji::N].count > maj
  185. :no
  186. when reactions[Emoji::CHECK].present? && reactions[Emoji::CHECK].count > 1
  187. :check
  188. when reactions[Emoji::CROSS].present? && reactions[Emoji::CROSS].count > 1
  189. :cross
  190. when reactions[Emoji::CRAYON].present? && reactions[Emoji::CRAYON].count > 1
  191. :crayon
  192. end
  193. case [form, vote]
  194. when [:character_application, :yes]
  195. params = content.split("\n")
  196. uid = UID.match(content)
  197. member = event.server.member(uid[1])
  198. character = CharacterController.edit_character(params)
  199. image_url = ImageController.edit_images(content, character.id)
  200. embed = character_embed(character, image_url, member) if character
  201. bot.send_message(
  202. CHAR_CHANNEL,
  203. "Good news, <@#{member.id}>! Your character was approved",
  204. false,
  205. embed
  206. ) if embed
  207. event.message.delete if embed
  208. event.respond("", admin_error_embed("Something went wrong when saving application")) unless embed
  209. when [:character_application, :no]
  210. reject_app(event, reject_char_embed(content))
  211. when [:character_rejection, :check]
  212. member = event.server.member(UID.match(content)[1])
  213. embed = message_user_embed(event)
  214. event.message.delete
  215. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  216. bot.send_message(member.dm.id, "", false, embed)
  217. when [:character_rejection, :cross]
  218. event.message.delete
  219. when [:character_rejection, :crayon]
  220. event.message.delete
  221. bot.send_temporary_message(event.channel.id, "", 35, false, self_edit_embed(content))
  222. end
  223. end
  224. # This will trigger when any reaction is removed in discord
  225. bot.reaction_remove do |event|
  226. end
  227. # This will trigger when a member is updated
  228. bot.member_update do |event|
  229. end
  230. # This will trigger when anyone joins the server
  231. bot.member_join do |event|
  232. end
  233. # This will trigger when anyone leaves the server
  234. bot.member_leave do |event|
  235. end
  236. # This will trigger when anyone is banned from the server
  237. bot.user_ban do |event|
  238. end
  239. # This will trigger when anyone is un-banned from the server
  240. bot.user_unban do |event|
  241. end
  242. bot.run