bot.rb 41 KB

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