bot.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. ADMINS = 308250685554556930
  11. # ---
  12. Dotenv.load if BOT_ENV != 'production'
  13. db_yml = File.open('config/database.yml') do |erb|
  14. ERB.new(erb.read).result
  15. end
  16. db_config = YAML.safe_load(db_yml)[BOT_ENV]
  17. ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
  18. ActiveRecord::Base.establish_connection(
  19. adapter: 'postgresql',
  20. host: db_config.fetch('host') { 'localhost' },
  21. database: db_config['database'],
  22. user: db_config['user'],
  23. password: db_config['password']
  24. )
  25. Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
  26. Dir['/lib/*.rb'].each { |f| require f }
  27. token = ENV['DISCORD_BOT_TOKEN']
  28. bot = Discordrb::Bot.new(token: token)
  29. # Methods: define basic methods here
  30. # ---
  31. # Commands: structure basic bot commands here
  32. commands = []
  33. hello = Command.new(:hello, "Says hello!\nGreat for testing if the bot is responsive") do |event|
  34. user = event.author.nickname || event.author.name
  35. greetings = [
  36. "Hi there, #{user}",
  37. "Greetings #{user}, you lovable bum",
  38. "Alola, #{user}",
  39. "Hello, #{user}! The Guildmasters have been waiting",
  40. "#{user} would like to battle!"
  41. ]
  42. Embed.new(
  43. description: greetings.sample,
  44. color: event.author.color.combined,
  45. thumbnail: {
  46. url: Image::HAPPY
  47. }
  48. )
  49. end
  50. opts = { "" => "displays a list of all commands", "command" => "displays info and usage for specified command" }
  51. help = Command.new(:help, "Displays help information for the commands", opts) do |event, command|
  52. short = /pkmn-(\w+)/.match(command) if command
  53. cmd = short ? short[1] : command if command
  54. cmd = commands.detect { |c| c.name == cmd.to_sym } if cmd
  55. if command && cmd
  56. command_embed(cmd)
  57. elsif !command
  58. all_commands_embed(commands)
  59. else
  60. command_error_embed("Command not found!", help)
  61. end
  62. end
  63. opts = { "type" => "" }
  64. matchup = Command.new(:matchup, "Displays a chart of effectiveness for the given type", opts) do |event, type|
  65. channel = event.channel.id
  66. file = "images/Type #{type.capitalize}.png"
  67. if File.exists?(file)
  68. bot.send_file(channel, File.open(file, 'r'))
  69. else
  70. bot.respond("I do not know this pokemon type! Please try again!")
  71. end
  72. end
  73. opts = { "" => "starts a new app", "name" => "edits an existing app", "name | (in)active" => "sets app to active or inactive" }
  74. app = Command.new(:app, "Everything to do with character applications", opts) do |event, name, status|
  75. user = event.author
  76. user_name = user.nickname || user.name
  77. color = user.color ? user.color.combined : Color::DEFAULT
  78. character = Character.where(user_id: user.id).find_by(name: name) if name
  79. active = status.match(/(in)?active/i) if status
  80. if name && !character
  81. app_not_found_embed(user_name, name)
  82. elsif status && active && character
  83. character.update!(active: active[0].capitalize)
  84. character.reload
  85. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  86. elsif name && character && !status
  87. edit_url = Url::CHARACTER + character.edit_url
  88. embed = edit_app_dm(name, edit_url, color)
  89. bot.send_message(user.dm.id, "", false, embed)
  90. edit_app_embed(user_name, name, color)
  91. elsif !name && !status
  92. embed = new_app_dm(user_name, color, user.id)
  93. message = bot.send_message(user.dm.id, "", false, embed)
  94. message.react(Emoji::PHONE)
  95. new_app_embed(user_name, color)
  96. else
  97. command_error_embed("There was an error processing your application!", app)
  98. end
  99. end
  100. opts = { "question | option1, option2, etc" => "Creates a poll for the specified question with the given options"}
  101. poll = Command.new(:poll, "Creates a dynamic poll in any channel", opts) do |event, question, options|
  102. options_array = options.split(/\s?,\s?/) if options
  103. new_poll_embed(event, question, options_array) if options
  104. command_error_embed("There was an error creating your poll!", poll) unless question && options
  105. end
  106. opts = { "participants" => "May accept Everyone, Here, or a comma seperated list of names"}
  107. raffle = Command.new(:raffle, "Creates a raffle and picks a winner", opts) do |event, participant|
  108. participants =
  109. case participant
  110. when /^everyone$/i
  111. event.server.members
  112. when /^here$/i
  113. event.message.channel.users
  114. else
  115. participant.split(/\s?,\s?/)
  116. end
  117. winner = participants.sample
  118. winner_name =
  119. case winner
  120. when String
  121. winner
  122. else
  123. winner.nickname || winner.username
  124. end
  125. if winner_name
  126. new_generic_embed(event, "Raffle Results!", "Winner: " + winner_name)
  127. else
  128. command_error_embed("There was an error creating your raffle!", raffle)
  129. end
  130. end
  131. opts = { "name | key, words | (n)sfw | url" => "" }
  132. image = Command.new(:image, "Edit your character's images", opts) do |event, name, keyword, tag, url|
  133. character = Character.where(user_id: event.author.id).find_by!(name: name) if name
  134. category = /(n)?sfw/i.match(tag) if tag
  135. if character && category
  136. image = "_New Character Image_:\n\n>>> **Character**: #{character.name}\n**Species**: #{character.species}"
  137. image += "\n\n**Character ID**: #{character.id}\n**Keyword**: #{keyword}\n**Category**: #{tag}\n\n**URL**: #{url}"
  138. approval = bot.send_message(Channel::APPROVAL, image, false, nil)
  139. approval.react(Emoji::Y)
  140. approval.react(Emoji::N)
  141. end
  142. if approval
  143. success_embed("Your image has been submitted for approval!")
  144. else
  145. error_embed("Something went wrong!")
  146. end
  147. rescue ActiveRecord::RecordNotFound
  148. error_embed("Could not find your character name #{name}")
  149. end
  150. # ---
  151. commands = [
  152. hello,
  153. matchup,
  154. app,
  155. help,
  156. poll,
  157. raffle
  158. ]
  159. # This will trigger on every message sent in discord
  160. bot.message do |event|
  161. content = event.message.content
  162. author = event.author.id
  163. command = /^pkmn-(\w+)/.match(content)
  164. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  165. reply = cmd.call(content, event) if cmd
  166. case reply
  167. when Embed
  168. event.send_embed("", reply)
  169. when String
  170. event.respond(reply)
  171. end
  172. event.send_embed("", error_embed("Command not found!")) if command && !cmd && event.server
  173. Character.check_user(event) if author == Bot::CHARACTER
  174. end
  175. pm_commands = [ image ]
  176. # This will trigger when a dm is sent to the bot from a user
  177. bot.pm do |event|
  178. content = event.message.content
  179. command = /^pkmn-(\w+)/.match(content)
  180. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  181. reply = cmd.call(content, event) if cmd
  182. case reply
  183. when Embed
  184. event.send_embed("", reply)
  185. when String
  186. event.respond(reply)
  187. end
  188. end
  189. # This will trigger when any reaction is added in discord
  190. bot.reaction_add do |event|
  191. content = event.message.content
  192. reactions = event.message.reactions
  193. maj = event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2 if event.server
  194. maj = 1
  195. form =
  196. case
  197. when event.message.author.id == Bot::CHARACTER
  198. :character_application
  199. when event.message.from_bot? && content.match(Regex::CHAR_APP)
  200. :character_rejection
  201. when event.message.from_bot? && event.server.nil?
  202. :user_key
  203. when event.message.from_bot? && content.match(/\_New\sCharacter\sImage\_:/)
  204. :image_application
  205. end
  206. vote =
  207. case
  208. when reactions[Emoji::Y].present? && reactions[Emoji::Y].count > maj
  209. :yes
  210. when reactions[Emoji::N].present? && reactions[Emoji::N].count > maj
  211. :no
  212. when reactions[Emoji::CHECK].present? && reactions[Emoji::CHECK].count > 1
  213. :check
  214. when reactions[Emoji::CROSS].present? && reactions[Emoji::CROSS].count > 1
  215. :cross
  216. when reactions[Emoji::CRAYON].present? && reactions[Emoji::CRAYON].count > 1
  217. :crayon
  218. when reactions[Emoji::PHONE].present? && reactions[Emoji::PHONE].count > 1
  219. :phone
  220. end
  221. case [form, vote]
  222. when [:character_application, :yes]
  223. params = content.split("\n")
  224. uid = Regex::UID.match(content)
  225. member = event.server.member(uid[1])
  226. character = CharacterController.edit_character(params)
  227. image_url = ImageController.default_image(content, character.id)
  228. embed = character_embed(character, image_url, member) if character
  229. bot.send_message(
  230. Channel::CHARACTER,
  231. "Good news, <@#{member.id}>! Your character was approved",
  232. false,
  233. embed
  234. ) if embed
  235. event.message.delete if embed
  236. event.respond("", admin_error_embed("Something went wrong when saving application")) unless embed
  237. when [:character_application, :no]
  238. reject_app(event, reject_char_embed(content))
  239. when [:character_rejection, :check]
  240. member = event.server.member(Regex::UID.match(content)[1])
  241. embed = message_user_embed(event)
  242. event.message.delete
  243. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  244. bot.send_message(member.dm.id, "", false, embed)
  245. when [:character_rejection, :cross]
  246. event.message.delete
  247. when [:character_rejection, :crayon]
  248. event.message.delete
  249. bot.send_temporary_message(event.channel.id, "", 35, false, self_edit_embed(content))
  250. when [:user_key, :phone]
  251. event.message.delete_own_reaction(Emoji::PHONE)
  252. user = event.message.reacted_with(Emoji::PHONE).first
  253. bot.send_message(user.dm.id, user.id, false, nil)
  254. when [:image_application, :yes]
  255. params = content.split("\n")
  256. image = ImageController.edit_image(params)
  257. char = Character.find(image.char_id)
  258. user = event.server.member(char.user_id)
  259. embed = char_image_embed(char.name, image, user)
  260. event.message.delete if embed
  261. channel = image.category.upcase == 'SFW' ? Channel::CHARACTER : Channel::CHARACTER_NSFW
  262. bot.send_message(channel, "Image Approved!", false, embed)
  263. end
  264. end
  265. # This will trigger when any reaction is removed in discord
  266. bot.reaction_remove do |event|
  267. end
  268. # This will trigger when a member is updated
  269. bot.member_update do |event|
  270. end
  271. # This will trigger when anyone joins the server
  272. bot.member_join do |event|
  273. end
  274. # This will trigger when anyone leaves the server
  275. bot.member_leave do |event|
  276. end
  277. # This will trigger when anyone is banned from the server
  278. bot.user_ban do |event|
  279. end
  280. # This will trigger when anyone is un-banned from the server
  281. bot.user_unban do |event|
  282. end
  283. bot.run