bot.rb 42 KB

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