bot.rb 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. opts = {
  214. "" => "List all guild members",
  215. "@user" => "List all characters belonging to the user",
  216. "name " => "Display the given character",
  217. "name | section" => "Display the given section for the character",
  218. "name | image | keword" => "Display the given image"
  219. }
  220. desc = "Display info about the guild members"
  221. member = Command.new(:member, desc, opts) do |event, name, section, keyword|
  222. sections = [:all, :default, :bio, :type, :status, :rumors, :image]
  223. case name
  224. when Regex::UID
  225. user_id = Regex::UID.match(name)
  226. when String
  227. chars = Character.where(name: name)
  228. char = chars.first if chars.length == 1
  229. if char
  230. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  231. user = event.server.member(char.user_id)
  232. color = CharacterController.type_color(char)
  233. end
  234. end
  235. case
  236. when !name
  237. chars = Character.all
  238. char_list_embed(chars)
  239. when name && user_id
  240. chars = Character.where(user_id: user_id[1])
  241. user = event.server.member(user_id[1])
  242. chars_id = []
  243. chars.each do |char|
  244. chars_id.push char.id if char.active == 'Active'
  245. end
  246. embed = user_char_embed(chars, user)
  247. msg = event.send_embed("", embed)
  248. Carousel.create(message_id: msg.id, options: chars_id)
  249. option_react(msg, chars_id)
  250. when name && chars && !char
  251. embed = dup_char_embed(chars, name)
  252. chars_id = chars.map(&:id)
  253. msg = event.send_embed("", embed)
  254. Carousel.create(message_id: msg.id, options: chars_id)
  255. option_react(msg, chars_id)
  256. when name && char && !section
  257. embed = character_embed(
  258. char: char,
  259. img: img,
  260. section: :default,
  261. user: user,
  262. color: color
  263. )
  264. msg = event.send_embed("", embed)
  265. Carousel.create(message_id: msg.id, char_id: char.id)
  266. section_react(msg)
  267. when char && section && keyword
  268. embed = command_error_embed(
  269. "Invalid Arguments",
  270. member
  271. )unless /image/i.match(section)
  272. unless embed
  273. img = CharImage.where(char_id: char.id).find_by!(keyword: keyword)
  274. embed = error_embed(
  275. "Wrong Channel!",
  276. "The requested image is NSFW"
  277. )if img.category == 'NSFW' && !event.channel.nsfw?
  278. end
  279. unless embed
  280. embed = character_embed(
  281. char: char,
  282. img: img,
  283. section: :image,
  284. user: user,
  285. color: color
  286. )
  287. msg = event.send_embed("", embed)
  288. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  289. arrow_react(msg)
  290. end
  291. embed
  292. when name && char && section
  293. sect = section.downcase.to_sym
  294. nsfw = event.channel.nsfw?
  295. img = ImageController.img_scroll(
  296. char_id: char.id,
  297. nsfw: nsfw
  298. )if section == :image
  299. if sections.detect{ |s| s == sect }
  300. embed = character_embed(
  301. char: char,
  302. img: img,
  303. section: sect,
  304. user: user,
  305. color: color,
  306. )
  307. msg = event.send_embed("", embed)
  308. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  309. if sect == :image
  310. arrow_react(msg)
  311. else
  312. section_react(msg)
  313. end
  314. else
  315. error_embed("Invalid Section!")
  316. end
  317. end
  318. rescue ActiveRecord::RecordNotFound => e
  319. error_embed("Record Not Found!", e.message)
  320. end
  321. # ---
  322. commands = [
  323. hello,
  324. matchup,
  325. app,
  326. help,
  327. poll,
  328. raffle,
  329. member
  330. ]
  331. # This will trigger on every message sent in discord
  332. bot.message do |event|
  333. content = event.message.content
  334. author = event.author.id
  335. command = /^pkmn-(\w+)/.match(content)
  336. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  337. reply = cmd.call(content, event) if cmd
  338. case reply
  339. when Embed
  340. event.send_embed("", reply)
  341. when String
  342. event.respond(reply)
  343. end
  344. event.send_embed(
  345. "",
  346. error_embed("Command not found!")
  347. )if command && !cmd && event.server
  348. Character.check_user(event) if author == Bot::CHARACTER
  349. end
  350. pm_commands = [ image ]
  351. # This will trigger when a dm is sent to the bot from a user
  352. bot.pm do |event|
  353. content = event.message.content
  354. command = /^pkmn-(\w+)/.match(content)
  355. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  356. reply = cmd.call(content, event) if cmd
  357. case reply
  358. when Embed
  359. event.send_embed("", reply)
  360. when String
  361. event.respond(reply)
  362. end
  363. end
  364. # This will trigger when any reaction is added in discord
  365. bot.reaction_add do |event|
  366. content = event.message.content
  367. reactions = event.message.reactions
  368. maj = if event.server
  369. event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  370. end
  371. maj = 1
  372. form =
  373. case
  374. when event.message.author.id == Bot::CHARACTER
  375. :character_application
  376. when event.message.from_bot? && content.match(Regex::CHAR_APP)
  377. :character_rejection
  378. when event.message.from_bot? && event.server.nil?
  379. :pm
  380. when event.message.from_bot? && content.match(/\_New\sCharacter\sImage\_:/)
  381. :image_application
  382. when carousel = Carousel.find_by(message_id: event.message.id)
  383. :carousel
  384. end
  385. vote =
  386. case
  387. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  388. when reactions[Emoji::N]&.count.to_i > maj then :no
  389. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  390. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  391. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  392. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  393. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  394. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  395. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  396. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  397. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  398. when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  399. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  400. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  401. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  402. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  403. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  404. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  405. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  406. end
  407. case [form, vote]
  408. when [:character_application, :yes]
  409. params = content.split("\n")
  410. uid = Regex::UID.match(content)
  411. user = event.server.member(uid[1])
  412. char = CharacterController.edit_character(params)
  413. img = ImageController.default_image(content, char.id)
  414. color = CharacterController.type_color(char)
  415. embed = character_embed(
  416. char: char,
  417. img: img,
  418. user: user,
  419. color: color
  420. )if char
  421. if embed
  422. bot.send_message(
  423. Channel::CHARACTER,
  424. "Good news, <@#{user.id}>! Your character was approved",
  425. false,
  426. embed
  427. )
  428. event.message.delete
  429. else
  430. event.respond(
  431. "",
  432. admin_error_embed("Something went wrong when saving application")
  433. )
  434. end
  435. when [:character_application, :no]
  436. content = event.message.content
  437. embed = reject_char_embed(content)
  438. event.message.delete
  439. reject = event.send_embed(content, embed)
  440. Emoji::CHAR_APP.each do |reaction|
  441. reject.react(reaction)
  442. end
  443. reject.react(Emoji::CHECK)
  444. reject.react(Emoji::CROSS)
  445. reject.react(Emoji::CRAYON)
  446. when [:character_rejection, :check]
  447. user = event.server.member(Regex::UID.match(content)[1])
  448. embed = user_char_app(event)
  449. event.message.delete
  450. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  451. bot.send_message(user.dm.id, "", false, embed)
  452. when [:character_rejection, :cross]
  453. event.message.delete
  454. when [:character_rejection, :crayon]
  455. event.message.delete
  456. bot.send_temporary_message(
  457. event.channel.id,
  458. "",
  459. 35,
  460. false,
  461. self_edit_embed(content)
  462. )
  463. when [:pm, :phone]
  464. event.message.delete_own_reaction(Emoji::PHONE)
  465. user = event.message.reacted_with(Emoji::PHONE).first
  466. bot.send_message(user.dm.id, user.id, false, nil)
  467. when [:image_application, :yes]
  468. params = content.split("\n")
  469. img = ImageController.edit_image(params)
  470. char = Character.find(img.char_id)
  471. user = event.server.member(char.user_id)
  472. color = CharacterController.type_color(char)
  473. embed = char_image_embed(char, img, user, color)
  474. event.message.delete if embed
  475. channel = if img.category == 'SFW'
  476. Channel::CHARACTER
  477. else
  478. Channel::CHARACTER_NSFW
  479. end
  480. bot.send_message(channel, "Image Approved!", false, embed)
  481. when [:image_application, :no]
  482. content = event.message.content
  483. embed = reject_img_embed(content)
  484. event.message.delete
  485. reject = event.send_embed(content, embed)
  486. Emoji::IMG_APP.each do |reaction|
  487. reject.react(reaction)
  488. end
  489. reject.react(Emoji::CHECK)
  490. reject.react(Emoji::CROSS)
  491. when [:image_application, :check]
  492. user = event.server.member(Regex::UID.match(content)[1])
  493. embed = user_img_app(event)
  494. event.message.delete
  495. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  496. bot.send_message(user.dm.id, "", false, embed)
  497. when [:image_application, :cross]
  498. event.message.delete
  499. when [:carousel, :notebook]
  500. emoji = Emoji::NOTEBOOK
  501. users = event.message.reacted_with(emoji)
  502. users.each do |user|
  503. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  504. end
  505. char = Character.find(carousel.char_id)
  506. embed = character_embed(
  507. char: char,
  508. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  509. user: event.server.member(char.user_id),
  510. color: CharacterController.type_color(char),
  511. section: :bio
  512. )
  513. event.message.edit("", embed)
  514. when [:carousel, :question]
  515. emoji = Emoji::QUESTION
  516. users = event.message.reacted_with(emoji)
  517. users.each do |user|
  518. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  519. end
  520. char = Character.find(carousel.char_id)
  521. embed = character_embed(
  522. char: char,
  523. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  524. user: event.server.member(char.user_id),
  525. color: CharacterController.type_color(char),
  526. section: :status
  527. )
  528. event.message.edit("", embed)
  529. when [:carousel, :pallet]
  530. emoji = Emoji::PALLET
  531. users = event.message.reacted_with(emoji)
  532. users.each do |user|
  533. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  534. end
  535. char = Character.find(carousel.char_id)
  536. embed = character_embed(
  537. char: char,
  538. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  539. user: event.server.member(char.user_id),
  540. color: CharacterController.type_color(char),
  541. section: :type
  542. )
  543. event.message.edit("", embed)
  544. when [:carousel, :ear]
  545. emoji = Emoji::EAR
  546. users = event.message.reacted_with(emoji)
  547. users.each do |user|
  548. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  549. end
  550. char = Character.find(carousel.char_id)
  551. embed = character_embed(
  552. char: char,
  553. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  554. user: event.server.member(char.user_id),
  555. color: CharacterController.type_color(char),
  556. section: :rumors
  557. )
  558. event.message.edit("", embed)
  559. when [:carousel, :picture]
  560. event.message.delete_all_reactions
  561. char = Character.find(carousel.char_id)
  562. img = ImageController.img_scroll(
  563. char_id: char.id,
  564. nsfw: event.channel.nsfw?,
  565. )
  566. carousel.update(id: carousel.id, image_id: img.id)
  567. embed = character_embed(
  568. char: char,
  569. img: img,
  570. user: event.server.member(char.user_id),
  571. color: CharacterController.type_color(char),
  572. section: :image
  573. )
  574. event.message.edit("", embed)
  575. arrow_react(event.message)
  576. when [:carousel, :bags]
  577. when [:carousel, :family]
  578. when [:carousel, :eyes]
  579. emoji = Emoji::EYES
  580. users = event.message.reacted_with(emoji)
  581. users.each do |user|
  582. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  583. end
  584. char = Character.find(carousel.char_id)
  585. embed = character_embed(
  586. char: char,
  587. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  588. user: event.server.member(char.user_id),
  589. color: CharacterController.type_color(char),
  590. section: :all
  591. )
  592. event.message.edit("", embed)
  593. when [:carousel, :key]
  594. emoji = Emoji::KEY
  595. users = event.message.reacted_with(emoji)
  596. users.each do |user|
  597. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  598. end
  599. char = Character.find(carousel.char_id)
  600. embed = character_embed(
  601. char: char,
  602. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  603. user: event.server.member(char.user_id),
  604. color: CharacterController.type_color(char),
  605. section: :default
  606. )
  607. event.message.edit("", embed)
  608. when [:carousel, :back]
  609. event.message.delete_all_reactions
  610. char = Character.find(carousel.char_id)
  611. embed = character_embed(
  612. char: char,
  613. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  614. user: event.server.member(char.user_id),
  615. color: CharacterController.type_color(char),
  616. section: :default
  617. )
  618. event.message.edit("", embed)
  619. section_react(event.message)
  620. when [:carousel, :left], [:carousel, :right]
  621. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  622. users = event.message.reacted_with(emoji)
  623. users.each do |user|
  624. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  625. end
  626. char = Character.find(carousel.char_id)
  627. img = ImageController.img_scroll(
  628. char_id: char.id,
  629. nsfw: event.channel.nsfw?,
  630. img: carousel.image_id,
  631. dir: vote
  632. )
  633. carousel.update(id: carousel.id, image_id: img.id)
  634. embed = character_embed(
  635. char: char,
  636. img: img,
  637. user: event.server.member(char.user_id),
  638. color: CharacterController.type_color(char),
  639. section: :image
  640. )
  641. event.message.edit("", embed)
  642. when [:carousel, :number]
  643. char_index = nil
  644. Emoji::NUMBERS.each.with_index do |emoji, i|
  645. char_index = i if reactions[emoji]&.count.to_i > 1
  646. end
  647. if char_index
  648. event.message.delete_all_reactions
  649. char = Character.find(carousel.options[char_index])
  650. carousel.update(id: carousel.id, char_id: char.id)
  651. embed = character_embed(
  652. char: char,
  653. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  654. user: event.server.member(char.user_id),
  655. color: CharacterController.type_color(char),
  656. section: :default
  657. )
  658. event.message.edit("", embed)
  659. section_react(event.message)
  660. end
  661. when [:carousel, :cross]
  662. event.message.delete
  663. carousel.delete
  664. end
  665. end
  666. # This will trigger when any reaction is removed in discord
  667. bot.reaction_remove do |event|
  668. end
  669. # This will trigger when a member is updated
  670. bot.member_update do |event|
  671. end
  672. # This will trigger when anyone joins the server
  673. bot.member_join do |event|
  674. end
  675. # This will trigger when anyone leaves the server
  676. bot.member_leave do |event|
  677. end
  678. # This will trigger when anyone is banned from the server
  679. bot.user_ban do |event|
  680. end
  681. # This will trigger when anyone is un-banned from the server
  682. bot.user_unban do |event|
  683. end
  684. bot.run