bot.rb 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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. character.update!(active: active[0].capitalize)
  260. character.reload
  261. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  262. when name && character && !status
  263. edit_url = Url::CHARACTER + character.edit_url
  264. embed = edit_app_dm(name, edit_url, color)
  265. bot.send_message(user.dm.id, "", false, embed)
  266. edit_app_embed(user_name, name, color) if event.server
  267. when !name && !status
  268. embed = new_app_dm(user_name, user.id, color)
  269. message = bot.send_message(user.dm.id, "", false, embed)
  270. message.react(Emoji::PHONE)
  271. new_app_embed(user_name, color) if event.server
  272. else
  273. command_error_embed("There was an error processing your application!", app)
  274. end
  275. end
  276. opts = { "question | option1, option2, etc" => ""}
  277. desc = "Creates a dynamic poll in any channel"
  278. poll = Command.new(:poll, desc, opts) do |event, question, options|
  279. if options
  280. options_array = options.split(/\s?,\s?/)
  281. new_poll_embed(event, question, options_array)
  282. else
  283. command_error_embed("There was an error creating your poll!", poll)
  284. end
  285. end
  286. opts = {
  287. "participants" =>
  288. "Accepts Everyone, Here, or a comma seperated list of names"
  289. }
  290. desc = "Creates a raffle and picks a winner"
  291. raffle = Command.new(:raffle, desc, opts) do |event, participant|
  292. participants =
  293. case participant
  294. when /^everyone$/i
  295. event.server.members
  296. when /^here$/i
  297. event.message.channel.users
  298. else
  299. participant.split(/\s?,\s?/)
  300. end
  301. winner = participants.sample
  302. winner_name =
  303. case winner
  304. when String
  305. winner
  306. else
  307. winner.nickname || winner.username
  308. end
  309. if winner_name
  310. message_embed("Raffle Results!", "Winner: #{winner_name}")
  311. else
  312. command_error_embed("There was an error creating your raffle!", raffle)
  313. end
  314. end
  315. opts = {
  316. "name | keyword | (n)sfw | url" => "add or update image",
  317. "name | keyword | delete" => "remove image",
  318. "name | keyword" => "display image",
  319. "name" => "list all images"
  320. }
  321. desc = "View, add and edit your characters' images"
  322. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url, id|
  323. user = event.author
  324. char =
  325. if id
  326. Character.where(user_id: id).find_by!(name: name) if name
  327. else
  328. Character.where(user_id: user.id).find_by!(name: name) if name
  329. end
  330. color = CharacterController.type_color(char) if char
  331. img = CharImage.where(char_id: char.id).find_by(keyword: keyword) if keyword
  332. case
  333. when /^Default$/i.match(keyword)
  334. error_embed(
  335. "Cannot update Default here!",
  336. "Use `pkmn-app character` to edit your default image in your form"
  337. )
  338. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  339. img_app = CharImage.to_form(
  340. char: char,
  341. keyword: keyword,
  342. category: tag,
  343. url: url,
  344. user_id: user.id
  345. )
  346. approval = bot.send_message(ENV['APP_CH'].to_i, "", false, img_app)
  347. approval.react(Emoji::Y)
  348. approval.react(Emoji::N)
  349. success_embed("Your image has been submitted for approval!")
  350. when char && img && tag && tag.match(/delete/i)
  351. CharImage.find(img.id).delete
  352. success_embed("Removed image: #{keyword}")
  353. when char && img && !tag
  354. char_image_embed(char, img, user, color)
  355. when char && !keyword
  356. imgs = CharImage.where(char_id: char.id)
  357. image_list_embed(char, imgs, user, color)
  358. when keyword && !img
  359. error_embed("Could not find your image!")
  360. else
  361. command_error_embed("Could not process your image request!", image)
  362. end
  363. rescue ActiveRecord::RecordNotFound
  364. error_embed(
  365. "Character not Found!",
  366. "I could not find your character named #{name}"
  367. )
  368. end
  369. opts = {
  370. "" => "List all guild members",
  371. "@user" => "List all characters belonging to the user",
  372. "name " => "Display the given character",
  373. "name | section" => "Display the given section for the character",
  374. "name | image | keword" => "Display the given image"
  375. }
  376. desc = "Display info about the guild members"
  377. member = Command.new(:member, desc, opts) do |event, name, section, keyword|
  378. sections = [:all, :default, :bio, :type, :status, :rumors, :image, :bags]
  379. case name
  380. when UID
  381. user_id = UID.match(name)
  382. when String
  383. chars = Character.where(name: name)
  384. unless event.channel.nsfw?
  385. chars = chars.reject { |c| c['rating'] == 'NSFW' }
  386. end
  387. char = chars.first if chars.length == 1
  388. if char
  389. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  390. user = char.user_id.match(/public/i) ?
  391. char.user_id : event.server.member(char.user_id)
  392. color = CharacterController.type_color(char)
  393. end
  394. end
  395. case
  396. when !name
  397. chars = Character.all
  398. char_list_embed(chars)
  399. when name && user_id
  400. chars = Character.where(user_id: user_id[1])
  401. user = event.server.member(user_id[1])
  402. chars_id = []
  403. chars.each do |char|
  404. chars_id.push char.id if char.active == 'Active'
  405. end
  406. embed = user_char_embed(chars, user)
  407. msg = event.send_embed("", embed)
  408. Carousel.create(message_id: msg.id, options: chars_id)
  409. option_react(msg, chars_id)
  410. when name && chars.empty?
  411. error_embed(
  412. "Character not found!",
  413. "Could not find a character named #{name}"
  414. )
  415. when name && chars && !char
  416. embed = dup_char_embed(chars, name)
  417. chars_id = chars.map(&:id)
  418. msg = event.send_embed("", embed)
  419. Carousel.create(message_id: msg.id, options: chars_id)
  420. option_react(msg, chars_id)
  421. when name && char && !section
  422. if char.rating == 'NSFW' && !event.channel.nsfw?
  423. embed = error_embed(
  424. "Wrong Channel!",
  425. "The requested character is NSFW"
  426. )
  427. else
  428. embed = character_embed(
  429. char: char,
  430. img: img,
  431. section: :default,
  432. user: user,
  433. color: color
  434. )
  435. msg = event.send_embed("", embed)
  436. Carousel.create(message_id: msg.id, char_id: char.id)
  437. section_react(msg)
  438. end
  439. when char && section && keyword
  440. img = CharImage.where(char_id: char.id).find_by!(keyword: keyword)
  441. if img.category == 'NSFW' && !event.channel.nsfw?
  442. embed = error_embed(
  443. "Wrong Channel!",
  444. "The requested image is NSFW"
  445. )
  446. elsif !/image/i.match(section)
  447. embed = command_error_embed(
  448. "Invalid Arguments",
  449. member
  450. )
  451. else
  452. embed = character_embed(
  453. char: char,
  454. img: img,
  455. section: :image,
  456. user: user,
  457. color: color
  458. )
  459. msg = event.send_embed("", embed)
  460. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  461. arrow_react(msg)
  462. end
  463. when name && char && section
  464. sect = section.downcase.to_sym
  465. nsfw = event.channel.nsfw?
  466. img = ImageController.img_scroll(
  467. char_id: char.id,
  468. nsfw: nsfw
  469. )if section == :image
  470. if char.rating == 'NSFW' && !event.channel.nsfw?
  471. embed = error_embed(
  472. "Wrong Channel!",
  473. "The requested character is NSFW"
  474. )
  475. elsif sections.detect{ |s| s == sect }
  476. embed = character_embed(
  477. char: char,
  478. img: img,
  479. section: sect,
  480. user: user,
  481. color: color,
  482. )
  483. msg = event.send_embed("", embed)
  484. Carousel.create(
  485. message_id: msg.id,
  486. char_id: char.id,
  487. image_id: img ? img.id : nil
  488. )
  489. if sect == :image
  490. arrow_react(msg)
  491. else
  492. section_react(msg)
  493. end
  494. else
  495. error_embed("Invalid Section!")
  496. end
  497. end
  498. rescue ActiveRecord::RecordNotFound => e
  499. error_embed("Record Not Found!", e.message)
  500. end
  501. desc = "Learn about Items"
  502. opts = { "" => "list all items", "item_name" => "show known info for the item" }
  503. item = Command.new(:item, desc, opts) do |event, name|
  504. #i = name ? Item.find_by!(name: name.capitalize) : Item.all
  505. #case
  506. #when name && i
  507. #item_embed(i)
  508. #when !name && i
  509. #item_list_embed(i)
  510. #else
  511. #command_error_embed("Error proccessing your request!", item)
  512. #end
  513. #rescue ActiveRecord::RecordNotFound
  514. #error_embed("Item Not Found!")
  515. end
  516. desc = "Add and remove items from characters' inventories"
  517. opts = { "item | (-/+) amount | character" => "negative numbers remove items" }
  518. inv = Command.new(:inv, desc, opts) do |event, item, amount, name|
  519. char = Character.find_by!(name: name) if name
  520. itm = Item.find_by!(name: item) if item
  521. amt = amount.to_i
  522. case
  523. when char && itm && amt
  524. i = Inventory.update_inv(itm, amt, char)
  525. user = event.server.member(char.user_id.to_i)
  526. color = CharacterController.type_color(char)
  527. case i
  528. when Inventory, true
  529. character_embed(char: char, user: user, color: color, section: :bags)
  530. when Embed
  531. i
  532. else
  533. error_embed("Something went wrong!", "Could not update inventory")
  534. end
  535. else
  536. command_error_embed("Could not proccess your request", inv)
  537. end
  538. rescue ActiveRecord::RecordNotFound => e
  539. error_embed(e.message)
  540. end
  541. desc = "Update or edit statuses"
  542. opts = { "name | effect" => "effect displays on user when afflicted" }
  543. status = Command.new(:status, desc, opts) do |event, name, effect, flag|
  544. admin = event.user.roles.map(&:name).include?('Guild Masters')
  545. if name && effect && admin
  546. s = StatusController.edit_status(name, effect, flag)
  547. case s
  548. when Status
  549. status_details(s)
  550. when Embed
  551. s
  552. end
  553. elsif name && !effect
  554. status_details(Status.find_by!(name: name))
  555. elsif !name && !effect
  556. status_list
  557. elsif !admin
  558. error_embed("You do not have permission to do that!")
  559. else
  560. command_error_embed("Could not create status!", status)
  561. end
  562. rescue ActiveRecord::RecordNotFound => e
  563. error_embed(e.message)
  564. end
  565. opts = { "character | ailment | %afflicted" => "afflictions apply in percentages" }
  566. afflict = Command.new(:afflict, desc, opts) do |event, name, status, amount|
  567. char = Character.find_by!(name: name) if name
  568. st = Status.find_by!(name: status) if status
  569. user = char.user_id.match(/public/i) ?
  570. 'Public' : event.server.member(char.user_id)
  571. if st && char
  572. user = char.user_id.match(/public/i) ?
  573. 'Public' : event.server.member(char.user_id)
  574. color = CharacterController.type_color(char)
  575. s = StatusController.edit_char_status(char, st, amount)
  576. case s
  577. when CharStatus
  578. character_embed(char: char, user: user, color: color, section: :status)
  579. when Embed
  580. s
  581. end
  582. else
  583. command_error_embed("Error afflicting #{char.name}", afflict)
  584. end
  585. rescue ActiveRecord::RecordNotFound => e
  586. error_embed(e.message)
  587. end
  588. opts = {
  589. "character | all" => "completely cures all ailments",
  590. "character | ailment" => "completely cures the ailment",
  591. "character | ailment | %cured" => "cures a percentage of ailment"
  592. }
  593. cure = Command.new(:cure, desc, opts) do |event, name, status, amount|
  594. char = Character.find_by!(name: name) if name
  595. st = Status.find_by!(name: status) if status && !status.match(/all/i)
  596. case
  597. when char && st && amount
  598. user = char.user_id.match(/public/i) ?
  599. 'Public' : event.server.member(char.user_id)
  600. color = CharacterController.type_color(char)
  601. s = StatusController.edit_char_status(st, "-#{amount}", char)
  602. case s
  603. when CharStatus
  604. character_embed(char: char, user: user, color: color, section: :status)
  605. when Embed
  606. s
  607. end
  608. when char && st && !amount
  609. CharStatus.where(char_id: char.id).find_by!(status_id: st.id).delete
  610. success_embed("Removed #{status} from #{name}")
  611. when char && status && status.match(/all/i)
  612. csts = CharStatus.where(char_id: char.id)
  613. csts.each do |cst|
  614. cst.delete
  615. end
  616. success_embed("Removed all ailments from #{name}")
  617. else
  618. end
  619. rescue ActiveRecord::RecordNotFound => e
  620. error_embed(e.message)
  621. end
  622. desc = "Everything to do with rescue teams"
  623. opts = {
  624. "" => "list of teams",
  625. "team_name" => "display team info",
  626. "team_name | (leave/apply) | character" => "leave or apply for team",
  627. "team_name | create | description" => "admin only command"
  628. }
  629. team = Command.new(:team, desc, opts) do |event, team_name, action, desc|
  630. unless action&.match(/create/i)
  631. t = Team.find_by!(name: team_name) if team_name
  632. char = if event.author.roles.map(&:name).include?('Guild Masters')
  633. Character.find_by!(name: desc) if desc
  634. else
  635. c = Character.where(user_id: event.author.id).find_by!(name: desc) if desc
  636. ct = CharTeam.where(char_id: c.id).find_by(active: true) if c
  637. action = "second_team" if ct && action.match(/apply/i)
  638. c
  639. end
  640. end
  641. case action
  642. when /leave/i
  643. ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  644. if ct
  645. ct.update(active: false)
  646. user = event.server.member(char.user_id.to_i)
  647. sql = "SELECT name " +
  648. "FROM char_teams JOIN characters " +
  649. "ON char_teams.char_id = characters.id " +
  650. "WHERE characters.user_id = ? " +
  651. "AND char_teams.team_id = ? " +
  652. "and char_teams.active = true;"
  653. sql =
  654. ActiveRecord::Base.send(:sanitize_sql_array, [sql, user.id.to_s, t.id])
  655. user_characters_team = ActiveRecord::Base.connection.exec_query(sql)
  656. user.remove_role(t.role.to_i) if user_characters_team.count == 0
  657. bot.send_message(
  658. t.channel.to_i,
  659. "#{char.name} has left the team",
  660. false,
  661. nil
  662. )
  663. end
  664. when /apply/i
  665. members = CharTeam.where(team_id: t.id)
  666. if members.count < 6
  667. embed = team_app_embed(t, char, event.server.member(char.user_id))
  668. msg = bot.send_message(t.channel.to_i, "", false, embed)
  669. msg.react(Emoji::Y)
  670. msg.react(Emoji::N)
  671. success_embed("Your request has been posted to #{t.name}!")
  672. else
  673. error_embed("#{t.name} is full!")
  674. end
  675. when /create/i
  676. team_name = team_name || ""
  677. desc = desc || ""
  678. embed = new_team_embed(event.message.author, team_name, desc)
  679. msg = bot.send_message(ENV['APP_CH'], "", false, embed)
  680. msg.react(Emoji::Y)
  681. msg.react(Emoji::N)
  682. success_embed("Your Team Application has been submitted!")
  683. when nil
  684. t ? team_embed(t) : teams_embed()
  685. when /second_team/i
  686. error_embed("#{char.name} is already in a team!")
  687. else
  688. command_error_embed("Could not process team request!", team)
  689. end
  690. rescue ActiveRecord::RecordNotFound => e
  691. error_embed(e.message)
  692. end
  693. desc = 'roll or create dice'
  694. opts = {
  695. '' => 'shows die options',
  696. 'xdy' => 'rolls x number of y sided die',
  697. 'a,b,c,d' => 'rolls the between the provided options',
  698. 'die_name' => 'rolls the given die',
  699. 'die_name | a,b,c,d' => 'creates the die'
  700. }
  701. roll = Command.new(:roll, desc, opts) do |event, die, array|
  702. usr = event.message.author
  703. usr_name = usr.nickname || usr.name
  704. color = usr.color&.combined if event.server
  705. case die
  706. when /([0-9]*?)d([0-9]+)/i
  707. result = DiceController.roll(die)
  708. when /,/
  709. result = DiceController.roll(die.split(/\s?,\s?/))
  710. when nil
  711. result = dice_embed
  712. else
  713. a = usr.roles.map(&:name).include?('Guild Master')
  714. d = DieArray.find_by(name: die.capitalize)
  715. if !d && array && a
  716. result =
  717. DieArray.create(name: die.capitalize, sides: array.split(/\s?,\s?/))
  718. elsif d && array && a
  719. d.update(sides: array.split(/\s?,\s?/))
  720. d.reload
  721. result = d
  722. elsif d && !array
  723. result = DiceController.roll(d.sides)
  724. else
  725. result = error_embed('Die not found!')
  726. end
  727. end
  728. case result
  729. when String, Integer
  730. Embed.new(description: "#{usr_name} rolled #{result}!", color: color)
  731. when DieArray
  732. success_embed("Created Die, #{die}, with sides: #{result.sides}")
  733. when Embed
  734. result
  735. end
  736. end
  737. # ---
  738. commands = [
  739. hello,
  740. matchup,
  741. app,
  742. help,
  743. poll,
  744. raffle,
  745. member,
  746. item,
  747. #inv,
  748. status,
  749. afflict,
  750. cure,
  751. team,
  752. stats,
  753. roll
  754. ]
  755. #locked_commands = [inv]
  756. # This will trigger on every message sent in discord
  757. bot.message do |event|
  758. content = event.message.content
  759. author = event.author.id
  760. command = /^pkmn-(\w+)/.match(content)
  761. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  762. if cmd
  763. reply = cmd.call(content, event)
  764. case reply
  765. when Embed
  766. event.send_embed("", reply)
  767. when String
  768. event.respond(reply)
  769. end
  770. elsif command && !cmd && event.server
  771. event.send_embed(
  772. "",
  773. error_embed("Command not found!")
  774. )
  775. elsif author == ENV['WEBHOOK'].to_i
  776. app = event.message.embeds.first
  777. if app.author.name == 'Character Application'
  778. Character.check_user(event)
  779. else
  780. approval_react(event)
  781. end
  782. elsif content == 'import users' && author == 215240568245190656
  783. User.import_user(File.open('users.txt', 'r'))
  784. elsif !event.author.bot_account? && !event.author.webhook?
  785. usr = User.find_by(id: author.to_s)
  786. msg = URL.match(content) ? content.gsub(URL, "x" * 150) : content
  787. file = event.message.attachments.map(&:filename).count
  788. msg = file > 0 ? msg + ("x" * 40) : msg
  789. img = usr.update_xp(msg, event.author)
  790. bot.send_file(event.message.channel, File.open(img, 'r')) if img
  791. end
  792. end
  793. pm_commands = [ image ]
  794. # This will trigger when a dm is sent to the bot from a user
  795. bot.pm do |event|
  796. content = event.message.content
  797. command = /^pkmn-(\w+)/.match(content)
  798. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  799. reply = cmd.call(content, event) if cmd
  800. case reply
  801. when Embed
  802. event.send_embed("", reply)
  803. when String
  804. event.respond(reply)
  805. end
  806. end
  807. # This will trigger when any reaction is added in discord
  808. bot.reaction_add do |event|
  809. reactions = event.message.reactions
  810. app = event.message.embeds.first
  811. carousel = Carousel.find_by(message_id: event.message.id)
  812. app = event.message.embeds.first
  813. carousel = Carousel.find_by(message_id: event.message.id)
  814. carousel = Carousel.find_by(message_id: event.message.id)
  815. maj = 100
  816. form =
  817. case app&.author&.name
  818. when 'New App' then :new_app
  819. when 'Character Application'
  820. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  821. maj = m.count > 2 ? m.count/2.0 : 2
  822. :character_application
  823. when 'Character Rejection' then :character_rejection
  824. when 'Image Application'
  825. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  826. maj = m.count > 2 ? m.count/2.0 : 2
  827. :image_application
  828. when 'Image Rejection' then :image_rejection
  829. when 'Item Application'
  830. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  831. maj = m.count > 2 ? m.count/2.0 : 2
  832. :item_application
  833. when 'Item Rejection' then :item_rejection
  834. when 'Team Application'
  835. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  836. maj = m.count > 2 ? m.count/2.0 : 2
  837. :team_application
  838. when 'Team Join Request'
  839. team_id = Team.find_by(channel: event.message.channel.id.to_s).role.to_i
  840. m = event.server.roles.find{ |r| r.id == team_id }.members
  841. maj = m.count > 2 ? m.count/2.0 : 2
  842. :team_request
  843. else
  844. if event.server == nil
  845. :new_app
  846. elsif carousel
  847. :carousel
  848. end
  849. end
  850. vote =
  851. case
  852. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  853. when reactions[Emoji::N]&.count.to_i > maj then :no
  854. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  855. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  856. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  857. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  858. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  859. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  860. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  861. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  862. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  863. #when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  864. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  865. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  866. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  867. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  868. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  869. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  870. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  871. end
  872. case [form, vote]
  873. when [:character_application, :yes]
  874. uid = UID.match(app.description)
  875. user =
  876. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  877. img_url = case
  878. when !app.thumbnail&.url.nil? then app.thumbnail.url
  879. when !app.image&.url.nil? then app.image.url
  880. end
  881. char = CharacterController.edit_character(app)
  882. img = ImageController.default_image(
  883. img_url,
  884. char.id
  885. )if img_url
  886. color = CharacterController.type_color(char)
  887. channel = case char.rating
  888. when /nsfw/i
  889. ENV['CHAR_NSFW_CH']
  890. when /hidden/i
  891. user.dm&.id
  892. else
  893. ENV['CHAR_CH']
  894. end
  895. embed = character_embed(
  896. char: char,
  897. img: img,
  898. user: user,
  899. color: color
  900. )if char
  901. if embed
  902. bot.send_message(
  903. channel.to_i,
  904. "Good news, #{uid}! Your character was approved",
  905. false,
  906. embed
  907. )
  908. event.message.delete
  909. else
  910. event.respond(
  911. "",
  912. admin_error_embed("Something went wrong when saving application")
  913. )
  914. end
  915. when [:character_application, :no]
  916. embed = reject_app_embed(app, :character)
  917. event.message.delete
  918. reject = event.send_embed("", embed)
  919. Emoji::CHAR_APP.each do |reaction|
  920. reject.react(reaction)
  921. end
  922. reject.react(Emoji::CHECK)
  923. reject.react(Emoji::CROSS)
  924. reject.react(Emoji::CRAYON)
  925. when [:character_application, :cross]
  926. event.message.delete
  927. when [:character_rejection, :check]
  928. user = event.server.member(UID.match(app.description)[1])
  929. embed = user_char_app(event)
  930. event.message.delete
  931. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  932. bot.send_message(user.dm.id, "", false, embed)
  933. when [:character_rejection, :cross]
  934. event.message.delete
  935. when [:character_rejection, :crayon]
  936. event.message.delete
  937. bot.send_temporary_message(
  938. event.channel.id,
  939. "",
  940. 35,
  941. false,
  942. self_edit_embed(app, Url::CHARACTER)
  943. )
  944. when [:new_app, :phone]
  945. event.message.delete_own_reaction(Emoji::PHONE)
  946. user = event.message.reacted_with(Emoji::PHONE).first
  947. bot.send_message(user.dm.id, user.id, false, nil)
  948. when [:image_application, :yes]
  949. img = ImageController.edit_image(app)
  950. char = Character.find(img.char_id)
  951. user = event.server.member(char.user_id)
  952. color = CharacterController.type_color(char)
  953. embed = char_image_embed(char, img, user, color)
  954. event.message.delete if embed
  955. channel = if img.category == 'SFW'
  956. ENV['CHAR_CH'].to_i
  957. else
  958. ENV['CHAR_NSFW_CH'].to_i
  959. end
  960. bot.send_message(channel, "Image Approved!", false, embed)
  961. when [:image_application, :no]
  962. embed = reject_app_embed(app, :image)
  963. event.message.delete
  964. reject = event.send_embed("", embed)
  965. Emoji::IMG_APP.each do |reaction|
  966. reject.react(reaction)
  967. end
  968. reject.react(Emoji::CHECK)
  969. reject.react(Emoji::CROSS)
  970. when [:image_rejection, :check]
  971. user = event.server.member(UID.match(app.description)[1])
  972. embed = user_img_app(event)
  973. event.message.delete
  974. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  975. bot.send_message(user.dm.id, "", false, embed)
  976. when [:image_rejection, :cross]
  977. event.message.delete
  978. when [:item_application, :yes]
  979. item = ItemController.edit_item(app)
  980. embed = item_embed(item)
  981. event.message.delete
  982. bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  983. when [:item_application, :no]
  984. embed = reject_app_embed(app)
  985. event.message.delete
  986. reject = event.send_embed("", embed)
  987. reject.react(Emoji::CRAYON)
  988. reject.react(Emoji::CROSS)
  989. when [:item_rejection, :crayon]
  990. embed = self_edit_embed(app, Url::ITEM)
  991. event.message.delete
  992. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  993. when [:item_rejection, :cross]
  994. event.message.delete
  995. when [:carousel, :notebook]
  996. emoji = Emoji::NOTEBOOK
  997. users = event.message.reacted_with(emoji)
  998. users.each do |user|
  999. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1000. end
  1001. char = Character.find(carousel.char_id)
  1002. user =
  1003. case
  1004. when char.user_id.match(/public/i)
  1005. "Public"
  1006. when member = event.server.member(char.user_id)
  1007. member
  1008. else
  1009. nil
  1010. end
  1011. embed = character_embed(
  1012. char: char,
  1013. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1014. user: user,
  1015. color: CharacterController.type_color(char),
  1016. section: :bio
  1017. )
  1018. event.message.edit("", embed)
  1019. when [:carousel, :question]
  1020. emoji = Emoji::QUESTION
  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: :status
  1041. )
  1042. event.message.edit("", embed)
  1043. when [:carousel, :pallet]
  1044. emoji = Emoji::PALLET
  1045. users = event.message.reacted_with(emoji)
  1046. users.each do |user|
  1047. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1048. end
  1049. char = Character.find(carousel.char_id)
  1050. user =
  1051. case
  1052. when char.user_id.match(/public/i)
  1053. "Public"
  1054. when member = event.server.member(char.user_id)
  1055. member
  1056. else
  1057. nil
  1058. end
  1059. embed = character_embed(
  1060. char: char,
  1061. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1062. user: user,
  1063. color: CharacterController.type_color(char),
  1064. section: :type
  1065. )
  1066. event.message.edit("", embed)
  1067. when [:carousel, :ear]
  1068. emoji = Emoji::EAR
  1069. users = event.message.reacted_with(emoji)
  1070. users.each do |user|
  1071. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1072. end
  1073. char = Character.find(carousel.char_id)
  1074. user =
  1075. case
  1076. when char.user_id.match(/public/i)
  1077. "Public"
  1078. when member = event.server.member(char.user_id)
  1079. member
  1080. else
  1081. nil
  1082. end
  1083. embed = character_embed(
  1084. char: char,
  1085. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1086. user: user,
  1087. color: CharacterController.type_color(char),
  1088. section: :rumors
  1089. )
  1090. event.message.edit("", embed)
  1091. when [:carousel, :picture]
  1092. event.message.delete_all_reactions
  1093. char = Character.find(carousel.char_id)
  1094. img = ImageController.img_scroll(
  1095. char_id: char.id,
  1096. nsfw: event.channel.nsfw?,
  1097. )
  1098. carousel.update(id: carousel.id, image_id: img&.id)
  1099. user =
  1100. case
  1101. when char.user_id.match(/public/i)
  1102. "Public"
  1103. when member = event.server.member(char.user_id)
  1104. member
  1105. else
  1106. nil
  1107. end
  1108. embed = character_embed(
  1109. char: char,
  1110. img: img,
  1111. user: user,
  1112. color: CharacterController.type_color(char),
  1113. section: :image
  1114. )
  1115. event.message.edit("", embed)
  1116. arrow_react(event.message)
  1117. when [:carousel, :bags]
  1118. emoji = Emoji::BAGS
  1119. users = event.message.reacted_with(emoji)
  1120. users.each do |user|
  1121. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1122. end
  1123. char = Character.find(carousel.char_id)
  1124. user =
  1125. case
  1126. when char.user_id.match(/public/i)
  1127. "Public"
  1128. when member = event.server.member(char.user_id)
  1129. member
  1130. else
  1131. nil
  1132. end
  1133. embed = character_embed(
  1134. char: char,
  1135. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1136. user: user,
  1137. color: CharacterController.type_color(char),
  1138. section: :bags
  1139. )
  1140. event.message.edit("", embed)
  1141. when [:carousel, :family]
  1142. when [:carousel, :eyes]
  1143. emoji = Emoji::EYES
  1144. users = event.message.reacted_with(emoji)
  1145. users.each do |user|
  1146. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1147. end
  1148. char = Character.find(carousel.char_id)
  1149. user =
  1150. case
  1151. when char.user_id.match(/public/i)
  1152. "Public"
  1153. when member = event.server.member(char.user_id)
  1154. member
  1155. else
  1156. nil
  1157. end
  1158. embed = character_embed(
  1159. char: char,
  1160. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1161. user: user,
  1162. color: CharacterController.type_color(char),
  1163. section: :all
  1164. )
  1165. event.message.edit("", embed)
  1166. when [:carousel, :key]
  1167. emoji = Emoji::KEY
  1168. users = event.message.reacted_with(emoji)
  1169. users.each do |user|
  1170. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1171. end
  1172. char = Character.find(carousel.char_id)
  1173. user =
  1174. case
  1175. when char.user_id.match(/public/i)
  1176. "Public"
  1177. when member = event.server.member(char.user_id)
  1178. member
  1179. else
  1180. nil
  1181. end
  1182. embed = character_embed(
  1183. char: char,
  1184. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1185. user: user,
  1186. color: CharacterController.type_color(char),
  1187. section: :default
  1188. )
  1189. event.message.edit("", embed)
  1190. when [:carousel, :back]
  1191. event.message.delete_all_reactions
  1192. char = Character.find(carousel.char_id)
  1193. user =
  1194. case
  1195. when char.user_id.match(/public/i)
  1196. "Public"
  1197. when member = event.server.member(char.user_id)
  1198. member
  1199. else
  1200. nil
  1201. end
  1202. embed = character_embed(
  1203. char: char,
  1204. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1205. user: user,
  1206. color: CharacterController.type_color(char),
  1207. section: :default
  1208. )
  1209. event.message.edit("", embed)
  1210. section_react(event.message)
  1211. when [:carousel, :left], [:carousel, :right]
  1212. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  1213. users = event.message.reacted_with(emoji)
  1214. users.each do |user|
  1215. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1216. end
  1217. char = Character.find(carousel.char_id)
  1218. img = ImageController.img_scroll(
  1219. char_id: char.id,
  1220. nsfw: event.channel.nsfw?,
  1221. img: carousel.image_id,
  1222. dir: vote
  1223. )
  1224. carousel.update(id: carousel.id, image_id: img.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: img,
  1237. user: user,
  1238. color: CharacterController.type_color(char),
  1239. section: :image
  1240. )
  1241. event.message.edit("", embed)
  1242. when [:carousel, :number]
  1243. char_index = nil
  1244. Emoji::NUMBERS.each.with_index do |emoji, i|
  1245. char_index = i if reactions[emoji]&.count.to_i > 1
  1246. end
  1247. if char_index
  1248. event.message.delete_all_reactions
  1249. char = Character.find(carousel.options[char_index])
  1250. carousel.update(id: carousel.id, char_id: char.id)
  1251. user =
  1252. case
  1253. when char.user_id.match(/public/i)
  1254. "Public"
  1255. when member = event.server.member(char.user_id)
  1256. member
  1257. else
  1258. nil
  1259. end
  1260. embed = character_embed(
  1261. char: char,
  1262. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1263. user: user,
  1264. color: CharacterController.type_color(char),
  1265. section: :default
  1266. )
  1267. event.message.edit("", embed)
  1268. section_react(event.message)
  1269. end
  1270. when [:carousel, :cross]
  1271. event.message.delete
  1272. carousel.delete
  1273. when [:team_application, :yes]
  1274. t = Team.find_by(name: app.title)
  1275. if t
  1276. t.update(description: app.description)
  1277. else
  1278. t = Team.create!(name: app.title, description: app.description)
  1279. # create role
  1280. role = event.server.create_role(
  1281. name: t.name,
  1282. colour: 3447003,
  1283. hoist: true,
  1284. mentionable: true,
  1285. reason: "New Team"
  1286. )
  1287. role.sort_above(ENV['TEAM_ROLE'])
  1288. # create channel
  1289. channel = event.server.create_channel(
  1290. t.name,
  1291. parent: 455776627125780489,
  1292. permission_overwrites: [
  1293. { id: event.server.everyone_role.id, deny: 1024 },
  1294. { id: role.id, allow: 1024 }
  1295. ]
  1296. )
  1297. t.update(role: role.id.to_s, channel: channel.id.to_s)
  1298. # embed
  1299. embed = message_embed(
  1300. "Team Approved: #{t.name}!",
  1301. "You can join with ```pkmn-team #{t.name} | apply | character_name```"
  1302. )
  1303. bot.send_message(ENV['TEAM_CH'], "", false, embed)
  1304. event.message.delete
  1305. end
  1306. when [:team_application, :no]
  1307. event.message.delete
  1308. when [:team_request, :yes]
  1309. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1310. char = Character.find(char_id[1].to_i)
  1311. t = Team.find_by(channel: event.message.channel.id.to_s)
  1312. event.message.delete
  1313. if ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  1314. ct.update(active: true)
  1315. else
  1316. CharTeam.create(char_id: char.id, team_id: t.id)
  1317. end
  1318. user = event.server.member(char.user_id)
  1319. user.add_role(t.role.to_i) if user
  1320. embed = message_embed("New Member!", "Welcome #{char.name} to the team!")
  1321. event.send_embed("", embed)
  1322. when [:team_request, :no]
  1323. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1324. char = Character.find(char_id[1])
  1325. t = Team.find_by(channel: event.message.channel.id.to_s)
  1326. bot.send_message(
  1327. ENV['TEAM_CH'],
  1328. "#{char.name} has been declined from team #{t.name}",
  1329. false,
  1330. nil
  1331. )
  1332. event.message.delete
  1333. end
  1334. end
  1335. # This will trigger when any reaction is removed in discord
  1336. bot.reaction_remove do |event|
  1337. end
  1338. # This will trigger when a member is updated
  1339. bot.member_update do |event|
  1340. end
  1341. # This will trigger when anyone joins the server
  1342. bot.member_join do |event|
  1343. unless User.find_by(id: event.user.id)
  1344. usr = User.create(id: event.user.id)
  1345. usr.make_stats
  1346. end
  1347. end
  1348. # This will trigger when anyone leaves the server
  1349. bot.member_leave do |event|
  1350. end
  1351. # This will trigger when anyone is banned from the server
  1352. bot.user_ban do |event|
  1353. end
  1354. # This will trigger when anyone is un-banned from the server
  1355. bot.user_unban do |event|
  1356. end
  1357. bot.run