bot.rb 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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 colors and regexes
  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. # 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. img = ImageUrl.find_by(name: 'happy')
  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: img.url
  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" => "Single Display",
  83. "primary | secondary" => "Double Display"
  84. }
  85. desc = "Displays a chart of effectiveness for the given type"
  86. matchup = Command.new(:matchup, desc, opts) do |event, primary, secondary|
  87. channel = event.channel.id
  88. image_out = 'images/Type Double.png'
  89. file_p = "images/Type #{primary.capitalize}.png" if primary
  90. file_s = "images/Type #{secondary.capitalize}.png" if secondary
  91. case
  92. when !file_p
  93. command_error_embed("There was an error processing your request!", matchup)
  94. when !file_s && File.exists?(file_p)
  95. bot.send_file(channel, File.open(file_p, 'r'))
  96. when File.exists?(file_p) && File.exists?(file_s)
  97. append_image(file_p, file_s, image_out)
  98. bot.send_file(channel, File.open(image_out, 'r'))
  99. else
  100. error_embed("Type(s) not found!")
  101. end
  102. end
  103. opts = {
  104. "" => "starts a new app",
  105. "name" => "edits an existing app",
  106. "name | (in)active" => "sets app to active or inactive"
  107. }
  108. desc = "Everything to do with character applications"
  109. app = Command.new(:app, desc, opts) do |event, name, status|
  110. user = event.author
  111. user_name = user.nickname || user.name
  112. color = user.color.combined if user.color
  113. chars = []
  114. character =
  115. if user.roles.map(&:name).include?('Guild Masters')
  116. chars = Character.where(name: name)
  117. chars.first if chars.length == 1
  118. else
  119. Character.where(user_id: user.id).find_by(name: name) if name
  120. end
  121. active = status.match(/(in)?active/i) if status
  122. case
  123. when !chars.empty? && !character
  124. chars.each do |char|
  125. edit_url = Url::CHARACTER + char.edit_url
  126. embed = edit_app_dm(name, edit_url, color)
  127. bot.send_message(
  128. user.dm.id,
  129. "<@#{char.user_id}>'s character:",
  130. false,
  131. embed
  132. )
  133. end
  134. when name && !character
  135. app_not_found_embed(user_name, name)
  136. when status && active && character
  137. character.update!(active: active[0].capitalize)
  138. character.reload
  139. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  140. when name && character && !status
  141. edit_url = Url::CHARACTER + character.edit_url
  142. embed = edit_app_dm(name, edit_url, color)
  143. bot.send_message(user.dm.id, "", false, embed)
  144. edit_app_embed(user_name, name, color)
  145. when !name && !status
  146. embed = new_app_dm(user_name, user.id, color)
  147. message = bot.send_message(user.dm.id, "", false, embed)
  148. message.react(Emoji::PHONE)
  149. new_app_embed(user_name, color)
  150. else
  151. command_error_embed("There was an error processing your application!", app)
  152. end
  153. end
  154. opts = { "question | option1, option2, etc" => ""}
  155. desc = "Creates a dynamic poll in any channel"
  156. poll = Command.new(:poll, desc, opts) do |event, question, options|
  157. if options
  158. options_array = options.split(/\s?,\s?/)
  159. new_poll_embed(event, question, options_array)
  160. else
  161. command_error_embed("There was an error creating your poll!", poll)
  162. end
  163. end
  164. opts = {
  165. "participants" =>
  166. "Accepts Everyone, Here, or a comma seperated list of names"
  167. }
  168. desc = "Creates a raffle and picks a winner"
  169. raffle = Command.new(:raffle, desc, opts) do |event, participant|
  170. participants =
  171. case participant
  172. when /^everyone$/i
  173. event.server.members
  174. when /^here$/i
  175. event.message.channel.users
  176. else
  177. participant.split(/\s?,\s?/)
  178. end
  179. winner = participants.sample
  180. winner_name =
  181. case winner
  182. when String
  183. winner
  184. else
  185. winner.nickname || winner.username
  186. end
  187. if winner_name
  188. message_embed("Raffle Results!", "Winner: #{winner_name}")
  189. else
  190. command_error_embed("There was an error creating your raffle!", raffle)
  191. end
  192. end
  193. opts = {
  194. "name | keyword | (n)sfw | url" => "add or update image",
  195. "name | keyword | delete" => "remove image",
  196. "name | keyword" => "display image",
  197. "name" => "list all images"
  198. }
  199. desc = "View, add and edit your characters' images"
  200. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url|
  201. user = event.author
  202. chars = []
  203. char =
  204. if user.roles.map(&:name).include?('Guild Masters')
  205. chars = Character.where(name: name)
  206. chars.first if chars.length == 1
  207. else
  208. Character.where(user_id: user.id).find_by!(name: name) if name
  209. end
  210. color = CharacterController.type_color(char) if char
  211. img = CharImage.where(char_id: char.id).find_by(keyword: keyword) if keyword
  212. case
  213. when /^Default$/i.match(keyword)
  214. error_embed(
  215. "Cannot update Default here!",
  216. "Use `pkmn-app character` to edit your default image in your form"
  217. )
  218. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  219. img_app = CharImage.to_form(
  220. char: char,
  221. keyword: keyword,
  222. category: tag,
  223. url: url,
  224. user_id: user.id
  225. )
  226. approval = bot.send_message(ENV['APP_CH'].to_i, "", false, img_app)
  227. approval.react(Emoji::Y)
  228. approval.react(Emoji::N)
  229. success_embed("Your image has been submitted for approval!")
  230. when char && img && tag && tag.match(/delete/i)
  231. CharImage.find(img.id).delete
  232. success_embed("Removed image: #{keyword}")
  233. when char && img && !tag
  234. char_image_embed(char, img, user, color)
  235. when char && !keyword
  236. imgs = CharImage.where(char_id: char.id)
  237. image_list_embed(char, imgs, user, color)
  238. when keyword && !img
  239. error_embed("Could not find your image!")
  240. else
  241. command_error_embed("Could not process your image request!", image)
  242. end
  243. rescue ActiveRecord::RecordNotFound
  244. error_embed(
  245. "Character not Found!",
  246. "I could not find your character named #{name}"
  247. )
  248. end
  249. opts = {
  250. "" => "List all guild members",
  251. "@user" => "List all characters belonging to the user",
  252. "name " => "Display the given character",
  253. "name | section" => "Display the given section for the character",
  254. "name | image | keword" => "Display the given image"
  255. }
  256. desc = "Display info about the guild members"
  257. member = Command.new(:member, desc, opts) do |event, name, section, keyword|
  258. sections = [:all, :default, :bio, :type, :status, :rumors, :image, :bags]
  259. case name
  260. when UID
  261. user_id = UID.match(name)
  262. when String
  263. chars = Character.where(name: name)
  264. char = chars.first if chars.length == 1
  265. if char
  266. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  267. user = char.user_id.match(/public/i) ?
  268. char.user_id : event.server.member(char.user_id)
  269. color = CharacterController.type_color(char)
  270. end
  271. end
  272. case
  273. when !name
  274. chars = Character.all
  275. char_list_embed(chars)
  276. when name && user_id
  277. chars = Character.where(user_id: user_id[1])
  278. user = event.server.member(user_id[1])
  279. chars_id = []
  280. chars.each do |char|
  281. chars_id.push char.id if char.active == 'Active'
  282. end
  283. embed = user_char_embed(chars, user)
  284. msg = event.send_embed("", embed)
  285. Carousel.create(message_id: msg.id, options: chars_id)
  286. option_react(msg, chars_id)
  287. when name && chars.empty?
  288. error_embed(
  289. "Character not found!",
  290. "Could not find a character named #{name}"
  291. )
  292. when name && chars && !char
  293. embed = dup_char_embed(chars, name)
  294. chars_id = chars.map(&:id)
  295. msg = event.send_embed("", embed)
  296. Carousel.create(message_id: msg.id, options: chars_id)
  297. option_react(msg, chars_id)
  298. when name && char && !section
  299. embed = character_embed(
  300. char: char,
  301. img: img,
  302. section: :default,
  303. user: user,
  304. color: color
  305. )
  306. msg = event.send_embed("", embed)
  307. Carousel.create(message_id: msg.id, char_id: char.id)
  308. section_react(msg)
  309. when char && section && keyword
  310. embed = command_error_embed(
  311. "Invalid Arguments",
  312. member
  313. )unless /image/i.match(section)
  314. unless embed
  315. img = CharImage.where(char_id: char.id).find_by!(keyword: keyword)
  316. embed = error_embed(
  317. "Wrong Channel!",
  318. "The requested image is NSFW"
  319. )if img.category == 'NSFW' && !event.channel.nsfw?
  320. end
  321. unless embed
  322. embed = character_embed(
  323. char: char,
  324. img: img,
  325. section: :image,
  326. user: user,
  327. color: color
  328. )
  329. msg = event.send_embed("", embed)
  330. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  331. arrow_react(msg)
  332. end
  333. embed
  334. when name && char && section
  335. sect = section.downcase.to_sym
  336. nsfw = event.channel.nsfw?
  337. img = ImageController.img_scroll(
  338. char_id: char.id,
  339. nsfw: nsfw
  340. )if section == :image
  341. if sections.detect{ |s| s == sect }
  342. embed = character_embed(
  343. char: char,
  344. img: img,
  345. section: sect,
  346. user: user,
  347. color: color,
  348. )
  349. msg = event.send_embed("", embed)
  350. Carousel.create(
  351. message_id: msg.id,
  352. char_id: char.id,
  353. image_id: img ? img.id : nil
  354. )
  355. if sect == :image
  356. arrow_react(msg)
  357. else
  358. section_react(msg)
  359. end
  360. else
  361. error_embed("Invalid Section!")
  362. end
  363. end
  364. rescue ActiveRecord::RecordNotFound => e
  365. error_embed("Record Not Found!", e.message)
  366. end
  367. item = Command.new(:item, desc, opts) do |event, name|
  368. i = name ? Item.find_by!(name: name.capitalize) : Item.all
  369. case
  370. when name && i
  371. item_embed(i)
  372. when !name && i
  373. item_list_embed(i)
  374. else
  375. command_error_embed("Error proccessing your request!", item)
  376. end
  377. rescue ActiveRecord::RecordNotFound
  378. error_embed("Item Not Found!")
  379. end
  380. desc = "Add and remove items from characters' inventories"
  381. opts = { "item | (-/+) amount | character" => "" }
  382. inv = Command.new(:inv, desc, opts) do |event, item, amount, name|
  383. char = Character.find_by!(name: name) if name
  384. itm = Item.find_by!(name: item) if item
  385. amt = amount.to_i
  386. case
  387. when char && itm && amt
  388. i = Inventory.update_inv(itm, amt, char)
  389. user = event.server.member(char.user_id.to_i)
  390. color = CharacterController.type_color(char)
  391. case i
  392. when Inventory, true
  393. character_embed(char: char, user: user, color: color, section: :bags)
  394. when Embed
  395. i
  396. else
  397. error_embed("Something went wrong!", "Could not update inventory")
  398. end
  399. else
  400. command_error_embed("Could not proccess your request", inv)
  401. end
  402. rescue ActiveRecord::RecordNotFound => e
  403. error_embed(e.message)
  404. end
  405. desc = "Update or edit statuses"
  406. opts = { "name | effect" => "" }
  407. status = Command.new(:status, desc, opts) do |event, name, effect|
  408. if name && effect
  409. s = StatusController.edit_status(name, effect)
  410. case s
  411. when Status
  412. success_embed("Created Status: #{name}")
  413. when Embed
  414. s
  415. end
  416. else
  417. command_error_embed("Could not create status!", status)
  418. end
  419. end
  420. opts = { "character | ailment | %afflicted" => "" }
  421. afflict = Command.new(:afflict, desc, opts) do |event, name, status, amount|
  422. char = Character.find_by!(name: name) if name
  423. st = Status.find_by!(name: status) if status
  424. user = char.user_id.match(/public/i) ?
  425. 'Public' : event.server.member(char.user_id)
  426. if st && amount && char
  427. user = char.user_id.match(/public/i) ?
  428. 'Public' : event.server.member(char.user_id)
  429. color = CharacterController.type_color(char)
  430. s = StatusController.edit_char_status(st, amount, char)
  431. case s
  432. when CharStatus
  433. character_embed(char: char, user: user, color: color, section: :status)
  434. when Embed
  435. s
  436. end
  437. else
  438. command_error_embed("Error afflicting #{char}", afflict)
  439. end
  440. rescue ActiveRecord::RecordNotFound => e
  441. error_embed(e.message)
  442. end
  443. opts = {
  444. "character | all" => "completely cures all ailments",
  445. "character | ailment" => "completely cures the ailment",
  446. "character | ailment | %cured" => "cures a percentage of ailment"
  447. }
  448. cure = Command.new(:cure, desc, opts) do |event, name, status, amount|
  449. char = Character.find_by!(name: name) if name
  450. st = Status.find_by!(name: status) if status && !status.match(/all/i)
  451. case
  452. when char && st && amount
  453. user = char.user_id.match(/public/i) ?
  454. 'Public' : event.server.member(char.user_id)
  455. color = CharacterController.type_color(char)
  456. s = StatusController.edit_char_status(st, "-#{amount}", char)
  457. case s
  458. when CharStatus
  459. character_embed(char: char, user: user, color: color, section: :status)
  460. when Embed
  461. s
  462. end
  463. when char && st && !amount
  464. CharStatus.where(char_id: char.id).find_by!(status_id: st.id).delete
  465. success_embed("Removed #{status} from #{name}")
  466. when char && status && status.match(/all/i)
  467. csts = CharStatus.where(char_id: char.id)
  468. csts.each do |cst|
  469. cst.delete
  470. end
  471. success_embed("Removed all ailments from #{name}")
  472. else
  473. end
  474. rescue ActiveRecord::RecordNotFound => e
  475. error_embed(e.message)
  476. end
  477. # ---
  478. commands = [
  479. hello,
  480. matchup,
  481. app,
  482. help,
  483. poll,
  484. raffle,
  485. member,
  486. item,
  487. inv,
  488. status,
  489. afflict,
  490. cure
  491. ]
  492. locked_commands = [inv]
  493. # This will trigger on every message sent in discord
  494. bot.message do |event|
  495. content = event.message.content
  496. author = event.author.id
  497. command = /^pkmn-(\w+)/.match(content)
  498. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  499. reply = cmd.call(content, event) if cmd
  500. case reply
  501. when Embed
  502. event.send_embed("", reply)
  503. when String
  504. event.respond(reply)
  505. end
  506. event.send_embed(
  507. "",
  508. error_embed("Command not found!")
  509. )if command && !cmd && event.server
  510. if author == ENV['WEBHOOK'].to_i
  511. app = event.message.embeds.first
  512. if app.author.name == 'Character Application'
  513. Character.check_user(event)
  514. else
  515. approval_react(event)
  516. end
  517. end
  518. end
  519. pm_commands = [ image ]
  520. # This will trigger when a dm is sent to the bot from a user
  521. bot.pm do |event|
  522. content = event.message.content
  523. command = /^pkmn-(\w+)/.match(content)
  524. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  525. reply = cmd.call(content, event) if cmd
  526. case reply
  527. when Embed
  528. event.send_embed("", reply)
  529. when String
  530. event.respond(reply)
  531. end
  532. end
  533. # This will trigger when any reaction is added in discord
  534. bot.reaction_add do |event|
  535. reactions = event.message.reactions
  536. app = event.message.embeds.first
  537. carousel = Carousel.find_by(message_id: event.message.id)
  538. app = event.message.embeds.first
  539. carousel = Carousel.find_by(message_id: event.message.id)
  540. carousel = Carousel.find_by(message_id: event.message.id)
  541. maj =
  542. if event.server
  543. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  544. m.count / 2
  545. end
  546. maj = 1
  547. form =
  548. case app.author&.name
  549. when 'New App' then :new_app
  550. when 'Character Application' then :character_application
  551. when 'Character Rejection' then :character_rejection
  552. when 'Image Application' then :image_application
  553. when 'Image Rejection' then :image_rejection
  554. when 'Item Application' then :item_application
  555. when 'Item Rejection' then :item_rejection
  556. else
  557. :carousel if carousel
  558. end
  559. vote =
  560. case
  561. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  562. when reactions[Emoji::N]&.count.to_i > maj then :no
  563. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  564. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  565. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  566. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  567. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  568. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  569. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  570. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  571. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  572. when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  573. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  574. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  575. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  576. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  577. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  578. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  579. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  580. end
  581. case [form, vote]
  582. when [:character_application, :yes]
  583. uid = UID.match(app.description)
  584. user =
  585. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  586. char = CharacterController.edit_character(app)
  587. img = ImageController.default_image(
  588. app.thumbnail.url,
  589. char.id
  590. )if app.thumbnail
  591. color = CharacterController.type_color(char)
  592. embed = character_embed(
  593. char: char,
  594. img: img,
  595. user: user,
  596. color: color
  597. )if char
  598. if embed
  599. bot.send_message(
  600. ENV['CHAR_CH'].to_i,
  601. "Good news, #{uid}! Your character was approved",
  602. false,
  603. embed
  604. )
  605. event.message.delete
  606. else
  607. event.respond(
  608. "",
  609. admin_error_embed("Something went wrong when saving application")
  610. )
  611. end
  612. when [:character_application, :no]
  613. embed = reject_app_embed(app, :character)
  614. event.message.delete
  615. reject = event.send_embed("", embed)
  616. Emoji::CHAR_APP.each do |reaction|
  617. reject.react(reaction)
  618. end
  619. reject.react(Emoji::CHECK)
  620. reject.react(Emoji::CROSS)
  621. reject.react(Emoji::CRAYON)
  622. when [:character_rejection, :check]
  623. user = event.server.member(UID.match(app.description)[1])
  624. embed = user_char_app(event)
  625. event.message.delete
  626. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  627. bot.send_message(user.dm.id, "", false, embed)
  628. when [:character_rejection, :cross]
  629. event.message.delete
  630. when [:character_rejection, :crayon]
  631. event.message.delete
  632. bot.send_temporary_message(
  633. event.channel.id,
  634. "",
  635. 35,
  636. false,
  637. self_edit_embed(app, URL::CHARACTER)
  638. )
  639. when [:new_app, :phone]
  640. event.message.delete_own_reaction(Emoji::PHONE)
  641. user = event.message.reacted_with(Emoji::PHONE).first
  642. bot.send_message(user.dm.id, user.id, false, nil)
  643. when [:image_application, :yes]
  644. img = ImageController.edit_image(app)
  645. char = Character.find(img.char_id)
  646. user = event.server.member(char.user_id)
  647. color = CharacterController.type_color(char)
  648. embed = char_image_embed(char, img, user, color)
  649. event.message.delete if embed
  650. channel = if img.category == 'SFW'
  651. ENV['CHAR_CH'].to_i
  652. else
  653. ENV['CHAR_CH_NSFW'].to_i
  654. end
  655. bot.send_message(channel, "Image Approved!", false, embed)
  656. when [:image_application, :no]
  657. embed = reject_app_embed(app, :image)
  658. event.message.delete
  659. reject = event.send_embed("", embed)
  660. Emoji::IMG_APP.each do |reaction|
  661. reject.react(reaction)
  662. end
  663. reject.react(Emoji::CHECK)
  664. reject.react(Emoji::CROSS)
  665. when [:image_rejection, :check]
  666. user = event.server.member(UID.match(app.description)[1])
  667. embed = user_img_app(event)
  668. event.message.delete
  669. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  670. bot.send_message(user.dm.id, "", false, embed)
  671. when [:image_rejection, :cross]
  672. event.message.delete
  673. when [:item_application, :yes]
  674. item = ItemController.edit_item(app)
  675. embed = item_embed(item)
  676. event.message.delete
  677. bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  678. when [:item_application, :no]
  679. embed = reject_app_embed(app)
  680. event.message.delete
  681. reject = event.send_embed("", embed)
  682. reject.react(Emoji::CRAYON)
  683. reject.react(Emoji::CROSS)
  684. when [:item_rejection, :crayon]
  685. embed = self_edit_embed(app, Url::ITEM)
  686. event.message.delete
  687. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  688. when [:item_rejection, :cross]
  689. event.message.delete
  690. when [:carousel, :notebook]
  691. emoji = Emoji::NOTEBOOK
  692. users = event.message.reacted_with(emoji)
  693. users.each do |user|
  694. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  695. end
  696. char = Character.find(carousel.char_id)
  697. user =
  698. case
  699. when char.user_id.match(/public/i)
  700. "Public"
  701. when member = event.server.member(char.user_id)
  702. member
  703. else
  704. nil
  705. end
  706. embed = character_embed(
  707. char: char,
  708. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  709. user: user,
  710. color: CharacterController.type_color(char),
  711. section: :bio
  712. )
  713. event.message.edit("", embed)
  714. when [:carousel, :question]
  715. emoji = Emoji::QUESTION
  716. users = event.message.reacted_with(emoji)
  717. users.each do |user|
  718. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  719. end
  720. char = Character.find(carousel.char_id)
  721. user =
  722. case
  723. when char.user_id.match(/public/i)
  724. "Public"
  725. when member = event.server.member(char.user_id)
  726. member
  727. else
  728. nil
  729. end
  730. embed = character_embed(
  731. char: char,
  732. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  733. user: user,
  734. color: CharacterController.type_color(char),
  735. section: :status
  736. )
  737. event.message.edit("", embed)
  738. when [:carousel, :pallet]
  739. emoji = Emoji::PALLET
  740. users = event.message.reacted_with(emoji)
  741. users.each do |user|
  742. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  743. end
  744. char = Character.find(carousel.char_id)
  745. user =
  746. case
  747. when char.user_id.match(/public/i)
  748. "Public"
  749. when member = event.server.member(char.user_id)
  750. member
  751. else
  752. nil
  753. end
  754. embed = character_embed(
  755. char: char,
  756. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  757. user: user,
  758. color: CharacterController.type_color(char),
  759. section: :type
  760. )
  761. event.message.edit("", embed)
  762. when [:carousel, :ear]
  763. emoji = Emoji::EAR
  764. users = event.message.reacted_with(emoji)
  765. users.each do |user|
  766. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  767. end
  768. char = Character.find(carousel.char_id)
  769. user =
  770. case
  771. when char.user_id.match(/public/i)
  772. "Public"
  773. when member = event.server.member(char.user_id)
  774. member
  775. else
  776. nil
  777. end
  778. embed = character_embed(
  779. char: char,
  780. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  781. user: user,
  782. color: CharacterController.type_color(char),
  783. section: :rumors
  784. )
  785. event.message.edit("", embed)
  786. when [:carousel, :picture]
  787. event.message.delete_all_reactions
  788. char = Character.find(carousel.char_id)
  789. img = ImageController.img_scroll(
  790. char_id: char.id,
  791. nsfw: event.channel.nsfw?,
  792. )
  793. carousel.update(id: carousel.id, image_id: img.id)
  794. user =
  795. case
  796. when char.user_id.match(/public/i)
  797. "Public"
  798. when member = event.server.member(char.user_id)
  799. member
  800. else
  801. nil
  802. end
  803. embed = character_embed(
  804. char: char,
  805. img: img,
  806. user: user,
  807. color: CharacterController.type_color(char),
  808. section: :image
  809. )
  810. event.message.edit("", embed)
  811. arrow_react(event.message)
  812. when [:carousel, :bags]
  813. emoji = Emoji::BAGS
  814. users = event.message.reacted_with(emoji)
  815. users.each do |user|
  816. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  817. end
  818. char = Character.find(carousel.char_id)
  819. user =
  820. case
  821. when char.user_id.match(/public/i)
  822. "Public"
  823. when member = event.server.member(char.user_id)
  824. member
  825. else
  826. nil
  827. end
  828. embed = character_embed(
  829. char: char,
  830. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  831. user: user,
  832. color: CharacterController.type_color(char),
  833. section: :bags
  834. )
  835. event.message.edit("", embed)
  836. when [:carousel, :family]
  837. when [:carousel, :eyes]
  838. emoji = Emoji::EYES
  839. users = event.message.reacted_with(emoji)
  840. users.each do |user|
  841. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  842. end
  843. char = Character.find(carousel.char_id)
  844. user =
  845. case
  846. when char.user_id.match(/public/i)
  847. "Public"
  848. when member = event.server.member(char.user_id)
  849. member
  850. else
  851. nil
  852. end
  853. embed = character_embed(
  854. char: char,
  855. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  856. user: user,
  857. color: CharacterController.type_color(char),
  858. section: :all
  859. )
  860. event.message.edit("", embed)
  861. when [:carousel, :key]
  862. emoji = Emoji::KEY
  863. users = event.message.reacted_with(emoji)
  864. users.each do |user|
  865. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  866. end
  867. char = Character.find(carousel.char_id)
  868. user =
  869. case
  870. when char.user_id.match(/public/i)
  871. "Public"
  872. when member = event.server.member(char.user_id)
  873. member
  874. else
  875. nil
  876. end
  877. embed = character_embed(
  878. char: char,
  879. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  880. user: user,
  881. color: CharacterController.type_color(char),
  882. section: :default
  883. )
  884. event.message.edit("", embed)
  885. when [:carousel, :back]
  886. event.message.delete_all_reactions
  887. char = Character.find(carousel.char_id)
  888. user =
  889. case
  890. when char.user_id.match(/public/i)
  891. "Public"
  892. when member = event.server.member(char.user_id)
  893. member
  894. else
  895. nil
  896. end
  897. embed = character_embed(
  898. char: char,
  899. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  900. user: user,
  901. color: CharacterController.type_color(char),
  902. section: :default
  903. )
  904. event.message.edit("", embed)
  905. section_react(event.message)
  906. when [:carousel, :left], [:carousel, :right]
  907. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  908. users = event.message.reacted_with(emoji)
  909. users.each do |user|
  910. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  911. end
  912. char = Character.find(carousel.char_id)
  913. img = ImageController.img_scroll(
  914. char_id: char.id,
  915. nsfw: event.channel.nsfw?,
  916. img: carousel.image_id,
  917. dir: vote
  918. )
  919. carousel.update(id: carousel.id, image_id: img.id)
  920. user =
  921. case
  922. when char.user_id.match(/public/i)
  923. "Public"
  924. when member = event.server.member(char.user_id)
  925. member
  926. else
  927. nil
  928. end
  929. embed = character_embed(
  930. char: char,
  931. img: img,
  932. user: user,
  933. color: CharacterController.type_color(char),
  934. section: :image
  935. )
  936. event.message.edit("", embed)
  937. when [:carousel, :number]
  938. char_index = nil
  939. Emoji::NUMBERS.each.with_index do |emoji, i|
  940. char_index = i if reactions[emoji]&.count.to_i > 1
  941. end
  942. if char_index
  943. event.message.delete_all_reactions
  944. char = Character.find(carousel.options[char_index])
  945. carousel.update(id: carousel.id, char_id: char.id)
  946. user =
  947. case
  948. when char.user_id.match(/public/i)
  949. "Public"
  950. when member = event.server.member(char.user_id)
  951. member
  952. else
  953. nil
  954. end
  955. embed = character_embed(
  956. char: char,
  957. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  958. user: user,
  959. color: CharacterController.type_color(char),
  960. section: :default
  961. )
  962. event.message.edit("", embed)
  963. section_react(event.message)
  964. end
  965. when [:carousel, :cross]
  966. event.message.delete
  967. carousel.delete
  968. end
  969. end
  970. # This will trigger when any reaction is removed in discord
  971. bot.reaction_remove do |event|
  972. end
  973. # This will trigger when a member is updated
  974. bot.member_update do |event|
  975. end
  976. # This will trigger when anyone joins the server
  977. bot.member_join do |event|
  978. end
  979. # This will trigger when anyone leaves the server
  980. bot.member_leave do |event|
  981. end
  982. # This will trigger when anyone is banned from the server
  983. bot.user_ban do |event|
  984. end
  985. # This will trigger when anyone is un-banned from the server
  986. bot.user_unban do |event|
  987. end
  988. bot.run