bot.rb 23 KB

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