bot.rb 42 KB

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