bot.rb 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. require 'bundler'
  2. require 'erb'
  3. require 'yaml'
  4. require 'json'
  5. require 'terminal-table'
  6. require 'rmagick'
  7. require 'down'
  8. BOT_ENV = ENV.fetch('BOT_ENV') { 'development' }
  9. Bundler.require(:default, BOT_ENV)
  10. require 'active_record'
  11. # Constants: such as roles and colors and regexes
  12. DISCORD = "#36393f"
  13. ERROR = "#a41e1f"
  14. UID = /<@\!?([0-9]+)>/
  15. URL = /https?:\/\/[\S]+/
  16. # ---
  17. Dotenv.load if BOT_ENV != 'production'
  18. db_yml = File.open('config/database.yml') do |erb|
  19. ERB.new(erb.read).result
  20. end
  21. db_config = YAML.safe_load(db_yml)[BOT_ENV]
  22. ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
  23. ActiveRecord::Base.establish_connection(
  24. adapter: 'postgresql',
  25. host: db_config.fetch('host') { 'localhost' },
  26. database: db_config['database'],
  27. user: db_config['user'],
  28. password: db_config['password']
  29. )
  30. Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
  31. Dir['./lib/*.rb'].each { |f| require f }
  32. token = ENV['DISCORD_BOT_TOKEN']
  33. bot = Discordrb::Bot.new(token: token)
  34. # Methods
  35. def stat_image(user, member, stats=nil)
  36. size_width = 570;
  37. size_height = 376;
  38. stats_frame = "images/LevelUp.png"
  39. level_up = "images/LevelUpFont.png"
  40. user_url_img = "images/Image_Builder/user_url_img.png"
  41. output_file = "images/Image_Builder/LevelUp"
  42. begin
  43. Down.download(member.avatar_url, destination: user_url_img)
  44. rescue Down::NotFound
  45. user_url_img = "images/Image_Builder/unknown_img.png"
  46. end
  47. #Gif Destroyer
  48. i = Magick::ImageList.new(user_url_img)
  49. i[0].write(user_url_img) if i.count > 1
  50. if stats
  51. merge_image(
  52. [stats_frame, level_up, user_url_img],
  53. output_file,
  54. size_width,
  55. size_height,
  56. [nil, nil, 19],
  57. [nil, nil, 92],
  58. [size_width, size_width, 165],
  59. [size_height, size_height, 165]
  60. )
  61. else
  62. merge_image(
  63. [stats_frame, user_url_img],
  64. output_file,
  65. size_width,
  66. size_height,
  67. [nil, 19],
  68. [nil, 92],
  69. [size_width, 165],
  70. [size_height, 165]
  71. )
  72. end
  73. last_level = user.level == 1 ? 0 : ((user.level + 4) **3) / 10.0
  74. this_level = user.next_level - last_level
  75. ratio = 1 - ((user.next_level - user.boosted_xp).to_f / this_level)
  76. user_name = member.nickname || member.name
  77. short_name = user_name.length > 15 ? "#{user_name[0..14]}..." : user_name
  78. rank = User.order(unboosted_xp: :desc)
  79. user_rank = rank.index{ |r| r.id == user.id } + 1
  80. gc = Draw.new
  81. gc.font('OpenSans-SemiBold.ttf')
  82. gc.stroke('#39c4ff').fill('#39c4ff')
  83. gc.rectangle(42, 48, 42 + (95 * ratio), 48 + 3)
  84. gc.stroke('none').fill('black')
  85. gc.pointsize('15')
  86. gc.text(15,25, short_name)
  87. gc.text(40, 45, "Lv.#{user.level}")
  88. gc.text(15, 290, "Rank: #{user_rank}")
  89. gc.text(40, 65, "Exp: #{user.boosted_xp}")
  90. gc.stroke('white').fill('white')
  91. gc.pointsize('30')
  92. gc.text(40,330, user_name)
  93. gc.text(40,360, "reached level #{user.level}!") if stats
  94. gc.text(40,360, "is level #{user.level}!") if !stats
  95. if stats
  96. gc.stroke('none').fill('black')
  97. gc.pointsize('18')
  98. gc.text(450, 97, stats['hp'].to_s)
  99. gc.text(450, 127, stats['attack'].to_s)
  100. gc.text(450, 159, stats['defense'].to_s)
  101. gc.text(450, 191, stats['sp_attack'].to_s)
  102. gc.text(450, 222, stats['sp_defense'].to_s)
  103. gc.text(450, 255, stats['speed'].to_s)
  104. gc.stroke('none').fill('maroon')
  105. gc.text(505, 97, "+ #{stats['hp'] - user.hp}")
  106. gc.text(505, 127, "+ #{stats['attack']- user.attack}")
  107. gc.text(505, 159, "+ #{stats['defense'] - user.defense}")
  108. gc.text(505, 191, "+ #{stats['sp_attack'] - user.sp_attack}")
  109. gc.text(505, 222, "+ #{stats['sp_defense']- user.sp_defense}")
  110. gc.text(505, 255, "+ #{stats['speed'] - user.speed}")
  111. else
  112. gc.stroke('none').fill('black')
  113. gc.pointsize('18')
  114. gc.text(450, 97, user.hp.to_s)
  115. gc.text(450, 127, user.attack.to_s)
  116. gc.text(450, 159, user.defense.to_s)
  117. gc.text(450, 191, user.sp_attack.to_s)
  118. gc.text(450, 222, user.sp_defense.to_s)
  119. gc.text(450, 255, user.speed.to_s)
  120. end
  121. u = Magick::ImageList.new("#{output_file}.png")
  122. gc.draw(u[0])
  123. u.write("#{output_file}.png")
  124. "#{output_file}.png"
  125. end
  126. #--
  127. # Commands: structure basic bot commands here
  128. commands = []
  129. pm_commands = []
  130. hello = Command.new(:hello, "Says hello!") do |event|
  131. user = event.author.nickname || event.author.name
  132. img = ImageUrl.find_by(name: 'happy')
  133. greetings = [
  134. "Hi there, #{user}",
  135. "Greetings #{user}, you lovable bum",
  136. "Alola, #{user}",
  137. "Hello, #{user}! The Guildmasters have been waiting",
  138. "#{user} would like to battle!"
  139. ]
  140. Embed.new(
  141. description: greetings.sample,
  142. color: event.author&.color&.combined,
  143. thumbnail: {
  144. url: img.url
  145. }
  146. )
  147. end
  148. opts = {
  149. "" => "displays a list of all commands",
  150. "command" => "displays info and usage for specified command"
  151. }
  152. desc = "Displays help information for the commands"
  153. help = Command.new(:help, desc, opts) do |event, command|
  154. if command
  155. short = /pkmn-(\w+)/.match(command)
  156. command = short ? short[1] : command
  157. cmd = commands.detect { |c| c.name == command.to_sym }
  158. pm_cmd = pm_commands.detect { |pc| pc.name == command.to_sym }
  159. end
  160. if command && cmd
  161. command_embed(cmd)
  162. elsif command && pm_cmd
  163. command_embed(pm_cmd, "PM Command")
  164. elsif !command
  165. embed = command_list_embed(
  166. pm_commands,
  167. "Can only be used in a pm with the bot",
  168. "PM Commands"
  169. )
  170. event.send_embed("", embed)
  171. command_list_embed(commands)
  172. else
  173. command_error_embed("Command not found!", help)
  174. end
  175. end
  176. opts = {
  177. "primary" => "Single Display",
  178. "primary | secondary" => "Double Display"
  179. }
  180. desc = "Displays a chart of effectiveness for the given type"
  181. matchup = Command.new(:matchup, desc, opts) do |event, primary, secondary|
  182. channel = event.channel.id
  183. image_out = 'images/Type Double.png'
  184. file_p = "images/Type #{primary.capitalize}.png" if primary
  185. file_s = "images/Type #{secondary.capitalize}.png" if secondary
  186. case
  187. when !file_p
  188. command_error_embed("There was an error processing your request!", matchup)
  189. when !file_s && File.exists?(file_p)
  190. bot.send_file(channel, File.open(file_p, 'r'))
  191. when File.exists?(file_p) && File.exists?(file_s)
  192. append_image(file_p, file_s, image_out)
  193. bot.send_file(channel, File.open(image_out, 'r'))
  194. else
  195. error_embed("Type(s) not found!")
  196. end
  197. end
  198. opts = {
  199. "@user" => "List all user stats",
  200. }
  201. desc = "Shows ones stats, level, rank, and experience"
  202. stats = Command.new(:stats, desc, opts) do |event, name, all|
  203. case name
  204. when UID
  205. user_id = UID.match(name)
  206. user = event.server.member(user_id[1])
  207. when String
  208. #See if you can find the name some other way?
  209. end
  210. usr = User.find_by!(id: user&.id)
  211. case all
  212. when /all/i
  213. show_stats(usr, user)
  214. when /reroll/i
  215. usr.make_stats
  216. usr.reload
  217. show_stats(usr, user)
  218. else
  219. output_file = stat_image(usr, user)
  220. bot.send_file(event.channel.id, File.open(output_file, 'r'))
  221. end
  222. rescue ActiveRecord::RecordNotFound => e
  223. error_embed(e.message)
  224. end
  225. opts = {
  226. "" => "starts a new app",
  227. "name" => "edits an existing app",
  228. "name | (in)active" => "sets app to active or inactive"
  229. }
  230. desc = "Everything to do with character applications"
  231. app = Command.new(:app, desc, opts) do |event, name, status|
  232. user = event.author
  233. user_name = user.nickname || user.name
  234. color = user.color.combined if event.server && user.color
  235. chars = []
  236. character =
  237. if user.roles.map(&:name).include?('Guild Masters')
  238. chars = Character.where(name: name)
  239. chars.first if chars.length == 1
  240. else
  241. Character.where(user_id: user.id).find_by(name: name) if name
  242. end
  243. active = status.match(/(in)?active/i) if status
  244. case
  245. when !chars.empty? && !character
  246. chars.each do |char|
  247. edit_url = Url::CHARACTER + char.edit_url
  248. embed = edit_app_dm(name, edit_url, color)
  249. bot.send_message(
  250. user.dm.id,
  251. "<@#{char.user_id}>'s character:",
  252. false,
  253. embed
  254. )
  255. end
  256. when name && !character
  257. app_not_found_embed(user_name, name)
  258. when status && active && character
  259. if active[1].nil?
  260. uid = character.user_id
  261. user_allowed = (User.find_by(id: uid).level / 10) + 1
  262. user_allowed = user_allowed + 1 if user.roles.map(&:name).include?('Nitro Booster')
  263. active_chars = Character.where(user_id: uid, active: 'Active')
  264. allowed = active_chars.count < user_allowed && character.active == 'Inactive'
  265. else
  266. allowed = true
  267. end
  268. if allowed
  269. character.update!(active: active[0].capitalize)
  270. character.reload
  271. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  272. else
  273. error_embed(
  274. "You're not allowed to do that!",
  275. "You either have too many active characters, the character is already active, or it is an NPC"
  276. )
  277. end
  278. when name && character && !status
  279. edit_url = Url::CHARACTER + character.edit_url
  280. embed = edit_app_dm(name, edit_url, color)
  281. bot.send_message(user.dm.id, "", false, embed)
  282. edit_app_embed(user_name, name, color) if event.server
  283. when !name && !status
  284. embed = new_app_dm(user_name, user.id, color)
  285. message = bot.send_message(user.dm.id, "", false, embed)
  286. message.react(Emoji::PHONE)
  287. new_app_embed(user_name, color) if event.server
  288. else
  289. command_error_embed("There was an error processing your application!", app)
  290. end
  291. end
  292. opts = { "question | option1, option2, etc" => ""}
  293. desc = "Creates a dynamic poll in any channel"
  294. poll = Command.new(:poll, desc, opts) do |event, question, options|
  295. if options
  296. options_array = options.split(/\s?,\s?/)
  297. new_poll_embed(event, question, options_array)
  298. else
  299. command_error_embed("There was an error creating your poll!", poll)
  300. end
  301. end
  302. opts = {
  303. "participants" =>
  304. "Accepts Everyone, Here, or a comma seperated list of names"
  305. }
  306. desc = "Creates a raffle and picks a winner"
  307. raffle = Command.new(:raffle, desc, opts) do |event, participant|
  308. participants =
  309. case participant
  310. when /^everyone$/i
  311. event.server.members
  312. when /^here$/i
  313. event.message.channel.users
  314. else
  315. participant.split(/\s?,\s?/)
  316. end
  317. winner = participants.sample
  318. winner_name =
  319. case winner
  320. when String
  321. winner
  322. else
  323. winner.nickname || winner.username
  324. end
  325. if winner_name
  326. message_embed("Raffle Results!", "Winner: #{winner_name}")
  327. else
  328. command_error_embed("There was an error creating your raffle!", raffle)
  329. end
  330. end
  331. opts = {
  332. "name | keyword | (n)sfw | url" => "add or update image",
  333. "name | keyword | delete" => "remove image",
  334. "name | keyword" => "display image",
  335. "name" => "list all images"
  336. }
  337. desc = "View, add and edit your characters' images"
  338. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url, id|
  339. user = event.author
  340. char =
  341. if id
  342. Character.where(user_id: id).find_by!(name: name) if name
  343. else
  344. Character.where(user_id: user.id).find_by!(name: name) if name
  345. end
  346. color = CharacterController.type_color(char) if char
  347. img = CharImage.where(char_id: char.id).find_by(keyword: keyword) if keyword
  348. case
  349. when /^Default$/i.match(keyword)
  350. error_embed(
  351. "Cannot update Default here!",
  352. "Use `pkmn-app character` to edit your default image in your form"
  353. )
  354. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  355. img_app = CharImage.to_form(
  356. char: char,
  357. keyword: keyword,
  358. category: tag,
  359. url: url,
  360. user_id: user.id
  361. )
  362. approval = bot.send_message(ENV['APP_CH'].to_i, "", false, img_app)
  363. approval.react(Emoji::Y)
  364. approval.react(Emoji::N)
  365. success_embed("Your image has been submitted for approval!")
  366. when char && img && tag && tag.match(/delete/i)
  367. CharImage.find(img.id).delete
  368. success_embed("Removed image: #{keyword}")
  369. when char && img && !tag
  370. char_image_embed(char, img, user, color)
  371. when char && !keyword
  372. imgs = CharImage.where(char_id: char.id)
  373. image_list_embed(char, imgs, user, color)
  374. when keyword && !img
  375. error_embed("Could not find your image!")
  376. else
  377. command_error_embed("Could not process your image request!", image)
  378. end
  379. rescue ActiveRecord::RecordNotFound
  380. error_embed(
  381. "Character not Found!",
  382. "I could not find your character named #{name}"
  383. )
  384. end
  385. opts = {
  386. "" => "List all guild members",
  387. "@user" => "List all characters belonging to the user",
  388. "name " => "Display the given character",
  389. "name | section" => "Display the given section for the character",
  390. "name | image | keword" => "Display the given image"
  391. }
  392. desc = "Display info about the guild members"
  393. member = Command.new(:member, desc, opts) do |event, name, section, keyword|
  394. sections = [:all, :default, :bio, :type, :status, :rumors, :image, :bags]
  395. case name
  396. when UID
  397. user_id = UID.match(name)
  398. when String
  399. chars = Character.where(name: name)
  400. unless event.channel.nsfw?
  401. chars = chars.reject { |c| c['rating'] == 'NSFW' }
  402. end
  403. char = chars.first if chars.length == 1
  404. if char
  405. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  406. user = case char.user_id
  407. when /public/i, /server/i
  408. char.user_id
  409. else
  410. event.server.member(char.user_id)
  411. end
  412. color = CharacterController.type_color(char)
  413. end
  414. end
  415. case
  416. when !name
  417. chars = Character.all
  418. char_list_embed(chars)
  419. when name && user_id
  420. chars = Character.where(user_id: user_id[1])
  421. user = event.server.member(user_id[1])
  422. chars_id = []
  423. chars.each do |char|
  424. chars_id.push char.id if char.active == 'Active'
  425. end
  426. embed = user_char_embed(chars, user)
  427. msg = event.send_embed("", embed)
  428. Carousel.create(message_id: msg.id, options: chars_id)
  429. option_react(msg, chars_id)
  430. when name && chars.empty?
  431. error_embed(
  432. "Character not found!",
  433. "Could not find a character named #{name}"
  434. )
  435. when name && chars && !char
  436. embed = dup_char_embed(chars, name)
  437. chars_id = chars.map(&:id)
  438. msg = event.send_embed("", embed)
  439. Carousel.create(message_id: msg.id, options: chars_id)
  440. option_react(msg, chars_id)
  441. when name && char && !section
  442. if char.rating == 'NSFW' && !event.channel.nsfw?
  443. embed = error_embed(
  444. "Wrong Channel!",
  445. "The requested character is NSFW"
  446. )
  447. else
  448. embed = character_embed(
  449. char: char,
  450. img: img,
  451. section: :default,
  452. user: user,
  453. color: color,
  454. event: event
  455. )
  456. msg = event.send_embed("", embed)
  457. Carousel.create(message_id: msg.id, char_id: char.id)
  458. section_react(msg)
  459. end
  460. when char && section && keyword
  461. img = CharImage.where(char_id: char.id).find_by!(keyword: keyword)
  462. if img.category == 'NSFW' && !event.channel.nsfw?
  463. embed = error_embed(
  464. "Wrong Channel!",
  465. "The requested image is NSFW"
  466. )
  467. elsif !/image/i.match(section)
  468. embed = command_error_embed(
  469. "Invalid Arguments",
  470. member
  471. )
  472. else
  473. embed = character_embed(
  474. char: char,
  475. img: img,
  476. section: :image,
  477. user: user,
  478. color: color,
  479. event: event
  480. )
  481. msg = event.send_embed("", embed)
  482. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  483. arrow_react(msg)
  484. end
  485. when name && char && section
  486. sect = section.downcase.to_sym
  487. nsfw = event.channel.nsfw?
  488. img = ImageController.img_scroll(
  489. char_id: char.id,
  490. nsfw: nsfw
  491. )if section == :image
  492. if char.rating == 'NSFW' && !event.channel.nsfw?
  493. embed = error_embed(
  494. "Wrong Channel!",
  495. "The requested character is NSFW"
  496. )
  497. elsif sections.detect{ |s| s == sect }
  498. embed = character_embed(
  499. char: char,
  500. img: img,
  501. section: sect,
  502. user: user,
  503. color: color,
  504. event: event
  505. )
  506. msg = event.send_embed("", embed)
  507. Carousel.create(
  508. message_id: msg.id,
  509. char_id: char.id,
  510. image_id: img ? img.id : nil
  511. )
  512. if sect == :image
  513. arrow_react(msg)
  514. else
  515. section_react(msg)
  516. end
  517. else
  518. error_embed("Invalid Section!")
  519. end
  520. end
  521. rescue ActiveRecord::RecordNotFound => e
  522. error_embed("Record Not Found!", e.message)
  523. end
  524. desc = "Learn about Items"
  525. opts = { "" => "list all items", "item_name" => "show known info for the item" }
  526. item = Command.new(:item, desc, opts) do |event, name|
  527. i = name ? Item.find_by!(name: name.capitalize) : Item.all
  528. case
  529. when name && i
  530. item_embed(i)
  531. when !name && i
  532. item_list_embed(i)
  533. #else
  534. #command_error_embed("Error proccessing your request!", item)
  535. end
  536. #rescue ActiveRecord::RecordNotFound
  537. #error_embed("Item Not Found!")
  538. end
  539. desc = "Add and remove items from characters' inventories"
  540. opts = { "item | (-/+) amount | character" => "negative numbers remove items" }
  541. inv = Command.new(:inv, desc, opts) do |event, item, amount, name|
  542. char = Character.find_by!(name: name) if name
  543. itm = Item.find_by!(name: item) if item
  544. amt = amount.to_i
  545. if char && itm && amt && event.user.roles.map(&:name).include?('Guild Masters')
  546. i = Inventory.update_inv(itm, amt, char)
  547. user = event.server.member(char.user_id.to_i)
  548. color = CharacterController.type_color(char)
  549. case i
  550. when Inventory, true
  551. character_embed(char: char, user: user, color: color, section: :bags, event: event)
  552. when Embed
  553. i
  554. else
  555. error_embed("Something went wrong!", "Could not update inventory")
  556. end
  557. elsif !event.user.roles.map(&:name).include?('Guild Masters')
  558. error_embed("You don't have permission to do that!")
  559. else
  560. command_error_embed("Could not proccess your request", inv)
  561. end
  562. rescue ActiveRecord::RecordNotFound => e
  563. error_embed(e.message)
  564. end
  565. desc = "Update or edit statuses"
  566. opts = { "name | effect" => "effect displays on user when afflicted" }
  567. status = Command.new(:status, desc, opts) do |event, name, effect, flag|
  568. admin = event.user.roles.map(&:name).include?('Guild Masters')
  569. if name && effect && admin
  570. s = StatusController.edit_status(name, effect, flag)
  571. case s
  572. when Status
  573. status_details(s)
  574. when Embed
  575. s
  576. end
  577. elsif name && !effect
  578. status_details(Status.find_by!(name: name))
  579. elsif !name && !effect
  580. status_list
  581. elsif !admin
  582. error_embed("You do not have permission to do that!")
  583. else
  584. command_error_embed("Could not create status!", status)
  585. end
  586. rescue ActiveRecord::RecordNotFound => e
  587. error_embed(e.message)
  588. end
  589. opts = { "character | ailment | %afflicted" => "afflictions apply in percentages" }
  590. afflict = Command.new(:afflict, desc, opts) do |event, name, status, amount|
  591. char = Character.find_by!(name: name) if name
  592. st = Status.find_by!(name: status) if status
  593. user = char.user_id.match(/public/i) ?
  594. 'Public' : event.server.member(char.user_id)
  595. if st && char
  596. user = char.user_id.match(/public/i) ?
  597. 'Public' : event.server.member(char.user_id)
  598. color = CharacterController.type_color(char)
  599. s = StatusController.edit_char_status(char, st, amount)
  600. case s
  601. when CharStatus
  602. character_embed(char: char, user: user, color: color, section: :status, event: event)
  603. when Embed
  604. s
  605. end
  606. else
  607. command_error_embed("Error afflicting #{char.name}", afflict)
  608. end
  609. rescue ActiveRecord::RecordNotFound => e
  610. error_embed(e.message)
  611. end
  612. opts = {
  613. "character | all" => "completely cures all ailments",
  614. "character | ailment" => "completely cures the ailment",
  615. "character | ailment | %cured" => "cures a percentage of ailment"
  616. }
  617. cure = Command.new(:cure, desc, opts) do |event, name, status, amount|
  618. char = Character.find_by!(name: name) if name
  619. st = Status.find_by!(name: status) if status && !status.match(/all/i)
  620. case
  621. when char && st && amount
  622. user = char.user_id.match(/public/i) ?
  623. 'Public' : event.server.member(char.user_id)
  624. color = CharacterController.type_color(char)
  625. s = StatusController.edit_char_status(st, "-#{amount}", char)
  626. case s
  627. when CharStatus
  628. character_embed(char: char, user: user, color: color, section: :status, event: event)
  629. when Embed
  630. s
  631. end
  632. when char && st && !amount
  633. CharStatus.where(char_id: char.id).find_by!(status_id: st.id).delete
  634. success_embed("Removed #{status} from #{name}")
  635. when char && status && status.match(/all/i)
  636. csts = CharStatus.where(char_id: char.id)
  637. csts.each do |cst|
  638. cst.delete
  639. end
  640. success_embed("Removed all ailments from #{name}")
  641. else
  642. end
  643. rescue ActiveRecord::RecordNotFound => e
  644. error_embed(e.message)
  645. end
  646. desc = "Everything to do with rescue teams"
  647. opts = {
  648. "" => "list of teams",
  649. "team_name" => "display team info",
  650. "team_name | (leave/apply) | character" => "leave or apply for team",
  651. "team_name | create | description" => "admin only command"
  652. }
  653. team = Command.new(:team, desc, opts) do |event, team_name, action, desc|
  654. unless action&.match(/create/i)
  655. t = Team.find_by!(name: team_name) if team_name
  656. char = if event.author.roles.map(&:name).include?('Guild Masters')
  657. Character.find_by!(name: desc) if desc
  658. else
  659. c = Character.where(user_id: event.author.id).find_by!(name: desc) if desc
  660. ct = CharTeam.where(char_id: c.id).find_by(active: true) if c
  661. action = "second_team" if ct && action.match(/apply/i)
  662. c
  663. end
  664. end
  665. case action
  666. when /leave/i
  667. ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  668. if ct
  669. ct.update(active: false)
  670. user = event.server.member(char.user_id.to_i)
  671. sql = "SELECT name " +
  672. "FROM char_teams JOIN characters " +
  673. "ON char_teams.char_id = characters.id " +
  674. "WHERE characters.user_id = ? " +
  675. "AND char_teams.team_id = ? " +
  676. "and char_teams.active = true;"
  677. sql =
  678. ActiveRecord::Base.send(:sanitize_sql_array, [sql, user.id.to_s, t.id])
  679. user_characters_team = ActiveRecord::Base.connection.exec_query(sql)
  680. user.remove_role(t.role.to_i) if user_characters_team.count == 0
  681. bot.send_message(
  682. t.channel.to_i,
  683. "#{char.name} has left the team",
  684. false,
  685. nil
  686. )
  687. end
  688. when /apply/i
  689. members = CharTeam.where(team_id: t.id)
  690. if members.count < 6
  691. embed = team_app_embed(t, char, event.server.member(char.user_id))
  692. msg = bot.send_message(t.channel.to_i, "", false, embed)
  693. msg.react(Emoji::Y)
  694. msg.react(Emoji::N)
  695. success_embed("Your request has been posted to #{t.name}!")
  696. else
  697. error_embed("#{t.name} is full!")
  698. end
  699. when /create/i
  700. team_name = team_name || ""
  701. desc = desc || ""
  702. embed = new_team_embed(event.message.author, team_name, desc)
  703. msg = bot.send_message(ENV['APP_CH'], "", false, embed)
  704. msg.react(Emoji::Y)
  705. msg.react(Emoji::N)
  706. success_embed("Your Team Application has been submitted!")
  707. when nil
  708. t ? team_embed(t) : teams_embed()
  709. when /second_team/i
  710. error_embed("#{char.name} is already in a team!")
  711. else
  712. command_error_embed("Could not process team request!", team)
  713. end
  714. rescue ActiveRecord::RecordNotFound => e
  715. error_embed(e.message)
  716. end
  717. desc = 'roll or create dice'
  718. opts = {
  719. '' => 'shows die options',
  720. 'xdy' => 'rolls x number of y sided die',
  721. 'a,b,c,d' => 'rolls the between the provided options',
  722. 'die_name' => 'rolls the given die',
  723. 'die_name | a,b,c,d' => 'creates the die'
  724. }
  725. roll = Command.new(:roll, desc, opts) do |event, die, array|
  726. usr = event.message.author
  727. usr_name = usr.nickname || usr.name
  728. color = usr.color&.combined if event.server
  729. case die
  730. when /([0-9]*?)d([0-9]+)/i
  731. result = DiceController.roll(die)
  732. when /,/
  733. result = DiceController.roll(die.split(/\s?,\s?/))
  734. when nil
  735. result = dice_embed
  736. else
  737. a = usr.roles.map(&:name).include?('Guild Master')
  738. d = DieArray.find_by(name: die.capitalize)
  739. if !d && array && a
  740. result =
  741. DieArray.create(name: die.capitalize, sides: array.split(/\s?,\s?/))
  742. elsif d && array && a
  743. d.update(sides: array.split(/\s?,\s?/))
  744. d.reload
  745. result = d
  746. elsif d && !array
  747. result = DiceController.roll(d.sides)
  748. else
  749. result = error_embed('Die not found!')
  750. end
  751. end
  752. case result
  753. when String, Integer
  754. Embed.new(description: "#{usr_name} rolled #{result}!", color: color)
  755. when DieArray
  756. success_embed("Created Die, #{die}, with sides: #{result.sides}")
  757. when Embed
  758. result
  759. end
  760. end
  761. # ---
  762. commands = [
  763. hello,
  764. matchup,
  765. app,
  766. help,
  767. poll,
  768. raffle,
  769. member,
  770. item,
  771. inv,
  772. status,
  773. afflict,
  774. cure,
  775. team,
  776. stats,
  777. roll
  778. ]
  779. #locked_commands = [inv]
  780. # This will trigger on every message sent in discord
  781. bot.message do |event|
  782. content = event.message.content
  783. author = event.author.id
  784. command = /^pkmn-(\w+)/.match(content)
  785. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  786. if cmd
  787. reply = cmd.call(content, event)
  788. case reply
  789. when Embed
  790. event.send_embed("", reply)
  791. when String
  792. event.respond(reply)
  793. end
  794. elsif command && !cmd && event.server
  795. event.send_embed(
  796. "",
  797. error_embed("Command not found!")
  798. )
  799. elsif author == ENV['WEBHOOK'].to_i
  800. app = event.message.embeds.first
  801. if app.author.name == 'Character Application'
  802. Character.check_user(event)
  803. else
  804. approval_react(event)
  805. end
  806. elsif event.message.channel == 454082477192118275 || event.message.channel == 613365750383640584
  807. elsif !event.author.bot_account? && !event.author.webhook?
  808. usr = User.find_by(id: author.to_s)
  809. msg = URL.match(content) ? content.gsub(URL, "x" * 150) : content
  810. file = event.message.attachments.map(&:filename).count
  811. msg = file > 0 ? msg + ("x" * 40) : msg
  812. img = usr.update_xp(msg, event.author)
  813. bot.send_file(event.message.channel, File.open(img, 'r')) if img
  814. end
  815. end
  816. pm_commands = [ image ]
  817. # This will trigger when a dm is sent to the bot from a user
  818. bot.pm do |event|
  819. content = event.message.content
  820. command = /^pkmn-(\w+)/.match(content)
  821. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  822. reply = cmd.call(content, event) if cmd
  823. case reply
  824. when Embed
  825. event.send_embed("", reply)
  826. when String
  827. event.respond(reply)
  828. end
  829. end
  830. # This will trigger when any reaction is added in discord
  831. bot.reaction_add do |event|
  832. reactions = event.message.reactions
  833. app = event.message.embeds.first
  834. carousel = Carousel.find_by(message_id: event.message.id)
  835. app = event.message.embeds.first
  836. carousel = Carousel.find_by(message_id: event.message.id)
  837. carousel = Carousel.find_by(message_id: event.message.id)
  838. maj = 100
  839. form =
  840. case app&.author&.name
  841. when 'New App' then :new_app
  842. when 'Character Application'
  843. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  844. maj = m.count > 2 ? m.count/2.0 : 2
  845. :character_application
  846. when 'Character Rejection' then :character_rejection
  847. when 'Image Application'
  848. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  849. maj = m.count > 2 ? m.count/2.0 : 2
  850. :image_application
  851. when 'Image Rejection' then :image_rejection
  852. when 'Item Application'
  853. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  854. maj = m.count > 2 ? m.count/2.0 : 2
  855. :item_application
  856. when 'Item Rejection' then :item_rejection
  857. when 'Team Application'
  858. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  859. maj = m.count > 2 ? m.count/2.0 : 2
  860. :team_application
  861. when 'Team Join Request'
  862. team_id = Team.find_by(channel: event.message.channel.id.to_s).role.to_i
  863. m = event.server.roles.find{ |r| r.id == team_id }.members
  864. maj = m.count > 2 ? m.count/2.0 : 2
  865. :team_request
  866. else
  867. if event.server == nil
  868. :new_app
  869. elsif carousel
  870. :carousel
  871. end
  872. end
  873. vote =
  874. case
  875. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  876. when reactions[Emoji::N]&.count.to_i > maj then :no
  877. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  878. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  879. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  880. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  881. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  882. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  883. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  884. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  885. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  886. #when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  887. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  888. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  889. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  890. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  891. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  892. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  893. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  894. end
  895. case [form, vote]
  896. when [:character_application, :yes]
  897. uid = UID.match(app.description)
  898. user =
  899. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  900. img_url = case
  901. when !app.thumbnail&.url.nil? then app.thumbnail.url
  902. when !app.image&.url.nil? then app.image.url
  903. end
  904. char = CharacterController.edit_character(app)
  905. img = ImageController.default_image(
  906. img_url,
  907. char.id
  908. )if img_url
  909. color = CharacterController.type_color(char)
  910. channel = case char.rating
  911. when /nsfw/i
  912. ENV['CHAR_NSFW_CH']
  913. when /hidden/i
  914. user.dm&.id
  915. else
  916. ENV['CHAR_CH']
  917. end
  918. embed = character_embed(
  919. char: char,
  920. img: img,
  921. user: user,
  922. color: color,
  923. event: event
  924. )if char
  925. if embed
  926. bot.send_message(
  927. channel.to_i,
  928. "Good news, #{uid}! Your character was approved",
  929. false,
  930. embed
  931. )
  932. event.message.delete
  933. else
  934. event.respond(
  935. "",
  936. admin_error_embed("Something went wrong when saving application")
  937. )
  938. end
  939. when [:character_application, :no]
  940. embed = reject_app_embed(app, :character)
  941. event.message.delete
  942. reject = event.send_embed("", embed)
  943. Emoji::CHAR_APP.each do |reaction|
  944. reject.react(reaction)
  945. end
  946. reject.react(Emoji::CHECK)
  947. reject.react(Emoji::CROSS)
  948. reject.react(Emoji::CRAYON)
  949. when [:character_application, :cross]
  950. event.message.delete
  951. when [:character_rejection, :check]
  952. user = event.server.member(UID.match(app.description)[1])
  953. embed = user_char_app(event)
  954. event.message.delete
  955. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  956. bot.send_message(user.dm.id, "", false, embed)
  957. when [:character_rejection, :cross]
  958. event.message.delete
  959. when [:character_rejection, :crayon]
  960. event.message.delete
  961. bot.send_temporary_message(
  962. event.channel.id,
  963. "",
  964. 35,
  965. false,
  966. self_edit_embed(app, Url::CHARACTER)
  967. )
  968. when [:new_app, :phone]
  969. event.message.delete_own_reaction(Emoji::PHONE)
  970. user = event.message.reacted_with(Emoji::PHONE).first
  971. bot.send_message(user.dm.id, user.id, false, nil)
  972. when [:image_application, :yes]
  973. img = ImageController.edit_image(app)
  974. char = Character.find(img.char_id)
  975. user = event.server.member(char.user_id)
  976. color = CharacterController.type_color(char)
  977. embed = char_image_embed(char, img, user, color)
  978. event.message.delete if embed
  979. channel = if img.category == 'SFW'
  980. ENV['CHAR_CH'].to_i
  981. else
  982. ENV['CHAR_NSFW_CH'].to_i
  983. end
  984. bot.send_message(channel, "Image Approved!", false, embed)
  985. when [:image_application, :no]
  986. embed = reject_app_embed(app, :image)
  987. event.message.delete
  988. reject = event.send_embed("", embed)
  989. Emoji::IMG_APP.each do |reaction|
  990. reject.react(reaction)
  991. end
  992. reject.react(Emoji::CHECK)
  993. reject.react(Emoji::CROSS)
  994. when [:image_rejection, :check]
  995. user = event.server.member(UID.match(app.description)[1])
  996. embed = user_img_app(event)
  997. event.message.delete
  998. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  999. bot.send_message(user.dm.id, "", false, embed)
  1000. when [:image_rejection, :cross]
  1001. event.message.delete
  1002. when [:item_application, :yes]
  1003. item = ItemController.edit_item(app)
  1004. embed = item_embed(item)
  1005. event.message.delete
  1006. #bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  1007. when [:item_application, :no]
  1008. embed = reject_app_embed(app)
  1009. event.message.delete
  1010. reject = event.send_embed("", embed)
  1011. reject.react(Emoji::CRAYON)
  1012. reject.react(Emoji::CROSS)
  1013. when [:item_rejection, :crayon]
  1014. embed = self_edit_embed(app, Url::ITEM)
  1015. event.message.delete
  1016. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  1017. when [:item_rejection, :cross]
  1018. event.message.delete
  1019. when [:carousel, :notebook]
  1020. emoji = Emoji::NOTEBOOK
  1021. users = event.message.reacted_with(emoji)
  1022. users.each do |user|
  1023. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1024. end
  1025. char = Character.find(carousel.char_id)
  1026. user =
  1027. case
  1028. when char.user_id.match(/public/i)
  1029. "Public"
  1030. when member = event.server.member(char.user_id)
  1031. member
  1032. else
  1033. nil
  1034. end
  1035. embed = character_embed(
  1036. char: char,
  1037. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1038. user: user,
  1039. color: CharacterController.type_color(char),
  1040. section: :bio,
  1041. event: event
  1042. )
  1043. event.message.edit("", embed)
  1044. when [:carousel, :question]
  1045. emoji = Emoji::QUESTION
  1046. users = event.message.reacted_with(emoji)
  1047. users.each do |user|
  1048. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1049. end
  1050. char = Character.find(carousel.char_id)
  1051. user =
  1052. case
  1053. when char.user_id.match(/public/i)
  1054. "Public"
  1055. when member = event.server.member(char.user_id)
  1056. member
  1057. else
  1058. nil
  1059. end
  1060. embed = character_embed(
  1061. char: char,
  1062. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1063. user: user,
  1064. color: CharacterController.type_color(char),
  1065. section: :status,
  1066. event: event
  1067. )
  1068. event.message.edit("", embed)
  1069. when [:carousel, :pallet]
  1070. emoji = Emoji::PALLET
  1071. users = event.message.reacted_with(emoji)
  1072. users.each do |user|
  1073. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1074. end
  1075. char = Character.find(carousel.char_id)
  1076. user =
  1077. case
  1078. when char.user_id.match(/public/i)
  1079. "Public"
  1080. when member = event.server.member(char.user_id)
  1081. member
  1082. else
  1083. nil
  1084. end
  1085. embed = character_embed(
  1086. char: char,
  1087. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1088. user: user,
  1089. color: CharacterController.type_color(char),
  1090. section: :type,
  1091. event: event
  1092. )
  1093. event.message.edit("", embed)
  1094. when [:carousel, :ear]
  1095. emoji = Emoji::EAR
  1096. users = event.message.reacted_with(emoji)
  1097. users.each do |user|
  1098. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1099. end
  1100. char = Character.find(carousel.char_id)
  1101. user =
  1102. case
  1103. when char.user_id.match(/public/i)
  1104. "Public"
  1105. when member = event.server.member(char.user_id)
  1106. member
  1107. else
  1108. nil
  1109. end
  1110. embed = character_embed(
  1111. char: char,
  1112. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1113. user: user,
  1114. color: CharacterController.type_color(char),
  1115. section: :rumors,
  1116. event: event
  1117. )
  1118. event.message.edit("", embed)
  1119. when [:carousel, :picture]
  1120. event.message.delete_all_reactions
  1121. char = Character.find(carousel.char_id)
  1122. img = ImageController.img_scroll(
  1123. char_id: char.id,
  1124. nsfw: event.channel.nsfw?,
  1125. )
  1126. carousel.update(id: carousel.id, image_id: img&.id)
  1127. user =
  1128. case
  1129. when char.user_id.match(/public/i)
  1130. "Public"
  1131. when member = event.server.member(char.user_id)
  1132. member
  1133. else
  1134. nil
  1135. end
  1136. embed = character_embed(
  1137. char: char,
  1138. img: img,
  1139. user: user,
  1140. color: CharacterController.type_color(char),
  1141. section: :image,
  1142. event: event
  1143. )
  1144. event.message.edit("", embed)
  1145. arrow_react(event.message)
  1146. when [:carousel, :bags]
  1147. emoji = Emoji::BAGS
  1148. users = event.message.reacted_with(emoji)
  1149. users.each do |user|
  1150. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1151. end
  1152. char = Character.find(carousel.char_id)
  1153. user =
  1154. case
  1155. when char.user_id.match(/public/i)
  1156. "Public"
  1157. when member = event.server.member(char.user_id)
  1158. member
  1159. else
  1160. nil
  1161. end
  1162. embed = character_embed(
  1163. char: char,
  1164. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1165. user: user,
  1166. color: CharacterController.type_color(char),
  1167. section: :bags,
  1168. event: event
  1169. )
  1170. event.message.edit("", embed)
  1171. when [:carousel, :family]
  1172. when [:carousel, :eyes]
  1173. emoji = Emoji::EYES
  1174. users = event.message.reacted_with(emoji)
  1175. users.each do |user|
  1176. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1177. end
  1178. char = Character.find(carousel.char_id)
  1179. user =
  1180. case
  1181. when char.user_id.match(/public/i)
  1182. "Public"
  1183. when member = event.server.member(char.user_id)
  1184. member
  1185. else
  1186. nil
  1187. end
  1188. embed = character_embed(
  1189. char: char,
  1190. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1191. user: user,
  1192. color: CharacterController.type_color(char),
  1193. section: :all,
  1194. event: event
  1195. )
  1196. event.message.edit("", embed)
  1197. when [:carousel, :key]
  1198. emoji = Emoji::KEY
  1199. users = event.message.reacted_with(emoji)
  1200. users.each do |user|
  1201. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1202. end
  1203. char = Character.find(carousel.char_id)
  1204. user =
  1205. case
  1206. when char.user_id.match(/public/i)
  1207. "Public"
  1208. when member = event.server.member(char.user_id)
  1209. member
  1210. else
  1211. nil
  1212. end
  1213. embed = character_embed(
  1214. char: char,
  1215. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1216. user: user,
  1217. color: CharacterController.type_color(char),
  1218. section: :default,
  1219. event: event
  1220. )
  1221. event.message.edit("", embed)
  1222. when [:carousel, :back]
  1223. event.message.delete_all_reactions
  1224. char = Character.find(carousel.char_id)
  1225. user =
  1226. case
  1227. when char.user_id.match(/public/i)
  1228. "Public"
  1229. when member = event.server.member(char.user_id)
  1230. member
  1231. else
  1232. nil
  1233. end
  1234. embed = character_embed(
  1235. char: char,
  1236. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1237. user: user,
  1238. color: CharacterController.type_color(char),
  1239. section: :default,
  1240. event: event
  1241. )
  1242. event.message.edit("", embed)
  1243. section_react(event.message)
  1244. when [:carousel, :left], [:carousel, :right]
  1245. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  1246. users = event.message.reacted_with(emoji)
  1247. users.each do |user|
  1248. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1249. end
  1250. char = Character.find(carousel.char_id)
  1251. img = ImageController.img_scroll(
  1252. char_id: char.id,
  1253. nsfw: event.channel.nsfw?,
  1254. img: carousel.image_id,
  1255. dir: vote
  1256. )
  1257. carousel.update(id: carousel.id, image_id: img.id)
  1258. user =
  1259. case
  1260. when char.user_id.match(/public/i)
  1261. "Public"
  1262. when member = event.server.member(char.user_id)
  1263. member
  1264. else
  1265. nil
  1266. end
  1267. embed = character_embed(
  1268. char: char,
  1269. img: img,
  1270. user: user,
  1271. color: CharacterController.type_color(char),
  1272. section: :image,
  1273. event: event
  1274. )
  1275. event.message.edit("", embed)
  1276. when [:carousel, :number]
  1277. char_index = nil
  1278. Emoji::NUMBERS.each.with_index do |emoji, i|
  1279. char_index = i if reactions[emoji]&.count.to_i > 1
  1280. end
  1281. if char_index
  1282. event.message.delete_all_reactions
  1283. char = Character.find(carousel.options[char_index])
  1284. carousel.update(id: carousel.id, char_id: char.id)
  1285. user =
  1286. case
  1287. when char.user_id.match(/public/i)
  1288. "Public"
  1289. when member = event.server.member(char.user_id)
  1290. member
  1291. else
  1292. nil
  1293. end
  1294. embed = character_embed(
  1295. char: char,
  1296. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1297. user: user,
  1298. color: CharacterController.type_color(char),
  1299. section: :default,
  1300. event: event
  1301. )
  1302. event.message.edit("", embed)
  1303. section_react(event.message)
  1304. end
  1305. when [:carousel, :cross]
  1306. event.message.delete
  1307. carousel.delete
  1308. when [:team_application, :yes]
  1309. t = Team.find_by(name: app.title)
  1310. if t
  1311. t.update(description: app.description)
  1312. else
  1313. t = Team.create!(name: app.title, description: app.description)
  1314. # create role
  1315. role = event.server.create_role(
  1316. name: t.name,
  1317. colour: 3447003,
  1318. hoist: true,
  1319. mentionable: true,
  1320. reason: "New Team"
  1321. )
  1322. role.sort_above(ENV['TEAM_ROLE'])
  1323. # create channel
  1324. channel = event.server.create_channel(
  1325. t.name,
  1326. parent: 455776627125780489,
  1327. permission_overwrites: [
  1328. { id: event.server.everyone_role.id, deny: 1024 },
  1329. { id: role.id, allow: 1024 }
  1330. ]
  1331. )
  1332. t.update(role: role.id.to_s, channel: channel.id.to_s)
  1333. # embed
  1334. embed = message_embed(
  1335. "Team Approved: #{t.name}!",
  1336. "You can join with ```pkmn-team #{t.name} | apply | character_name```"
  1337. )
  1338. bot.send_message(ENV['TEAM_CH'], "", false, embed)
  1339. event.message.delete
  1340. end
  1341. when [:team_application, :no]
  1342. event.message.delete
  1343. when [:team_request, :yes]
  1344. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1345. char = Character.find(char_id[1].to_i)
  1346. t = Team.find_by(channel: event.message.channel.id.to_s)
  1347. event.message.delete
  1348. if ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  1349. ct.update(active: true)
  1350. else
  1351. CharTeam.create(char_id: char.id, team_id: t.id)
  1352. end
  1353. user = event.server.member(char.user_id)
  1354. user.add_role(t.role.to_i) if user
  1355. embed = message_embed("New Member!", "Welcome #{char.name} to the team!")
  1356. event.send_embed("", embed)
  1357. when [:team_request, :no]
  1358. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1359. char = Character.find(char_id[1])
  1360. t = Team.find_by(channel: event.message.channel.id.to_s)
  1361. bot.send_message(
  1362. ENV['TEAM_CH'],
  1363. "#{char.name} has been declined from team #{t.name}",
  1364. false,
  1365. nil
  1366. )
  1367. event.message.delete
  1368. end
  1369. end
  1370. # This will trigger when any reaction is removed in discord
  1371. bot.reaction_remove do |event|
  1372. end
  1373. # This will trigger when a member is updated
  1374. bot.member_update do |event|
  1375. end
  1376. # This will trigger when anyone joins the server
  1377. bot.member_join do |event|
  1378. unless User.find_by(id: event.user.id)
  1379. usr = User.create(id: event.user.id)
  1380. usr.make_stats
  1381. end
  1382. end
  1383. # This will trigger when anyone leaves the server
  1384. bot.member_leave do |event|
  1385. end
  1386. # This will trigger when anyone is banned from the server
  1387. bot.user_ban do |event|
  1388. end
  1389. # This will trigger when anyone is un-banned from the server
  1390. bot.user_unban do |event|
  1391. end
  1392. bot.run