bot.rb 41 KB

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