bot.rb 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. desc = "Everything to do with rescue teams"
  478. opts = {
  479. "" => "list of teams",
  480. "team_name" => "display team info",
  481. "team_name | (leave/apply) | character" => "leave or apply for team",
  482. "team_name | create | description" => "admin only command"
  483. }
  484. team = Command.new(:team, desc, opts) do |event, team_name, action, desc|
  485. unless action&.match(/create/i)
  486. t = Team.find_by!(name: team_name) if team_name
  487. char = if event.author.roles.map(&:name).include?('Guild Masters')
  488. Character.find_by!(name: desc) if desc
  489. else
  490. c = Character.where(user_id: event.author.id).find_by!(name: desc)
  491. ct = CharTeam.where(char_id: c.id).find_by(active: true)
  492. action = "second_team" if ct && action.match(/apply/i)
  493. c
  494. end
  495. end
  496. case action
  497. when /leave/i
  498. ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  499. if ct
  500. ct.update(active: false)
  501. user = event.server.member(char.user_id.to_i)
  502. user.remove_role(t.role.to_i) if user
  503. end
  504. bot.send_message(
  505. t.channel.to_i,
  506. "#{char.name} has left the team",
  507. false,
  508. nil
  509. )
  510. when /apply/i
  511. embed = team_app_embed(t, char, event.server.member(char.user_id))
  512. msg = bot.send_message(t.channel.to_i, "", false, embed)
  513. msg.react(Emoji::Y)
  514. msg.react(Emoji::N)
  515. success_embed("Your request has been posted to #{t.name}!")
  516. when /create/i
  517. team_name = team_name || ""
  518. desc = desc || ""
  519. embed = new_team_embed(event.message.author, team_name, desc)
  520. msg = bot.send_message(ENV['APP_CH'], "", false, embed)
  521. msg.react(Emoji::Y)
  522. msg.react(Emoji::N)
  523. success_embed("Your Team Application has been submitted!")
  524. when nil
  525. t ? team_embed(t) : teams_embed()
  526. when /second_team/i
  527. error_embed("#{char.name} is already in a team!")
  528. else
  529. command_error_embed("Could not process team request!", team)
  530. end
  531. rescue ActiveRecord::RecordNotFound => e
  532. error_embed(e.message)
  533. end
  534. # ---
  535. commands = [
  536. hello,
  537. matchup,
  538. app,
  539. help,
  540. poll,
  541. raffle,
  542. member,
  543. item,
  544. inv,
  545. status,
  546. afflict,
  547. cure,
  548. team
  549. ]
  550. #locked_commands = [inv]
  551. # This will trigger on every message sent in discord
  552. bot.message do |event|
  553. content = event.message.content
  554. author = event.author.id
  555. command = /^pkmn-(\w+)/.match(content)
  556. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  557. reply = cmd.call(content, event) if cmd
  558. case reply
  559. when Embed
  560. event.send_embed("", reply)
  561. when String
  562. event.respond(reply)
  563. end
  564. event.send_embed(
  565. "",
  566. error_embed("Command not found!")
  567. )if command && !cmd && event.server
  568. if author == ENV['WEBHOOK'].to_i
  569. app = event.message.embeds.first
  570. if app.author.name == 'Character Application'
  571. Character.check_user(event)
  572. else
  573. approval_react(event)
  574. end
  575. end
  576. end
  577. pm_commands = [ image ]
  578. # This will trigger when a dm is sent to the bot from a user
  579. bot.pm do |event|
  580. content = event.message.content
  581. command = /^pkmn-(\w+)/.match(content)
  582. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  583. reply = cmd.call(content, event) if cmd
  584. case reply
  585. when Embed
  586. event.send_embed("", reply)
  587. when String
  588. event.respond(reply)
  589. end
  590. end
  591. # This will trigger when any reaction is added in discord
  592. bot.reaction_add do |event|
  593. reactions = event.message.reactions
  594. app = event.message.embeds.first
  595. carousel = Carousel.find_by(message_id: event.message.id)
  596. app = event.message.embeds.first
  597. carousel = Carousel.find_by(message_id: event.message.id)
  598. carousel = Carousel.find_by(message_id: event.message.id)
  599. maj =
  600. if event.server
  601. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  602. m.count / 2
  603. end
  604. maj = 1
  605. form =
  606. case app.author&.name
  607. when 'New App' then :new_app
  608. when 'Character Application' then :character_application
  609. when 'Character Rejection' then :character_rejection
  610. when 'Image Application' then :image_application
  611. when 'Image Rejection' then :image_rejection
  612. when 'Item Application' then :item_application
  613. when 'Item Rejection' then :item_rejection
  614. when 'Team Application' then :team_application
  615. when 'Team Join Request' then :team_request
  616. else
  617. :carousel if carousel
  618. end
  619. vote =
  620. case
  621. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  622. when reactions[Emoji::N]&.count.to_i > maj then :no
  623. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  624. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  625. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  626. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  627. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  628. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  629. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  630. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  631. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  632. #when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  633. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  634. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  635. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  636. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  637. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  638. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  639. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  640. end
  641. case [form, vote]
  642. when [:character_application, :yes]
  643. uid = UID.match(app.description)
  644. user =
  645. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  646. char = CharacterController.edit_character(app)
  647. img = ImageController.default_image(
  648. app.thumbnail.url,
  649. char.id
  650. )if app.thumbnail
  651. color = CharacterController.type_color(char)
  652. embed = character_embed(
  653. char: char,
  654. img: img,
  655. user: user,
  656. color: color
  657. )if char
  658. if embed
  659. bot.send_message(
  660. ENV['CHAR_CH'].to_i,
  661. "Good news, #{uid}! Your character was approved",
  662. false,
  663. embed
  664. )
  665. event.message.delete
  666. else
  667. event.respond(
  668. "",
  669. admin_error_embed("Something went wrong when saving application")
  670. )
  671. end
  672. when [:character_application, :no]
  673. embed = reject_app_embed(app, :character)
  674. event.message.delete
  675. reject = event.send_embed("", embed)
  676. Emoji::CHAR_APP.each do |reaction|
  677. reject.react(reaction)
  678. end
  679. reject.react(Emoji::CHECK)
  680. reject.react(Emoji::CROSS)
  681. reject.react(Emoji::CRAYON)
  682. when [:character_rejection, :check]
  683. user = event.server.member(UID.match(app.description)[1])
  684. embed = user_char_app(event)
  685. event.message.delete
  686. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  687. bot.send_message(user.dm.id, "", false, embed)
  688. when [:character_rejection, :cross]
  689. event.message.delete
  690. when [:character_rejection, :crayon]
  691. event.message.delete
  692. bot.send_temporary_message(
  693. event.channel.id,
  694. "",
  695. 35,
  696. false,
  697. self_edit_embed(app, URL::CHARACTER)
  698. )
  699. when [:new_app, :phone]
  700. event.message.delete_own_reaction(Emoji::PHONE)
  701. user = event.message.reacted_with(Emoji::PHONE).first
  702. bot.send_message(user.dm.id, user.id, false, nil)
  703. when [:image_application, :yes]
  704. img = ImageController.edit_image(app)
  705. char = Character.find(img.char_id)
  706. user = event.server.member(char.user_id)
  707. color = CharacterController.type_color(char)
  708. embed = char_image_embed(char, img, user, color)
  709. event.message.delete if embed
  710. channel = if img.category == 'SFW'
  711. ENV['CHAR_CH'].to_i
  712. else
  713. ENV['CHAR_CH_NSFW'].to_i
  714. end
  715. bot.send_message(channel, "Image Approved!", false, embed)
  716. when [:image_application, :no]
  717. embed = reject_app_embed(app, :image)
  718. event.message.delete
  719. reject = event.send_embed("", embed)
  720. Emoji::IMG_APP.each do |reaction|
  721. reject.react(reaction)
  722. end
  723. reject.react(Emoji::CHECK)
  724. reject.react(Emoji::CROSS)
  725. when [:image_rejection, :check]
  726. user = event.server.member(UID.match(app.description)[1])
  727. embed = user_img_app(event)
  728. event.message.delete
  729. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  730. bot.send_message(user.dm.id, "", false, embed)
  731. when [:image_rejection, :cross]
  732. event.message.delete
  733. when [:item_application, :yes]
  734. item = ItemController.edit_item(app)
  735. embed = item_embed(item)
  736. event.message.delete
  737. bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  738. when [:item_application, :no]
  739. embed = reject_app_embed(app)
  740. event.message.delete
  741. reject = event.send_embed("", embed)
  742. reject.react(Emoji::CRAYON)
  743. reject.react(Emoji::CROSS)
  744. when [:item_rejection, :crayon]
  745. embed = self_edit_embed(app, Url::ITEM)
  746. event.message.delete
  747. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  748. when [:item_rejection, :cross]
  749. event.message.delete
  750. when [:carousel, :notebook]
  751. emoji = Emoji::NOTEBOOK
  752. users = event.message.reacted_with(emoji)
  753. users.each do |user|
  754. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  755. end
  756. char = Character.find(carousel.char_id)
  757. user =
  758. case
  759. when char.user_id.match(/public/i)
  760. "Public"
  761. when member = event.server.member(char.user_id)
  762. member
  763. else
  764. nil
  765. end
  766. embed = character_embed(
  767. char: char,
  768. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  769. user: user,
  770. color: CharacterController.type_color(char),
  771. section: :bio
  772. )
  773. event.message.edit("", embed)
  774. when [:carousel, :question]
  775. emoji = Emoji::QUESTION
  776. users = event.message.reacted_with(emoji)
  777. users.each do |user|
  778. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  779. end
  780. char = Character.find(carousel.char_id)
  781. user =
  782. case
  783. when char.user_id.match(/public/i)
  784. "Public"
  785. when member = event.server.member(char.user_id)
  786. member
  787. else
  788. nil
  789. end
  790. embed = character_embed(
  791. char: char,
  792. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  793. user: user,
  794. color: CharacterController.type_color(char),
  795. section: :status
  796. )
  797. event.message.edit("", embed)
  798. when [:carousel, :pallet]
  799. emoji = Emoji::PALLET
  800. users = event.message.reacted_with(emoji)
  801. users.each do |user|
  802. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  803. end
  804. char = Character.find(carousel.char_id)
  805. user =
  806. case
  807. when char.user_id.match(/public/i)
  808. "Public"
  809. when member = event.server.member(char.user_id)
  810. member
  811. else
  812. nil
  813. end
  814. embed = character_embed(
  815. char: char,
  816. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  817. user: user,
  818. color: CharacterController.type_color(char),
  819. section: :type
  820. )
  821. event.message.edit("", embed)
  822. when [:carousel, :ear]
  823. emoji = Emoji::EAR
  824. users = event.message.reacted_with(emoji)
  825. users.each do |user|
  826. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  827. end
  828. char = Character.find(carousel.char_id)
  829. user =
  830. case
  831. when char.user_id.match(/public/i)
  832. "Public"
  833. when member = event.server.member(char.user_id)
  834. member
  835. else
  836. nil
  837. end
  838. embed = character_embed(
  839. char: char,
  840. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  841. user: user,
  842. color: CharacterController.type_color(char),
  843. section: :rumors
  844. )
  845. event.message.edit("", embed)
  846. when [:carousel, :picture]
  847. event.message.delete_all_reactions
  848. char = Character.find(carousel.char_id)
  849. img = ImageController.img_scroll(
  850. char_id: char.id,
  851. nsfw: event.channel.nsfw?,
  852. )
  853. carousel.update(id: carousel.id, image_id: img.id)
  854. user =
  855. case
  856. when char.user_id.match(/public/i)
  857. "Public"
  858. when member = event.server.member(char.user_id)
  859. member
  860. else
  861. nil
  862. end
  863. embed = character_embed(
  864. char: char,
  865. img: img,
  866. user: user,
  867. color: CharacterController.type_color(char),
  868. section: :image
  869. )
  870. event.message.edit("", embed)
  871. arrow_react(event.message)
  872. when [:carousel, :bags]
  873. emoji = Emoji::BAGS
  874. users = event.message.reacted_with(emoji)
  875. users.each do |user|
  876. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  877. end
  878. char = Character.find(carousel.char_id)
  879. user =
  880. case
  881. when char.user_id.match(/public/i)
  882. "Public"
  883. when member = event.server.member(char.user_id)
  884. member
  885. else
  886. nil
  887. end
  888. embed = character_embed(
  889. char: char,
  890. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  891. user: user,
  892. color: CharacterController.type_color(char),
  893. section: :bags
  894. )
  895. event.message.edit("", embed)
  896. when [:carousel, :family]
  897. when [:carousel, :eyes]
  898. emoji = Emoji::EYES
  899. users = event.message.reacted_with(emoji)
  900. users.each do |user|
  901. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  902. end
  903. char = Character.find(carousel.char_id)
  904. user =
  905. case
  906. when char.user_id.match(/public/i)
  907. "Public"
  908. when member = event.server.member(char.user_id)
  909. member
  910. else
  911. nil
  912. end
  913. embed = character_embed(
  914. char: char,
  915. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  916. user: user,
  917. color: CharacterController.type_color(char),
  918. section: :all
  919. )
  920. event.message.edit("", embed)
  921. when [:carousel, :key]
  922. emoji = Emoji::KEY
  923. users = event.message.reacted_with(emoji)
  924. users.each do |user|
  925. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  926. end
  927. char = Character.find(carousel.char_id)
  928. user =
  929. case
  930. when char.user_id.match(/public/i)
  931. "Public"
  932. when member = event.server.member(char.user_id)
  933. member
  934. else
  935. nil
  936. end
  937. embed = character_embed(
  938. char: char,
  939. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  940. user: user,
  941. color: CharacterController.type_color(char),
  942. section: :default
  943. )
  944. event.message.edit("", embed)
  945. when [:carousel, :back]
  946. event.message.delete_all_reactions
  947. char = Character.find(carousel.char_id)
  948. user =
  949. case
  950. when char.user_id.match(/public/i)
  951. "Public"
  952. when member = event.server.member(char.user_id)
  953. member
  954. else
  955. nil
  956. end
  957. embed = character_embed(
  958. char: char,
  959. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  960. user: user,
  961. color: CharacterController.type_color(char),
  962. section: :default
  963. )
  964. event.message.edit("", embed)
  965. section_react(event.message)
  966. when [:carousel, :left], [:carousel, :right]
  967. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  968. users = event.message.reacted_with(emoji)
  969. users.each do |user|
  970. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  971. end
  972. char = Character.find(carousel.char_id)
  973. img = ImageController.img_scroll(
  974. char_id: char.id,
  975. nsfw: event.channel.nsfw?,
  976. img: carousel.image_id,
  977. dir: vote
  978. )
  979. carousel.update(id: carousel.id, image_id: img.id)
  980. user =
  981. case
  982. when char.user_id.match(/public/i)
  983. "Public"
  984. when member = event.server.member(char.user_id)
  985. member
  986. else
  987. nil
  988. end
  989. embed = character_embed(
  990. char: char,
  991. img: img,
  992. user: user,
  993. color: CharacterController.type_color(char),
  994. section: :image
  995. )
  996. event.message.edit("", embed)
  997. when [:carousel, :number]
  998. char_index = nil
  999. Emoji::NUMBERS.each.with_index do |emoji, i|
  1000. char_index = i if reactions[emoji]&.count.to_i > 1
  1001. end
  1002. if char_index
  1003. event.message.delete_all_reactions
  1004. char = Character.find(carousel.options[char_index])
  1005. carousel.update(id: carousel.id, char_id: char.id)
  1006. user =
  1007. case
  1008. when char.user_id.match(/public/i)
  1009. "Public"
  1010. when member = event.server.member(char.user_id)
  1011. member
  1012. else
  1013. nil
  1014. end
  1015. embed = character_embed(
  1016. char: char,
  1017. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1018. user: user,
  1019. color: CharacterController.type_color(char),
  1020. section: :default
  1021. )
  1022. event.message.edit("", embed)
  1023. section_react(event.message)
  1024. end
  1025. when [:carousel, :cross]
  1026. event.message.delete
  1027. carousel.delete
  1028. when [:team_application, :yes]
  1029. t = Team.create!(name: app.title, description: app.description)
  1030. # create role
  1031. role = event.server.create_role(
  1032. name: t.name,
  1033. colour: 3447003,
  1034. hoist: true,
  1035. mentionable: true,
  1036. reason: "New Team"
  1037. )
  1038. #role.sort_above(ENV['TEAM_ROLE'])
  1039. # create channel
  1040. channel = event.server.create_channel(
  1041. t.name,
  1042. parent: 642055732321189928,
  1043. permission_overwrites: [
  1044. { id: event.server.everyone_role.id, deny: 1024 },
  1045. { id: role.id, allow: 1024 }
  1046. ]
  1047. )
  1048. t.update(role: role.id.to_s, channel: channel.id.to_s)
  1049. # embed
  1050. embed = message_embed(
  1051. "Team Approved: #{t.name}!",
  1052. "You can join with ```pkmn-team #{t.name} | apply | character_name```"
  1053. )
  1054. bot.send_message(ENV['TEAM_CH'], "", false, embed)
  1055. event.message.delete
  1056. when [:team_application, :no]
  1057. event.message.delete
  1058. when [:team_request, :yes]
  1059. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1060. char = Character.find(char_id[1].to_i)
  1061. t = Team.find_by(channel: event.message.channel.id.to_s)
  1062. event.message.delete
  1063. if ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  1064. ct.update(active: true)
  1065. else
  1066. CharTeam.create(char_id: char.id, team_id: t.id)
  1067. end
  1068. user = event.server.member(char.user_id)
  1069. user.add_role(t.role.to_i) if user
  1070. embed = message_embed("New Member!", "Welcome #{char.name} to the team!")
  1071. event.send_embed("", embed)
  1072. when [:team_request, :no]
  1073. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1074. char = Character.find(char_id[1])
  1075. t = Team.find_by(channel: event.message.channel.id.to_s)
  1076. bot.send_message(
  1077. ENV['TEAM_CH'],
  1078. "#{char.name} has been declined from team #{t.name}",
  1079. false,
  1080. nil
  1081. )
  1082. event.message.delete
  1083. end
  1084. end
  1085. # This will trigger when any reaction is removed in discord
  1086. bot.reaction_remove do |event|
  1087. end
  1088. # This will trigger when a member is updated
  1089. bot.member_update do |event|
  1090. end
  1091. # This will trigger when anyone joins the server
  1092. bot.member_join do |event|
  1093. end
  1094. # This will trigger when anyone leaves the server
  1095. bot.member_leave do |event|
  1096. end
  1097. # This will trigger when anyone is banned from the server
  1098. bot.user_ban do |event|
  1099. end
  1100. # This will trigger when anyone is un-banned from the server
  1101. bot.user_unban do |event|
  1102. end
  1103. bot.run