bot.rb 24 KB

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