bot.rb 41 KB

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