bot.rb 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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_TEST = 308250685554556930
  11. ADMINS = 453262984769437696
  12. # ---
  13. Dotenv.load if BOT_ENV != 'production'
  14. db_yml = File.open('config/database.yml') do |erb|
  15. ERB.new(erb.read).result
  16. end
  17. db_config = YAML.safe_load(db_yml)[BOT_ENV]
  18. ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
  19. ActiveRecord::Base.establish_connection(
  20. adapter: 'postgresql',
  21. host: db_config.fetch('host') { 'localhost' },
  22. database: db_config['database'],
  23. user: db_config['user'],
  24. password: db_config['password']
  25. )
  26. Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
  27. Dir['/lib/*.rb'].each { |f| require f }
  28. token = ENV['DISCORD_BOT_TOKEN']
  29. bot = Discordrb::Bot.new(token: token)
  30. # Methods: define basic methods here
  31. # ---
  32. # Commands: structure basic bot commands here
  33. commands = []
  34. pm_commands = []
  35. hello = Command.new(:hello, "Says hello!") do |event|
  36. user = event.author.nickname || event.author.name
  37. greetings = [
  38. "Hi there, #{user}",
  39. "Greetings #{user}, you lovable bum",
  40. "Alola, #{user}",
  41. "Hello, #{user}! The Guildmasters have been waiting",
  42. "#{user} would like to battle!"
  43. ]
  44. Embed.new(
  45. description: greetings.sample,
  46. color: event.author.color.combined,
  47. thumbnail: {
  48. url: Image::HAPPY
  49. }
  50. )
  51. end
  52. opts = {
  53. "" => "displays a list of all commands",
  54. "command" => "displays info and usage for specified command"
  55. }
  56. desc = "Displays help information for the commands"
  57. help = Command.new(:help, desc, opts) do |event, command|
  58. if command
  59. short = /pkmn-(\w+)/.match(command)
  60. command = short ? short[1] : command
  61. cmd = commands.detect { |c| c.name == command.to_sym }
  62. pm_cmd = pm_commands.detect { |pc| pc.name == command.to_sym }
  63. end
  64. if command && cmd
  65. command_embed(cmd)
  66. elsif command && pm_cmd
  67. command_embed(pm_cmd, "PM Command")
  68. elsif !command
  69. embed = command_list_embed(
  70. pm_commands,
  71. "Can only be used in a pm with the bot",
  72. "PM Commands"
  73. )
  74. event.send_embed("", embed)
  75. command_list_embed(commands)
  76. else
  77. command_error_embed("Command not found!", help)
  78. end
  79. end
  80. opts = { "type" => "" }
  81. desc = "Displays a chart of effectiveness for the given type"
  82. matchup = Command.new(:matchup, desc, opts) do |event, type|
  83. channel = event.channel.id
  84. file = "images/Type #{type.capitalize}.png"
  85. if File.exists?(file)
  86. bot.send_file(channel, File.open(file, 'r'))
  87. else
  88. bot.respond("I do not know this pokemon type! Please try again!")
  89. end
  90. end
  91. opts = {
  92. "" => "starts a new app",
  93. "name" => "edits an existing app",
  94. "name | (in)active" => "sets app to active or inactive"
  95. }
  96. desc = "Everything to do with character applications"
  97. app = Command.new(:app, desc, opts) do |event, name, status|
  98. user = event.author
  99. user_name = user.nickname || user.name
  100. color = user.color ? user.color.combined : Color::DEFAULT
  101. character = Character.where(user_id: user.id).find_by(name: name) if name
  102. active = status.match(/(in)?active/i) if status
  103. case
  104. when name && !character
  105. app_not_found_embed(user_name, name)
  106. when status && active && character
  107. character.update!(active: active[0].capitalize)
  108. character.reload
  109. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  110. when name && character && !status
  111. edit_url = Url::CHARACTER + character.edit_url
  112. embed = edit_app_dm(name, edit_url, color)
  113. bot.send_message(user.dm.id, "", false, embed)
  114. edit_app_embed(user_name, name, color)
  115. when !name && !status
  116. embed = new_app_dm(user_name, color, user.id)
  117. message = bot.send_message(user.dm.id, "", false, embed)
  118. message.react(Emoji::PHONE)
  119. new_app_embed(user_name, color)
  120. else
  121. command_error_embed("There was an error processing your application!", app)
  122. end
  123. end
  124. opts = { "question | option1, option2, etc" => ""}
  125. desc = "Creates a dynamic poll in any channel"
  126. poll = Command.new(:poll, desc, opts) do |event, question, options|
  127. if options
  128. options_array = options.split(/\s?,\s?/)
  129. new_poll_embed(event, question, options_array)
  130. else
  131. command_error_embed("There was an error creating your poll!", poll)
  132. end
  133. end
  134. opts = {
  135. "participants" =>
  136. "Accepts Everyone, Here, or a comma seperated list of names"
  137. }
  138. desc = "Creates a raffle and picks a winner"
  139. raffle = Command.new(:raffle, desc, opts) do |event, participant|
  140. participants =
  141. case participant
  142. when /^everyone$/i
  143. event.server.members
  144. when /^here$/i
  145. event.message.channel.users
  146. else
  147. participant.split(/\s?,\s?/)
  148. end
  149. winner = participants.sample
  150. winner_name =
  151. case winner
  152. when String
  153. winner
  154. else
  155. winner.nickname || winner.username
  156. end
  157. if winner_name
  158. message_embed("Raffle Results!", "Winner: #{winner_name}")
  159. else
  160. command_error_embed("There was an error creating your raffle!", raffle)
  161. end
  162. end
  163. opts = {
  164. "name | keyword | (n)sfw | url" => "add or update image",
  165. "name | keyword | delete" => "remove image",
  166. "name | keyword" => "display image",
  167. "name" => "list all images"
  168. }
  169. desc = "View, add and edit your characters' images"
  170. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
  171. user = event.author
  172. char = Character.where(user_id: user.id).find_by!(name: name) if name
  173. color = CharacterController.type_color(char) if char
  174. img = CharImage.find_by(keyword: keyword) if keyword
  175. case
  176. when /^Default$/i.match(keyword)
  177. error_embed(
  178. "Cannot update Default here!",
  179. "Use `pkmn-app character` to edit your default image in your form"
  180. )
  181. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  182. img_app = CharImage.to_form(
  183. char.name,
  184. char.species,
  185. char.id,
  186. keyword,
  187. tag,
  188. url,
  189. user.id
  190. )
  191. approval = bot.send_message(Channel::APPROVAL, img_app, false, nil)
  192. approval.react(Emoji::Y)
  193. approval.react(Emoji::N)
  194. success_embed("Your image has been submitted for approval!")
  195. when char && img && tag && tag.match(/delete/i)
  196. CharImage.find(img.id).delete
  197. success_embed("Removed image: #{keyword}")
  198. when char && img && !tag
  199. char_image_embed(char, img, user, color)
  200. when char && !keyword
  201. imgs = CharImage.where(char_id: char.id)
  202. image_list_embed(char, imgs, user, color)
  203. when keyword && !img
  204. error_embed("Could not find your image!")
  205. else
  206. command_error_embed("Could not process your image request!", image)
  207. end
  208. rescue ActiveRecord::RecordNotFound
  209. error_embed(
  210. "Character not Found!",
  211. "I could not find your character named #{name}"
  212. )
  213. end
  214. opts = {
  215. "" => "List all guild members",
  216. "@user" => "List all characters belonging to the user",
  217. "name " => "Display the given character",
  218. "name | section" => "Display the given section for the character",
  219. "name | image | keword" => "Display the given image"
  220. }
  221. desc = "Display info about the guild members"
  222. member = Command.new(:member, desc, opts) do |event, name, section, keyword|
  223. sections = [:all, :default, :bio, :type, :status, :rumors, :image]
  224. case name
  225. when Regex::UID
  226. user_id = Regex::UID.match(name)
  227. when String
  228. chars = Character.where(name: name)
  229. char = chars.first if chars.length == 1
  230. if char
  231. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  232. user = event.server.member(char.user_id)
  233. color = CharacterController.type_color(char)
  234. end
  235. end
  236. case
  237. when !name
  238. chars = Character.all
  239. char_list_embed(chars)
  240. when name && user_id
  241. chars = Character.where(user_id: user_id[1])
  242. user = event.server.member(user_id[1])
  243. chars_id = []
  244. chars.each do |char|
  245. chars_id.push char.id if char.active == 'Active'
  246. end
  247. embed = user_char_embed(chars, user)
  248. msg = event.send_embed("", embed)
  249. Carousel.create(message_id: msg.id, options: chars_id)
  250. option_react(msg, chars_id)
  251. when name && chars && !char
  252. embed = dup_char_embed(chars, name)
  253. chars_id = chars.map(&:id)
  254. msg = event.send_embed("", embed)
  255. Carousel.create(message_id: msg.id, options: chars_id)
  256. option_react(msg, chars_id)
  257. when name && char && !section
  258. embed = character_embed(
  259. char: char,
  260. img: img,
  261. section: :default,
  262. user: user,
  263. color: color
  264. )
  265. msg = event.send_embed("", embed)
  266. Carousel.create(message_id: msg.id, char_id: char.id)
  267. section_react(msg)
  268. when char && section && keyword
  269. embed = command_error_embed(
  270. "Invalid Arguments",
  271. member
  272. )unless /image/i.match(section)
  273. unless embed
  274. img = CharImage.where(char_id: char.id).find_by!(keyword: keyword)
  275. embed = error_embed(
  276. "Wrong Channel!",
  277. "The requested image is NSFW"
  278. )if img.category == 'NSFW' && !event.channel.nsfw?
  279. end
  280. unless embed
  281. embed = character_embed(
  282. char: char,
  283. img: img,
  284. section: :image,
  285. user: user,
  286. color: color
  287. )
  288. msg = event.send_embed("", embed)
  289. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  290. arrow_react(msg)
  291. end
  292. embed
  293. when name && char && section
  294. sect = section.downcase.to_sym
  295. nsfw = event.channel.nsfw?
  296. img = ImageController.img_scroll(
  297. char_id: char.id,
  298. nsfw: nsfw
  299. )if section == :image
  300. if sections.detect{ |s| s == sect }
  301. embed = character_embed(
  302. char: char,
  303. img: img,
  304. section: sect,
  305. user: user,
  306. color: color,
  307. )
  308. msg = event.send_embed("", embed)
  309. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  310. if sect == :image
  311. arrow_react(msg)
  312. else
  313. section_react(msg)
  314. end
  315. else
  316. error_embed("Invalid Section!")
  317. end
  318. end
  319. rescue ActiveRecord::RecordNotFound => e
  320. error_embed("Record Not Found!", e.message)
  321. end
  322. # ---
  323. commands = [
  324. hello,
  325. matchup,
  326. app,
  327. help,
  328. poll,
  329. raffle,
  330. member
  331. ]
  332. # This will trigger on every message sent in discord
  333. bot.message do |event|
  334. content = event.message.content
  335. author = event.author.id
  336. command = /^pkmn-(\w+)/.match(content)
  337. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  338. reply = cmd.call(content, event) if cmd
  339. case reply
  340. when Embed
  341. event.send_embed("", reply)
  342. when String
  343. event.respond(reply)
  344. end
  345. event.send_embed(
  346. "",
  347. error_embed("Command not found!")
  348. )if command && !cmd && event.server
  349. Character.check_user(event) if author == Bot::CHARACTER
  350. end
  351. pm_commands = [ image ]
  352. # This will trigger when a dm is sent to the bot from a user
  353. bot.pm do |event|
  354. content = event.message.content
  355. command = /^pkmn-(\w+)/.match(content)
  356. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  357. reply = cmd.call(content, event) if cmd
  358. case reply
  359. when Embed
  360. event.send_embed("", reply)
  361. when String
  362. event.respond(reply)
  363. end
  364. end
  365. # This will trigger when any reaction is added in discord
  366. bot.reaction_add do |event|
  367. content = event.message.content
  368. reactions = event.message.reactions
  369. maj = if event.server
  370. event.server.roles.find{ |r| r.id == ADMINS }.members.count / 2
  371. end
  372. maj = 1
  373. form =
  374. case
  375. when event.message.author.id == Bot::CHARACTER
  376. :character_application
  377. when event.message.from_bot? && content.match(Regex::CHAR_APP)
  378. :character_rejection
  379. when event.message.from_bot? && event.server.nil?
  380. :pm
  381. when event.message.from_bot? && content.match(/\_New\sCharacter\sImage\_:/)
  382. :image_application
  383. when carousel = Carousel.find_by(message_id: event.message.id)
  384. :carousel
  385. end
  386. vote =
  387. case
  388. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  389. when reactions[Emoji::N]&.count.to_i > maj then :no
  390. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  391. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  392. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  393. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  394. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  395. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  396. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  397. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  398. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  399. when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  400. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  401. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  402. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  403. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  404. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  405. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  406. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  407. end
  408. case [form, vote]
  409. when [:character_application, :yes]
  410. params = content.split("\n")
  411. uid = Regex::UID.match(content)
  412. user = event.server.member(uid[1])
  413. char = CharacterController.edit_character(params)
  414. img = ImageController.default_image(content, char.id)
  415. color = CharacterController.type_color(char)
  416. embed = character_embed(
  417. char: char,
  418. img: img,
  419. user: user,
  420. color: color
  421. )if char
  422. if embed
  423. bot.send_message(
  424. Channel::CHARACTER,
  425. "Good news, <@#{user.id}>! Your character was approved",
  426. false,
  427. embed
  428. )
  429. event.message.delete
  430. else
  431. event.respond(
  432. "",
  433. admin_error_embed("Something went wrong when saving application")
  434. )
  435. end
  436. when [:character_application, :no]
  437. content = event.message.content
  438. embed = reject_char_embed(content)
  439. event.message.delete
  440. reject = event.send_embed(content, embed)
  441. Emoji::CHAR_APP.each do |reaction|
  442. reject.react(reaction)
  443. end
  444. reject.react(Emoji::CHECK)
  445. reject.react(Emoji::CROSS)
  446. reject.react(Emoji::CRAYON)
  447. when [:character_rejection, :check]
  448. user = event.server.member(Regex::UID.match(content)[1])
  449. embed = user_char_app(event)
  450. event.message.delete
  451. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  452. bot.send_message(user.dm.id, "", false, embed)
  453. when [:character_rejection, :cross]
  454. event.message.delete
  455. when [:character_rejection, :crayon]
  456. event.message.delete
  457. bot.send_temporary_message(
  458. event.channel.id,
  459. "",
  460. 35,
  461. false,
  462. self_edit_embed(content)
  463. )
  464. when [:pm, :phone]
  465. event.message.delete_own_reaction(Emoji::PHONE)
  466. user = event.message.reacted_with(Emoji::PHONE).first
  467. bot.send_message(user.dm.id, user.id, false, nil)
  468. when [:image_application, :yes]
  469. params = content.split("\n")
  470. img = ImageController.edit_image(params)
  471. char = Character.find(img.char_id)
  472. user = event.server.member(char.user_id)
  473. color = CharacterController.type_color(char)
  474. embed = char_image_embed(char, img, user, color)
  475. event.message.delete if embed
  476. channel = if img.category == 'SFW'
  477. Channel::CHARACTER
  478. else
  479. Channel::CHARACTER_NSFW
  480. end
  481. bot.send_message(channel, "Image Approved!", false, embed)
  482. when [:image_application, :no]
  483. content = event.message.content
  484. embed = reject_img_embed(content)
  485. event.message.delete
  486. reject = event.send_embed(content, embed)
  487. Emoji::IMG_APP.each do |reaction|
  488. reject.react(reaction)
  489. end
  490. reject.react(Emoji::CHECK)
  491. reject.react(Emoji::CROSS)
  492. when [:image_application, :check]
  493. user = event.server.member(Regex::UID.match(content)[1])
  494. embed = user_img_app(event)
  495. event.message.delete
  496. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  497. bot.send_message(user.dm.id, "", false, embed)
  498. when [:image_application, :cross]
  499. event.message.delete
  500. when [:carousel, :notebook]
  501. emoji = Emoji::NOTEBOOK
  502. users = event.message.reacted_with(emoji)
  503. users.each do |user|
  504. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  505. end
  506. char = Character.find(carousel.char_id)
  507. embed = character_embed(
  508. char: char,
  509. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  510. user: event.server.member(char.user_id),
  511. color: CharacterController.type_color(char),
  512. section: :bio
  513. )
  514. event.message.edit("", embed)
  515. when [:carousel, :question]
  516. emoji = Emoji::QUESTION
  517. users = event.message.reacted_with(emoji)
  518. users.each do |user|
  519. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  520. end
  521. char = Character.find(carousel.char_id)
  522. embed = character_embed(
  523. char: char,
  524. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  525. user: event.server.member(char.user_id),
  526. color: CharacterController.type_color(char),
  527. section: :status
  528. )
  529. event.message.edit("", embed)
  530. when [:carousel, :pallet]
  531. emoji = Emoji::PALLET
  532. users = event.message.reacted_with(emoji)
  533. users.each do |user|
  534. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  535. end
  536. char = Character.find(carousel.char_id)
  537. embed = character_embed(
  538. char: char,
  539. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  540. user: event.server.member(char.user_id),
  541. color: CharacterController.type_color(char),
  542. section: :type
  543. )
  544. event.message.edit("", embed)
  545. when [:carousel, :ear]
  546. emoji = Emoji::EAR
  547. users = event.message.reacted_with(emoji)
  548. users.each do |user|
  549. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  550. end
  551. char = Character.find(carousel.char_id)
  552. embed = character_embed(
  553. char: char,
  554. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  555. user: event.server.member(char.user_id),
  556. color: CharacterController.type_color(char),
  557. section: :rumors
  558. )
  559. event.message.edit("", embed)
  560. when [:carousel, :picture]
  561. event.message.delete_all_reactions
  562. char = Character.find(carousel.char_id)
  563. img = ImageController.img_scroll(
  564. char_id: char.id,
  565. nsfw: event.channel.nsfw?,
  566. )
  567. carousel.update(id: carousel.id, image_id: img.id)
  568. embed = character_embed(
  569. char: char,
  570. img: img,
  571. user: event.server.member(char.user_id),
  572. color: CharacterController.type_color(char),
  573. section: :image
  574. )
  575. event.message.edit("", embed)
  576. arrow_react(event.message)
  577. when [:carousel, :bags]
  578. when [:carousel, :family]
  579. when [:carousel, :eyes]
  580. emoji = Emoji::EYES
  581. users = event.message.reacted_with(emoji)
  582. users.each do |user|
  583. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  584. end
  585. char = Character.find(carousel.char_id)
  586. embed = character_embed(
  587. char: char,
  588. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  589. user: event.server.member(char.user_id),
  590. color: CharacterController.type_color(char),
  591. section: :all
  592. )
  593. event.message.edit("", embed)
  594. when [:carousel, :key]
  595. emoji = Emoji::KEY
  596. users = event.message.reacted_with(emoji)
  597. users.each do |user|
  598. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  599. end
  600. char = Character.find(carousel.char_id)
  601. embed = character_embed(
  602. char: char,
  603. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  604. user: event.server.member(char.user_id),
  605. color: CharacterController.type_color(char),
  606. section: :default
  607. )
  608. event.message.edit("", embed)
  609. when [:carousel, :back]
  610. event.message.delete_all_reactions
  611. char = Character.find(carousel.char_id)
  612. embed = character_embed(
  613. char: char,
  614. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  615. user: event.server.member(char.user_id),
  616. color: CharacterController.type_color(char),
  617. section: :default
  618. )
  619. event.message.edit("", embed)
  620. section_react(event.message)
  621. when [:carousel, :left], [:carousel, :right]
  622. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  623. users = event.message.reacted_with(emoji)
  624. users.each do |user|
  625. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  626. end
  627. char = Character.find(carousel.char_id)
  628. img = ImageController.img_scroll(
  629. char_id: char.id,
  630. nsfw: event.channel.nsfw?,
  631. img: carousel.image_id,
  632. dir: vote
  633. )
  634. carousel.update(id: carousel.id, image_id: img.id)
  635. embed = character_embed(
  636. char: char,
  637. img: img,
  638. user: event.server.member(char.user_id),
  639. color: CharacterController.type_color(char),
  640. section: :image
  641. )
  642. event.message.edit("", embed)
  643. when [:carousel, :number]
  644. char_index = nil
  645. Emoji::NUMBERS.each.with_index do |emoji, i|
  646. char_index = i if reactions[emoji]&.count.to_i > 1
  647. end
  648. if char_index
  649. event.message.delete_all_reactions
  650. char = Character.find(carousel.options[char_index])
  651. carousel.update(id: carousel.id, char_id: char.id)
  652. embed = character_embed(
  653. char: char,
  654. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  655. user: event.server.member(char.user_id),
  656. color: CharacterController.type_color(char),
  657. section: :default
  658. )
  659. event.message.edit("", embed)
  660. section_react(event.message)
  661. end
  662. when [:carousel, :cross]
  663. event.message.delete
  664. carousel.delete
  665. end
  666. end
  667. # This will trigger when any reaction is removed in discord
  668. bot.reaction_remove do |event|
  669. end
  670. # This will trigger when a member is updated
  671. bot.member_update do |event|
  672. end
  673. # This will trigger when anyone joins the server
  674. bot.member_join do |event|
  675. end
  676. # This will trigger when anyone leaves the server
  677. bot.member_leave do |event|
  678. end
  679. # This will trigger when anyone is banned from the server
  680. bot.user_ban do |event|
  681. end
  682. # This will trigger when anyone is un-banned from the server
  683. bot.user_unban do |event|
  684. end
  685. bot.run