bot.rb 12 KB

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