bot.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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!") 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 = {
  51. "" => "displays a list of all commands",
  52. "command" => "displays info and usage for specified command"
  53. }
  54. desc = "Displays help information for the commands"
  55. help = Command.new(:help, desc, opts) do |event, command|
  56. if command
  57. short = /pkmn-(\w+)/.match(command)
  58. command = short ? short[1] : command
  59. cmd = commands.detect { |c| c.name == command.to_sym }
  60. end
  61. if command && cmd
  62. command_embed(cmd)
  63. elsif !command
  64. all_commands_embed(commands)
  65. else
  66. command_error_embed("Command not found!", help)
  67. end
  68. end
  69. opts = { "type" => "" }
  70. desc = "Displays a chart of effectiveness for the given type"
  71. matchup = Command.new(:matchup, desc, opts) do |event, type|
  72. channel = event.channel.id
  73. file = "images/Type #{type.capitalize}.png"
  74. if File.exists?(file)
  75. bot.send_file(channel, File.open(file, 'r'))
  76. else
  77. bot.respond("I do not know this pokemon type! Please try again!")
  78. end
  79. end
  80. opts = {
  81. "" => "starts a new app",
  82. "name" => "edits an existing app",
  83. "name | (in)active" => "sets app to active or inactive"
  84. }
  85. desc = "Everything to do with character applications"
  86. app = Command.new(:app, desc, opts) do |event, name, status|
  87. user = event.author
  88. user_name = user.nickname || user.name
  89. color = user.color ? user.color.combined : Color::DEFAULT
  90. character = Character.where(user_id: user.id).find_by(name: name) if name
  91. active = status.match(/(in)?active/i) if status
  92. case
  93. when name && !character
  94. app_not_found_embed(user_name, name)
  95. when status && active && character
  96. character.update!(active: active[0].capitalize)
  97. character.reload
  98. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  99. when name && character && !status
  100. edit_url = Url::CHARACTER + character.edit_url
  101. embed = edit_app_dm(name, edit_url, color)
  102. bot.send_message(user.dm.id, "", false, embed)
  103. edit_app_embed(user_name, name, color)
  104. when !name && !status
  105. embed = new_app_dm(user_name, color, user.id)
  106. message = bot.send_message(user.dm.id, "", false, embed)
  107. message.react(Emoji::PHONE)
  108. new_app_embed(user_name, color)
  109. else
  110. command_error_embed("There was an error processing your application!", app)
  111. end
  112. end
  113. opts = { "question | option1, option2, etc" => ""}
  114. desc = "Creates a dynamic poll in any channel"
  115. poll = Command.new(:poll, desc, opts) do |event, question, options|
  116. if options
  117. options_array = options.split(/\s?,\s?/)
  118. new_poll_embed(event, question, options_array)
  119. else
  120. command_error_embed("There was an error creating your poll!", poll)
  121. end
  122. end
  123. opts = { "participants" => "May accept Everyone, Here, or a comma seperated list of names"}
  124. raffle = Command.new(:raffle, "Creates a raffle and picks a winner", opts) do |event, participant|
  125. participants =
  126. case participant
  127. when /^everyone$/i
  128. event.server.members
  129. when /^here$/i
  130. event.message.channel.users
  131. else
  132. participant.split(/\s?,\s?/)
  133. end
  134. winner = participants.sample
  135. winner_name =
  136. case winner
  137. when String
  138. winner
  139. else
  140. winner.nickname || winner.username
  141. end
  142. if winner_name
  143. new_generic_embed(event, "Raffle Results!", "Winner: " + winner_name)
  144. else
  145. command_error_embed("There was an error creating your raffle!", raffle)
  146. end
  147. end
  148. opts = {
  149. "name | keyword | (n)sfw | url" => "add or update image",
  150. "name | keyword | delete" => "remove image",
  151. "name | keyword" => "display image",
  152. "name" => "list all images"
  153. }
  154. desc = "Edit your characters' images"
  155. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
  156. user = event.author
  157. char = Character.where(user_id: user.id).find_by!(name: name) if name
  158. color = CharacterController.type_color(char) if char
  159. img = CharImage.find_by(keyword: keyword) if keyword
  160. case
  161. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  162. img_app = CharImage.to_form(char.name, char.species, char.id, keyword, tag, url)
  163. approval = bot.send_message(Channel::APPROVAL, img_app, false, nil)
  164. approval.react(Emoji::Y)
  165. approval.react(Emoji::N)
  166. success_embed("Your image has been submitted for approval!")
  167. when char && img && tag && tag.match(/delete/i)
  168. CharImage.find(img.id).delete
  169. success_embed("Removed image: #{keyword}")
  170. when char && img && !tag
  171. char_image_embed(char, img, user, color)
  172. when char && !keyword
  173. imgs = CharImage.where(char_id: char.id)
  174. image_list_embed(char, imgs, user, color)
  175. when keyword && !img
  176. error_embed("Could not find your image!")
  177. else
  178. command_error_embed("Could not process your image request!", image)
  179. end
  180. rescue ActiveRecord::RecordNotFound
  181. error_embed("Could not find your character name #{name}")
  182. end
  183. # ---
  184. commands = [
  185. hello,
  186. matchup,
  187. app,
  188. help,
  189. poll,
  190. raffle
  191. ]
  192. # This will trigger on every message sent in discord
  193. bot.message do |event|
  194. content = event.message.content
  195. author = event.author.id
  196. command = /^pkmn-(\w+)/.match(content)
  197. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  198. reply = cmd.call(content, event) if cmd
  199. case reply
  200. when Embed
  201. event.send_embed("", reply)
  202. when String
  203. event.respond(reply)
  204. end
  205. event.send_embed(
  206. "",
  207. error_embed("Command not found!")
  208. )if command && !cmd && event.server
  209. Character.check_user(event) if author == Bot::CHARACTER
  210. end
  211. pm_commands = [ image ]
  212. # This will trigger when a dm is sent to the bot from a user
  213. bot.pm do |event|
  214. content = event.message.content
  215. command = /^pkmn-(\w+)/.match(content)
  216. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  217. reply = cmd.call(content, event) if cmd
  218. case reply
  219. when Embed
  220. event.send_embed("", reply)
  221. when String
  222. event.respond(reply)
  223. end
  224. end
  225. # This will trigger when any reaction is added in discord
  226. bot.reaction_add do |event|
  227. content = event.message.content
  228. reactions = event.message.reactions
  229. maj = if event.server
  230. event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  231. end
  232. maj = 1
  233. form =
  234. case
  235. when event.message.author.id == Bot::CHARACTER
  236. :character_application
  237. when event.message.from_bot? && content.match(Regex::CHAR_APP)
  238. :character_rejection
  239. when event.message.from_bot? && event.server.nil?
  240. :pm
  241. when event.message.from_bot? && content.match(/\_New\sCharacter\sImage\_:/)
  242. :image_application
  243. end
  244. vote =
  245. case
  246. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  247. when reactions[Emoji::N]&.count.to_i > maj then :no
  248. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  249. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  250. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  251. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  252. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  253. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  254. end
  255. case [form, vote]
  256. when [:character_application, :yes]
  257. params = content.split("\n")
  258. uid = Regex::UID.match(content)
  259. user = event.server.member(uid[1])
  260. char = CharacterController.edit_character(params)
  261. image_url = ImageController.default_image(content, char.id)
  262. color = CharacterController.type_color(char)
  263. embed = character_embed(char, image_url, user, color) if character
  264. if embed
  265. bot.send_message(
  266. Channel::CHARACTER,
  267. "Good news, <@#{user.id}>! Your character was approved",
  268. false,
  269. embed
  270. )
  271. event.message.delete
  272. else
  273. event.respond(
  274. "",
  275. admin_error_embed("Something went wrong when saving application")
  276. )
  277. end
  278. when [:character_application, :no]
  279. reject_app(event, reject_char_embed(content))
  280. when [:character_rejection, :check]
  281. user = event.server.member(Regex::UID.match(content)[1])
  282. embed = message_user_embed(event)
  283. event.message.delete
  284. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  285. bot.send_message(user.dm.id, "", false, embed)
  286. when [:character_rejection, :cross]
  287. event.message.delete
  288. when [:character_rejection, :crayon]
  289. event.message.delete
  290. bot.send_temporary_message(
  291. event.channel.id,
  292. "",
  293. 35,
  294. false,
  295. self_edit_embed(content)
  296. )
  297. when [:pm, :phone]
  298. event.message.delete_own_reaction(Emoji::PHONE)
  299. user = event.message.reacted_with(Emoji::PHONE).first
  300. bot.send_message(user.dm.id, user.id, false, nil)
  301. when [:image_application, :yes]
  302. params = content.split("\n")
  303. img = ImageController.edit_image(params)
  304. char = Character.find(img.char_id)
  305. user = event.server.member(char.user_id)
  306. color = CharacterController.type_color(char)
  307. embed = char_image_embed(char, img, user, color)
  308. event.message.delete if embed
  309. channel = if img.category == 'SFW'
  310. Channel::CHARACTER
  311. else
  312. Channel::CHARACTER_NSFW
  313. end
  314. bot.send_message(channel, "Image Approved!", false, embed)
  315. end
  316. end
  317. # This will trigger when any reaction is removed in discord
  318. bot.reaction_remove do |event|
  319. end
  320. # This will trigger when a member is updated
  321. bot.member_update do |event|
  322. end
  323. # This will trigger when anyone joins the server
  324. bot.member_join do |event|
  325. end
  326. # This will trigger when anyone leaves the server
  327. bot.member_leave do |event|
  328. end
  329. # This will trigger when anyone is banned from the server
  330. bot.user_ban do |event|
  331. end
  332. # This will trigger when anyone is un-banned from the server
  333. bot.user_unban do |event|
  334. end
  335. bot.run