bot.rb 42 KB

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