bot.rb 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  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. landmark = true if status.match(/landmark/i) && user.roles.map(&:name).include?('Guild Masters')
  237. character =
  238. if user.roles.map(&:name).include?('Guild Masters')
  239. chars = Character.where(name: name)
  240. chars.first if chars.length == 1
  241. else
  242. Character.where(user_id: user.id).find_by(name: name) if name
  243. end
  244. active = status.match(/(in)?active/i) if status
  245. case
  246. when landmark
  247. lm = Landmark.find_by(name: name)
  248. edit_url = 'https://docs.google.com/forms/d/e/1FAIpQLSc1aFBTJxGbvauUOGF1WGEvik5SJ_3SFkyIfbR2h8eK8Fxe7Q/viewform'
  249. edit_url+= lm.edit_url
  250. embed = edit_app_dm(name, edit_url)
  251. when !chars.empty? && !character
  252. chars.each do |char|
  253. edit_url = Url::CHARACTER + char.edit_url
  254. embed = edit_app_dm(name, edit_url, color)
  255. bot.send_message(
  256. user.dm.id,
  257. "<@#{char.user_id}>'s character:",
  258. false,
  259. embed
  260. )
  261. end
  262. when name && !character
  263. app_not_found_embed(user_name, name)
  264. when status && active && character
  265. if active[1].nil?
  266. uid = character.user_id
  267. user_allowed = (User.find_by(id: uid).level / 10) + 1
  268. user_allowed = user_allowed + 1 if user.roles.map(&:name).include?('Nitro Booster')
  269. active_chars = Character.where(user_id: uid, active: 'Active')
  270. allowed = active_chars.count < user_allowed && character.active == 'Inactive'
  271. else
  272. allowed = true
  273. end
  274. if allowed
  275. character.update!(active: active[0].capitalize)
  276. character.reload
  277. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  278. else
  279. error_embed(
  280. "You're not allowed to do that!",
  281. "You either have too many active characters, the character is already active, or it is an NPC"
  282. )
  283. end
  284. when name && character && !status
  285. edit_url = Url::CHARACTER + character.edit_url
  286. embed = edit_app_dm(name, edit_url, color)
  287. bot.send_message(user.dm.id, "", false, embed)
  288. edit_app_embed(user_name, name, color) if event.server
  289. when !name && !status
  290. embed = new_app_dm(user_name, user.id, color)
  291. message = bot.send_message(user.dm.id, "", false, embed)
  292. message.react(Emoji::PHONE)
  293. new_app_embed(user_name, color) if event.server
  294. else
  295. command_error_embed("There was an error processing your application!", app)
  296. end
  297. end
  298. opts = { "question | option1, option2, etc" => ""}
  299. desc = "Creates a dynamic poll in any channel"
  300. poll = Command.new(:poll, desc, opts) do |event, question, options|
  301. if options
  302. options_array = options.split(/\s?,\s?/)
  303. new_poll_embed(event, question, options_array)
  304. else
  305. command_error_embed("There was an error creating your poll!", poll)
  306. end
  307. end
  308. opts = {
  309. "participants" =>
  310. "Accepts Everyone, Here, or a comma seperated list of names"
  311. }
  312. desc = "Creates a raffle and picks a winner"
  313. raffle = Command.new(:raffle, desc, opts) do |event, participant|
  314. participants =
  315. case participant
  316. when /^everyone$/i
  317. event.server.members
  318. when /^here$/i
  319. event.message.channel.users
  320. else
  321. participant.split(/\s?,\s?/)
  322. end
  323. winner = participants.sample
  324. winner_name =
  325. case winner
  326. when String
  327. winner
  328. else
  329. winner.nickname || winner.username
  330. end
  331. if winner_name
  332. message_embed("Raffle Results!", "Winner: #{winner_name}")
  333. else
  334. command_error_embed("There was an error creating your raffle!", raffle)
  335. end
  336. end
  337. opts = {
  338. "name | keyword | (n)sfw | url" => "add or update image",
  339. "name | keyword | delete" => "remove image",
  340. "name | keyword" => "display image",
  341. "name" => "list all images"
  342. }
  343. desc = "View, add and edit your characters' images"
  344. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url, id|
  345. user = event.author
  346. char =
  347. if id
  348. Character.where(user_id: id).find_by!(name: name) if name
  349. else
  350. Character.where(user_id: user.id).find_by!(name: name) if name
  351. end
  352. color = CharacterController.type_color(char) if char
  353. img = CharImage.where(char_id: char.id).find_by(keyword: keyword) if keyword
  354. case
  355. when /^Default$/i.match(keyword)
  356. error_embed(
  357. "Cannot update Default here!",
  358. "Use `pkmn-app character` to edit your default image in your form"
  359. )
  360. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  361. img_app = CharImage.to_form(
  362. char: char,
  363. keyword: keyword,
  364. category: tag,
  365. url: url,
  366. user_id: user.id
  367. )
  368. approval = bot.send_message(ENV['APP_CH'].to_i, "", false, img_app)
  369. approval.react(Emoji::Y)
  370. approval.react(Emoji::N)
  371. success_embed("Your image has been submitted for approval!")
  372. when char && img && tag && tag.match(/delete/i)
  373. CharImage.find(img.id).delete
  374. success_embed("Removed image: #{keyword}")
  375. when char && img && !tag
  376. char_image_embed(char, img, user, color)
  377. when char && !keyword
  378. imgs = CharImage.where(char_id: char.id)
  379. image_list_embed(char, imgs, user, color)
  380. when keyword && !img
  381. error_embed("Could not find your image!")
  382. else
  383. command_error_embed("Could not process your image request!", image)
  384. end
  385. rescue ActiveRecord::RecordNotFound
  386. error_embed(
  387. "Character not Found!",
  388. "I could not find your character named #{name}"
  389. )
  390. end
  391. opts = {
  392. "" => "List all guild members",
  393. "@user" => "List all characters belonging to the user",
  394. "name " => "Display the given character",
  395. "name | section" => "Display the given section for the character",
  396. "name | image | keword" => "Display the given image"
  397. }
  398. desc = "Display info about the guild members"
  399. member = Command.new(:member, desc, opts) do |event, name, section, keyword|
  400. sections = [:all, :default, :bio, :type, :status, :rumors, :image, :bags]
  401. case name
  402. when UID
  403. user_id = UID.match(name)
  404. when String
  405. chars = Character.where(name: name)
  406. unless event.channel.nsfw?
  407. chars = chars.reject { |c| c['rating'] == 'NSFW' }
  408. end
  409. char = chars.first if chars.length == 1
  410. if char
  411. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  412. user = case char.user_id
  413. when /public/i, /server/i
  414. char.user_id
  415. else
  416. event.server.member(char.user_id)
  417. end
  418. color = CharacterController.type_color(char)
  419. end
  420. end
  421. case
  422. when !name
  423. chars = Character.all
  424. char_list_embed(chars)
  425. when name && user_id
  426. chars = Character.where(user_id: user_id[1])
  427. user = event.server.member(user_id[1])
  428. chars_id = []
  429. chars.each do |char|
  430. chars_id.push char.id if char.active == 'Active'
  431. end
  432. embed = user_char_embed(chars, user)
  433. msg = event.send_embed("", embed)
  434. Carousel.create(message_id: msg.id, options: chars_id)
  435. option_react(msg, chars_id)
  436. when name && chars.empty?
  437. error_embed(
  438. "Character not found!",
  439. "Could not find a character named #{name}"
  440. )
  441. when name && chars && !char
  442. embed = dup_char_embed(chars, name)
  443. chars_id = chars.map(&:id)
  444. msg = event.send_embed("", embed)
  445. Carousel.create(message_id: msg.id, options: chars_id)
  446. option_react(msg, chars_id)
  447. when name && char && !section
  448. if char.rating == 'NSFW' && !event.channel.nsfw?
  449. embed = error_embed(
  450. "Wrong Channel!",
  451. "The requested character is NSFW"
  452. )
  453. else
  454. embed = character_embed(
  455. char: char,
  456. img: img,
  457. section: :default,
  458. user: user,
  459. color: color,
  460. event: event
  461. )
  462. msg = event.send_embed("", embed)
  463. Carousel.create(message_id: msg.id, char_id: char.id)
  464. section_react(msg)
  465. end
  466. when char && section && keyword
  467. img = CharImage.where(char_id: char.id).find_by!(keyword: keyword)
  468. if img.category == 'NSFW' && !event.channel.nsfw?
  469. embed = error_embed(
  470. "Wrong Channel!",
  471. "The requested image is NSFW"
  472. )
  473. elsif !/image/i.match(section)
  474. embed = command_error_embed(
  475. "Invalid Arguments",
  476. member
  477. )
  478. else
  479. embed = character_embed(
  480. char: char,
  481. img: img,
  482. section: :image,
  483. user: user,
  484. color: color,
  485. event: event
  486. )
  487. msg = event.send_embed("", embed)
  488. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  489. arrow_react(msg)
  490. end
  491. when name && char && section
  492. sect = section.downcase.to_sym
  493. nsfw = event.channel.nsfw?
  494. img = ImageController.img_scroll(
  495. char_id: char.id,
  496. nsfw: nsfw
  497. )if section == :image
  498. if char.rating == 'NSFW' && !event.channel.nsfw?
  499. embed = error_embed(
  500. "Wrong Channel!",
  501. "The requested character is NSFW"
  502. )
  503. elsif sections.detect{ |s| s == sect }
  504. embed = character_embed(
  505. char: char,
  506. img: img,
  507. section: sect,
  508. user: user,
  509. color: color,
  510. event: event
  511. )
  512. msg = event.send_embed("", embed)
  513. Carousel.create(
  514. message_id: msg.id,
  515. char_id: char.id,
  516. image_id: img ? img.id : nil
  517. )
  518. if sect == :image
  519. arrow_react(msg)
  520. else
  521. section_react(msg)
  522. end
  523. else
  524. error_embed("Invalid Section!")
  525. end
  526. end
  527. rescue ActiveRecord::RecordNotFound => e
  528. error_embed("Record Not Found!", e.message)
  529. end
  530. desc = "Learn about Items"
  531. opts = { "" => "list all items", "item_name" => "show known info for the item" }
  532. item = Command.new(:item, desc, opts) do |event, name|
  533. i = name ? Item.find_by!(name: name.capitalize) : Item.all
  534. case
  535. when name && i
  536. item_embed(i)
  537. when !name && i
  538. item_list_embed(i)
  539. #else
  540. #command_error_embed("Error proccessing your request!", item)
  541. end
  542. #rescue ActiveRecord::RecordNotFound
  543. #error_embed("Item Not Found!")
  544. end
  545. desc = "Add and remove items from characters' inventories"
  546. opts = { "item | (-/+) amount | character" => "negative numbers remove items" }
  547. inv = Command.new(:inv, desc, opts) do |event, item, amount, name|
  548. char = Character.find_by!(name: name) if name
  549. itm = Item.find_by!(name: item) if item
  550. amt = amount.to_i
  551. if char && itm && amt && event.user.roles.map(&:name).include?('Guild Masters')
  552. i = Inventory.update_inv(itm, amt, char)
  553. user = event.server.member(char.user_id.to_i)
  554. color = CharacterController.type_color(char)
  555. case i
  556. when Inventory, true
  557. character_embed(char: char, user: user, color: color, section: :bags, event: event)
  558. when Embed
  559. i
  560. else
  561. error_embed("Something went wrong!", "Could not update inventory")
  562. end
  563. elsif !event.user.roles.map(&:name).include?('Guild Masters')
  564. error_embed("You don't have permission to do that!")
  565. else
  566. command_error_embed("Could not proccess your request", inv)
  567. end
  568. rescue ActiveRecord::RecordNotFound => e
  569. error_embed(e.message)
  570. end
  571. desc = "Update or edit statuses"
  572. opts = { "name | effect" => "effect displays on user when afflicted" }
  573. status = Command.new(:status, desc, opts) do |event, name, effect, flag|
  574. admin = event.user.roles.map(&:name).include?('Guild Masters')
  575. if name && effect && admin
  576. s = StatusController.edit_status(name, effect, flag)
  577. case s
  578. when Status
  579. status_details(s)
  580. when Embed
  581. s
  582. end
  583. elsif name && !effect
  584. status_details(Status.find_by!(name: name))
  585. elsif !name && !effect
  586. status_list
  587. elsif !admin
  588. error_embed("You do not have permission to do that!")
  589. else
  590. command_error_embed("Could not create status!", status)
  591. end
  592. rescue ActiveRecord::RecordNotFound => e
  593. error_embed(e.message)
  594. end
  595. opts = { "character | ailment | %afflicted" => "afflictions apply in percentages" }
  596. afflict = Command.new(:afflict, desc, opts) do |event, name, status, amount|
  597. char = Character.find_by!(name: name) if name
  598. st = Status.find_by!(name: status) if status
  599. user = char.user_id.match(/public/i) ?
  600. 'Public' : event.server.member(char.user_id)
  601. if st && char
  602. user = char.user_id.match(/public/i) ?
  603. 'Public' : event.server.member(char.user_id)
  604. color = CharacterController.type_color(char)
  605. s = StatusController.edit_char_status(char, st, amount)
  606. case s
  607. when CharStatus
  608. character_embed(char: char, user: user, color: color, section: :status, event: event)
  609. when Embed
  610. s
  611. end
  612. else
  613. command_error_embed("Error afflicting #{char.name}", afflict)
  614. end
  615. rescue ActiveRecord::RecordNotFound => e
  616. error_embed(e.message)
  617. end
  618. opts = {
  619. "character | all" => "completely cures all ailments",
  620. "character | ailment" => "completely cures the ailment",
  621. "character | ailment | %cured" => "cures a percentage of ailment"
  622. }
  623. cure = Command.new(:cure, desc, opts) do |event, name, status, amount|
  624. char = Character.find_by!(name: name) if name
  625. st = Status.find_by!(name: status) if status && !status.match(/all/i)
  626. case
  627. when char && st && amount
  628. user = char.user_id.match(/public/i) ?
  629. 'Public' : event.server.member(char.user_id)
  630. color = CharacterController.type_color(char)
  631. s = StatusController.edit_char_status(st, "-#{amount}", char)
  632. case s
  633. when CharStatus
  634. character_embed(char: char, user: user, color: color, section: :status, event: event)
  635. when Embed
  636. s
  637. end
  638. when char && st && !amount
  639. CharStatus.where(char_id: char.id).find_by!(status_id: st.id).delete
  640. success_embed("Removed #{status} from #{name}")
  641. when char && status && status.match(/all/i)
  642. csts = CharStatus.where(char_id: char.id)
  643. csts.each do |cst|
  644. cst.delete
  645. end
  646. success_embed("Removed all ailments from #{name}")
  647. else
  648. end
  649. rescue ActiveRecord::RecordNotFound => e
  650. error_embed(e.message)
  651. end
  652. desc = "Everything to do with rescue teams"
  653. opts = {
  654. "" => "list of teams",
  655. "team_name" => "display team info",
  656. "team_name | (leave/apply) | character" => "leave or apply for team",
  657. "team_name | create | description" => "apply to create a new team",
  658. "team_name | update | description" => "must be used in team chat channel"
  659. }
  660. team = Command.new(:team, desc, opts) do |event, team_name, action, desc|
  661. unless action&.match(/create/i) || action&.match(/update/i)
  662. t = Team.find_by!(name: team_name) if team_name
  663. user = User.find_by(id: event.author.id)
  664. char = if event.author.roles.map(&:name).include?('Guild Masters')
  665. Character.find_by!(name: desc) if desc
  666. else
  667. c = Character.where(user_id: event.author.id).find_by!(name: desc) if desc
  668. ct = CharTeam.where(char_id: c.id).find_by(active: true) if c
  669. action = "second_team" if ct && action.match(/apply/i)
  670. c
  671. end
  672. end
  673. case action
  674. when /leave/i
  675. ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  676. if ct
  677. ct.update(active: false)
  678. user = event.server.member(char.user_id.to_i)
  679. sql = "SELECT name " +
  680. "FROM char_teams JOIN characters " +
  681. "ON char_teams.char_id = characters.id " +
  682. "WHERE characters.user_id = ? " +
  683. "AND char_teams.team_id = ? " +
  684. "and char_teams.active = true;"
  685. sql =
  686. ActiveRecord::Base.send(:sanitize_sql_array, [sql, user.id.to_s, t.id])
  687. user_characters_team = ActiveRecord::Base.connection.exec_query(sql)
  688. user.remove_role(t.role.to_i) if user_characters_team.count == 0
  689. bot.send_message(
  690. t.channel.to_i,
  691. "#{char.name} has left the team",
  692. false,
  693. nil
  694. )
  695. end
  696. when /apply/i
  697. members = CharTeam.where(team_id: t.id, active: true)
  698. if members.count >= 6
  699. error_embed("#{t.name} is full!")
  700. elsif user.level < 5
  701. error_embed("You are not high enough level!")
  702. else
  703. embed = team_app_embed(t, char, event.server.member(char.user_id))
  704. msg = bot.send_message(t.channel.to_i, "", false, embed)
  705. msg.react(Emoji::Y)
  706. msg.react(Emoji::N)
  707. success_embed("Your request has been posted to #{t.name}!")
  708. end
  709. when /create/i
  710. team_name = team_name || ""
  711. desc = desc || ""
  712. embed = new_team_embed(event.message.author, team_name, desc)
  713. msg = bot.send_message(ENV['APP_CH'], "", false, embed)
  714. msg.react(Emoji::Y)
  715. msg.react(Emoji::N)
  716. success_embed("Your Team Application has been submitted!")
  717. when /update/i
  718. team_name = team_name || ""
  719. desc = desc || ""
  720. t = Team.find_by(channel: event.message.channel.id)
  721. if t
  722. t.update(name: team_name, description: desc)
  723. t.reload
  724. team_embed(t)
  725. else
  726. error_embed("Must be used in team chat")
  727. end
  728. when nil
  729. t ? team_embed(t) : teams_embed()
  730. when /second_team/i
  731. error_embed("#{char.name} is already in a team!")
  732. else
  733. command_error_embed("Could not process team request!", team)
  734. end
  735. rescue ActiveRecord::RecordNotFound => e
  736. error_embed(e.message)
  737. end
  738. desc = 'roll or create dice'
  739. opts = {
  740. '' => 'shows die options',
  741. 'xdy' => 'rolls x number of y sided die',
  742. 'a,b,c,d' => 'rolls the between the provided options',
  743. 'die_name' => 'rolls the given die',
  744. 'die_name | a,b,c,d' => 'creates the die'
  745. }
  746. roll = Command.new(:roll, desc, opts) do |event, die, array|
  747. usr = event.message.author
  748. usr_name = usr.nickname || usr.name
  749. color = usr.color&.combined if event.server
  750. case die
  751. when /([0-9]*?)d([0-9]+)/i
  752. result = DiceController.roll(die)
  753. when /,/
  754. result = DiceController.roll(die.split(/\s?,\s?/))
  755. when nil
  756. result = dice_embed
  757. else
  758. a = usr.roles.map(&:name).include?('Guild Master')
  759. d = DieArray.find_by(name: die.capitalize)
  760. if !d && array && a
  761. result =
  762. DieArray.create(name: die.capitalize, sides: array.split(/\s?,\s?/))
  763. elsif d && array && a
  764. d.update(sides: array.split(/\s?,\s?/))
  765. d.reload
  766. result = d
  767. elsif d && !array
  768. result = DiceController.roll(d.sides)
  769. else
  770. result = error_embed('Die not found!')
  771. end
  772. end
  773. case result
  774. when String, Integer
  775. Embed.new(description: "#{usr_name} rolled #{result}!", color: color)
  776. when DieArray
  777. success_embed("Created Die, #{die}, with sides: #{result.sides}")
  778. when Embed
  779. result
  780. end
  781. end
  782. opts = {
  783. "" => "List all landmarks",
  784. "name " => "Display the given landmark",
  785. }
  786. desc = "Display info about the guild members"
  787. landmark = Command.new(:landmark, desc, opts) do |event, name, section, keyword|
  788. lm = Landmark.find_by!(name: name) if name
  789. usr = case lm&.user_id
  790. when /server/i
  791. lm&.user_id
  792. else
  793. event.server.member(lm&.user_id)
  794. end
  795. if lm && !section && !keyword
  796. embed = landmark_embed(lm: lm, user: usr, event: event)
  797. msg = event.send_embed("", embed)
  798. Carousel.create(message_id: msg.id, landmark_id: lm.id)
  799. landmark_react(msg)
  800. elsif lm && section && !keyword
  801. case section
  802. when /history/i
  803. embed = landmark_embed(lm: lm, user: usr, section: :history, event: event)
  804. when /warnings?/i
  805. embed = landmark_embed(lm: lm, user: usr, section: :warning, event: event)
  806. when /map/i
  807. embed = landmark_embed(lm: lm, user: usr, section: :map, event: event)
  808. when /layout/i
  809. embed = landmark_embed(lm: lm, user: usr, section: :layout, event: event)
  810. when /npcs?/i
  811. embed = landmark_embed(lm: lm, user: usr, section: :npcs, event: event)
  812. end
  813. msg = event.send_embed("", embed)
  814. Carousel.create(message_id: msg.id, landmark_id: lm.id)
  815. landmark_react(msg)
  816. elsif !name && !section && !keyword
  817. landmark_list
  818. end
  819. rescue ActiveRecord::RecordNotFound => e
  820. error_embed("Record Not Found!", e.message)
  821. end
  822. # ---
  823. commands = [
  824. hello,
  825. matchup,
  826. app,
  827. help,
  828. poll,
  829. raffle,
  830. member,
  831. item,
  832. inv,
  833. status,
  834. afflict,
  835. cure,
  836. team,
  837. stats,
  838. roll,
  839. landmark
  840. ]
  841. #locked_commands = [inv]
  842. # This will trigger on every message sent in discord
  843. bot.message do |event|
  844. content = event.message.content
  845. author = event.author.id
  846. command = /^pkmn-(\w+)/.match(content)
  847. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  848. if cmd
  849. reply = cmd.call(content, event)
  850. case reply
  851. when Embed
  852. event.send_embed("", reply)
  853. when String
  854. event.respond(reply)
  855. end
  856. #elsif command && !cmd && event.server
  857. #event.send_embed(
  858. #"",
  859. #error_embed("Command not found!")
  860. #)
  861. elsif author == ENV['WEBHOOK'].to_i
  862. app = event.message.embeds.first
  863. if app.author.name == 'Character Application'
  864. Character.check_user(event)
  865. else
  866. approval_react(event)
  867. end
  868. elsif event.message.channel == 454082477192118275 || event.message.channel == 613365750383640584
  869. elsif !event.author.bot_account? && !event.author.webhook? && event.server
  870. usr = User.find_by(id: author.to_s)
  871. msg = URL.match(content) ? content.gsub(URL, "x" * 150) : content
  872. file = event.message.attachments.map(&:filename).count
  873. msg = file > 0 ? msg + ("x" * 40) : msg
  874. img = usr.update_xp(msg, event.author)
  875. bot.send_file(event.message.channel, File.open(img, 'r')) if img
  876. end
  877. end
  878. pm_commands = [ image ]
  879. # This will trigger when a dm is sent to the bot from a user
  880. bot.pm do |event|
  881. content = event.message.content
  882. command = /^pkmn-(\w+)/.match(content)
  883. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  884. reply = cmd.call(content, event) if cmd
  885. case reply
  886. when Embed
  887. event.send_embed("", reply)
  888. when String
  889. event.respond(reply)
  890. end
  891. end
  892. # This will trigger when any reaction is added in discord
  893. bot.reaction_add do |event|
  894. reactions = event.message.reactions
  895. app = event.message.embeds.first
  896. carousel = Carousel.find_by(message_id: event.message.id)
  897. team_chat = Team.find_by(channel: event.message.channel.id)
  898. maj = 100
  899. form =
  900. case app&.author&.name
  901. when 'New App' then :new_app
  902. when 'Character Application'
  903. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  904. maj = m.count > 2 ? m.count/2.0 : 2
  905. :character_application
  906. when 'Character Rejection' then :character_rejection
  907. when 'Image Application'
  908. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  909. maj = m.count > 2 ? m.count/2.0 : 2
  910. :image_application
  911. when 'Image Rejection' then :image_rejection
  912. when 'Item Application'
  913. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  914. maj = m.count > 2 ? m.count/2.0 : 2
  915. :item_application
  916. when 'Item Rejection' then :item_rejection
  917. when 'Team Application'
  918. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  919. maj = m.count > 2 ? m.count/2.0 : 2
  920. :team_application
  921. when 'Team Join Request'
  922. team_id = Team.find_by(channel: event.message.channel.id.to_s).role.to_i
  923. m = event.server.roles.find{ |r| r.id == team_id }.members
  924. maj = m.count > 2 ? m.count/2.0 : 2
  925. :team_request
  926. when 'Landmark Application'
  927. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  928. maj = m.count > 2 ? m.count/2.0 : 2
  929. :landmark_application
  930. else
  931. if event.server == nil
  932. :new_app
  933. elsif carousel&.char_id || carousel&.options
  934. :member
  935. elsif carousel&.landmark_id
  936. :landmark
  937. elsif team_chat
  938. :team_chat
  939. end
  940. end
  941. vote =
  942. case
  943. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  944. when reactions[Emoji::N]&.count.to_i > maj then :no
  945. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  946. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  947. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  948. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  949. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  950. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  951. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  952. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  953. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  954. #when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  955. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  956. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  957. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  958. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  959. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  960. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  961. when reactions[Emoji::BOOKS]&.count.to_i > 1 then :books
  962. when reactions[Emoji::SKULL]&.count.to_i > 1 then :skull
  963. when reactions[Emoji::MAP]&.count.to_i > 1 then :map
  964. when reactions[Emoji::HOUSES]&.count.to_i > 1 then :houses
  965. when reactions[Emoji::PEOPLE]&.count.to_i > 1 then :people
  966. when reactions[Emoji::BUST]&.count.to_i > 1 then :bust
  967. when reactions[Emoji::PIN]&.count.to_i > 0 then :pin
  968. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  969. end
  970. case [form, vote]
  971. when [:character_application, :yes]
  972. uid = UID.match(app.description)
  973. user =
  974. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  975. img_url = case
  976. when !app.thumbnail&.url.nil? then app.thumbnail.url
  977. when !app.image&.url.nil? then app.image.url
  978. end
  979. char = CharacterController.edit_character(app)
  980. img = ImageController.default_image(
  981. img_url,
  982. char.id
  983. )if img_url
  984. color = CharacterController.type_color(char)
  985. channel = case char.rating
  986. when /nsfw/i
  987. ENV['CHAR_NSFW_CH']
  988. when /hidden/i
  989. user.dm&.id
  990. else
  991. ENV['CHAR_CH']
  992. end
  993. embed = character_embed(
  994. char: char,
  995. img: img,
  996. user: user,
  997. color: color,
  998. event: event
  999. )if char
  1000. if embed
  1001. bot.send_message(
  1002. channel.to_i,
  1003. "Good news, #{uid}! Your character was approved",
  1004. false,
  1005. embed
  1006. )
  1007. event.message.delete
  1008. else
  1009. event.respond(
  1010. "",
  1011. admin_error_embed("Something went wrong when saving application")
  1012. )
  1013. end
  1014. when [:character_application, :no]
  1015. embed = reject_app_embed(app, :character)
  1016. event.message.delete
  1017. reject = event.send_embed("", embed)
  1018. Emoji::CHAR_APP.each do |reaction|
  1019. reject.react(reaction)
  1020. end
  1021. reject.react(Emoji::CHECK)
  1022. reject.react(Emoji::CROSS)
  1023. reject.react(Emoji::CRAYON)
  1024. when [:character_application, :cross]
  1025. event.message.delete
  1026. when [:character_application, :crayon]
  1027. event.message.delete
  1028. bot.send_temporary_message(
  1029. event.channel.id,
  1030. "",
  1031. 35,
  1032. false,
  1033. self_edit_embed(app, Url::CHARACTER)
  1034. )
  1035. when [:character_rejection, :check]
  1036. user = event.server.member(UID.match(app.description)[1])
  1037. embed = user_char_app(event)
  1038. event.message.delete
  1039. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  1040. bot.send_message(user.dm.id, "", false, embed)
  1041. when [:character_rejection, :cross]
  1042. event.message.delete
  1043. when [:character_rejection, :crayon]
  1044. event.message.delete
  1045. bot.send_temporary_message(
  1046. event.channel.id,
  1047. "",
  1048. 35,
  1049. false,
  1050. self_edit_embed(app, Url::CHARACTER)
  1051. )
  1052. when [:landmark_application, :yes]
  1053. uid = UID.match(app.description)
  1054. user =
  1055. app.description.match(/server/i) ? 'Server' : event.server.member(uid[1])
  1056. img_url = case
  1057. when !app.thumbnail&.url.nil? then app.thumbnail.url
  1058. when !app.image&.url.nil? then app.image.url
  1059. end
  1060. lm = LandmarkController.edit_landmark(app)
  1061. embed = landmark_embed(lm: lm, user: user, event: event)if lm
  1062. channel = '453277760429883393'
  1063. if embed
  1064. bot.send_message(
  1065. channel.to_i,
  1066. "Good news, #{uid}! Your landmark was approved",
  1067. false,
  1068. embed
  1069. )
  1070. event.message.delete
  1071. else
  1072. event.respond(
  1073. "",
  1074. admin_error_embed("Something went wrong when saving application")
  1075. )
  1076. end
  1077. when [:landmark_application, :cross]
  1078. event.message.delete
  1079. when [:landmark_application, :crayon]
  1080. event.message.delete
  1081. bot.send_temporary_message(
  1082. event.channel.id,
  1083. "",
  1084. 35,
  1085. false,
  1086. self_edit_embed(app, Url::LANDMARK)
  1087. )
  1088. when [:new_app, :phone]
  1089. event.message.delete_own_reaction(Emoji::PHONE)
  1090. user = event.message.reacted_with(Emoji::PHONE).first
  1091. bot.send_message(user.dm.id, user.id, false, nil)
  1092. when [:image_application, :yes]
  1093. img = ImageController.edit_image(app)
  1094. char = Character.find(img.char_id)
  1095. user = event.server.member(char.user_id)
  1096. color = CharacterController.type_color(char)
  1097. embed = char_image_embed(char, img, user, color)
  1098. event.message.delete if embed
  1099. channel = if img.category == 'SFW'
  1100. ENV['CHAR_CH'].to_i
  1101. else
  1102. ENV['CHAR_NSFW_CH'].to_i
  1103. end
  1104. bot.send_message(channel, "Image Approved!", false, embed)
  1105. when [:image_application, :no]
  1106. embed = reject_app_embed(app, :image)
  1107. event.message.delete
  1108. reject = event.send_embed("", embed)
  1109. Emoji::IMG_APP.each do |reaction|
  1110. reject.react(reaction)
  1111. end
  1112. reject.react(Emoji::CHECK)
  1113. reject.react(Emoji::CROSS)
  1114. when [:image_rejection, :check]
  1115. user = event.server.member(UID.match(app.description)[1])
  1116. embed = user_img_app(event)
  1117. event.message.delete
  1118. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  1119. bot.send_message(user.dm.id, "", false, embed)
  1120. when [:image_rejection, :cross]
  1121. event.message.delete
  1122. when [:item_application, :yes]
  1123. item = ItemController.edit_item(app)
  1124. embed = item_embed(item)
  1125. event.message.delete
  1126. #bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  1127. when [:item_application, :no]
  1128. embed = reject_app_embed(app)
  1129. event.message.delete
  1130. reject = event.send_embed("", embed)
  1131. reject.react(Emoji::CRAYON)
  1132. reject.react(Emoji::CROSS)
  1133. when [:item_rejection, :crayon]
  1134. embed = self_edit_embed(app, Url::ITEM)
  1135. event.message.delete
  1136. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  1137. when [:item_rejection, :cross]
  1138. event.message.delete
  1139. when [:member, :picture]
  1140. event.message.delete_all_reactions
  1141. char = Character.find(carousel.char_id)
  1142. img = ImageController.img_scroll(
  1143. char_id: char.id,
  1144. nsfw: event.channel.nsfw?,
  1145. )
  1146. carousel.update(id: carousel.id, image_id: img&.id)
  1147. user =
  1148. case
  1149. when char.user_id.match(/public/i)
  1150. "Public"
  1151. when member = event.server.member(char.user_id)
  1152. member
  1153. else
  1154. nil
  1155. end
  1156. embed = character_embed(
  1157. char: char,
  1158. img: img,
  1159. user: user,
  1160. color: CharacterController.type_color(char),
  1161. section: :image,
  1162. event: event
  1163. )
  1164. event.message.edit("", embed)
  1165. arrow_react(event.message)
  1166. when [:member, :bags]
  1167. emoji = Emoji::BAGS
  1168. users = event.message.reacted_with(emoji)
  1169. users.each do |user|
  1170. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1171. end
  1172. char = Character.find(carousel.char_id)
  1173. user =
  1174. case
  1175. when char.user_id.match(/public/i)
  1176. "Public"
  1177. when member = event.server.member(char.user_id)
  1178. member
  1179. else
  1180. nil
  1181. end
  1182. embed = character_embed(
  1183. char: char,
  1184. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1185. user: user,
  1186. color: CharacterController.type_color(char),
  1187. section: :bags,
  1188. event: event
  1189. )
  1190. event.message.edit("", embed)
  1191. when [:member, :family]
  1192. when [:member, :eyes]
  1193. emoji = Emoji::EYES
  1194. users = event.message.reacted_with(emoji)
  1195. users.each do |user|
  1196. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1197. end
  1198. char = Character.find(carousel.char_id)
  1199. user =
  1200. case
  1201. when char.user_id.match(/public/i)
  1202. "Public"
  1203. when member = event.server.member(char.user_id)
  1204. member
  1205. else
  1206. nil
  1207. end
  1208. embed = character_embed(
  1209. char: char,
  1210. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1211. user: user,
  1212. color: CharacterController.type_color(char),
  1213. section: :all,
  1214. event: event
  1215. )
  1216. event.message.edit("", embed)
  1217. when [:member, :bust]
  1218. event.message.delete_all_reactions
  1219. char = Character.find(carousel.char_id)
  1220. chars = []
  1221. actives = []
  1222. embed = case char.user_id
  1223. when /pulic/i, /server/i
  1224. chars = Character.all
  1225. char_list_embed(chars)
  1226. else
  1227. chars = Character.where(user_id: char.user_id)
  1228. user = event.server.member(char.user_id)
  1229. actives = Character.where(user_id: char.user_id, active: 'Active')
  1230. ids = actives.map(&:id)
  1231. carousel.update(options: ids)
  1232. user_char_embed(chars, user)
  1233. end
  1234. event.message.edit("", embed)
  1235. option_react(event.message, actives) unless actives.empty?
  1236. when [:member, :back]
  1237. event.message.delete_all_reactions
  1238. char = Character.find(carousel.char_id)
  1239. user =
  1240. case
  1241. when char.user_id.match(/public/i)
  1242. "Public"
  1243. when member = event.server.member(char.user_id)
  1244. member
  1245. else
  1246. nil
  1247. end
  1248. embed = character_embed(
  1249. char: char,
  1250. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1251. user: user,
  1252. color: CharacterController.type_color(char),
  1253. section: :default,
  1254. event: event
  1255. )
  1256. event.message.edit("", embed)
  1257. section_react(event.message)
  1258. when [:member, :left], [:member, :right]
  1259. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  1260. users = event.message.reacted_with(emoji)
  1261. users.each do |user|
  1262. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1263. end
  1264. char = Character.find(carousel.char_id)
  1265. img = ImageController.img_scroll(
  1266. char_id: char.id,
  1267. nsfw: event.channel.nsfw?,
  1268. img: carousel.image_id,
  1269. dir: vote
  1270. )
  1271. carousel.update(id: carousel.id, image_id: img.id)
  1272. user =
  1273. case
  1274. when char.user_id.match(/public/i)
  1275. "Public"
  1276. when member = event.server.member(char.user_id)
  1277. member
  1278. else
  1279. nil
  1280. end
  1281. embed = character_embed(
  1282. char: char,
  1283. img: img,
  1284. user: user,
  1285. color: CharacterController.type_color(char),
  1286. section: :image,
  1287. event: event
  1288. )
  1289. event.message.edit("", embed)
  1290. when [:member, :number]
  1291. char_index = nil
  1292. Emoji::NUMBERS.each.with_index do |emoji, i|
  1293. char_index = i if reactions[emoji]&.count.to_i > 1
  1294. end
  1295. if char_index
  1296. event.message.delete_all_reactions
  1297. char = Character.find(carousel.options[char_index])
  1298. carousel.update(id: carousel.id, char_id: char.id)
  1299. user =
  1300. case
  1301. when char.user_id.match(/public/i)
  1302. "Public"
  1303. when member = event.server.member(char.user_id)
  1304. member
  1305. else
  1306. nil
  1307. end
  1308. embed = character_embed(
  1309. char: char,
  1310. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1311. user: user,
  1312. color: CharacterController.type_color(char),
  1313. section: :default,
  1314. event: event
  1315. )
  1316. event.message.edit("", embed)
  1317. section_react(event.message)
  1318. end
  1319. when [:member, :cross]
  1320. event.message.delete
  1321. carousel.delete
  1322. when [:landmark, :books]
  1323. emoji = Emoji::BOOKS
  1324. users = event.message.reacted_with(emoji)
  1325. users.each do |user|
  1326. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1327. end
  1328. lm = Landmark.find(carousel.landmark_id)
  1329. user =
  1330. case
  1331. when lm.user_id.match(/server/i)
  1332. "Server Owned"
  1333. when member = event.server.member(lm.user_id)
  1334. member
  1335. else
  1336. nil
  1337. end
  1338. embed = landmark_embed(lm: lm, user: user, section: :history, event: event)
  1339. event.message.edit("", embed)
  1340. when [:landmark, :skull]
  1341. emoji = Emoji::SKULL
  1342. users = event.message.reacted_with(emoji)
  1343. users.each do |user|
  1344. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1345. end
  1346. lm = Landmark.find(carousel.landmark_id)
  1347. user =
  1348. case
  1349. when lm.user_id.match(/server/i)
  1350. "Server Owned"
  1351. when member = event.server.member(lm.user_id)
  1352. member
  1353. else
  1354. nil
  1355. end
  1356. embed = landmark_embed(lm: lm, user: user, section: :warning, event: event)
  1357. event.message.edit("", embed)
  1358. when [:landmark, :map]
  1359. emoji = Emoji::MAP
  1360. users = event.message.reacted_with(emoji)
  1361. users.each do |user|
  1362. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1363. end
  1364. lm = Landmark.find(carousel.landmark_id)
  1365. user =
  1366. case
  1367. when lm.user_id.match(/server/i)
  1368. "Server Owned"
  1369. when member = event.server.member(lm.user_id)
  1370. member
  1371. else
  1372. nil
  1373. end
  1374. embed = landmark_embed(lm: lm, user: user, section: :map, event: event)
  1375. event.message.edit("", embed)
  1376. when [:landmark, :houses]
  1377. emoji = Emoji::HOUSES
  1378. users = event.message.reacted_with(emoji)
  1379. users.each do |user|
  1380. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1381. end
  1382. lm = Landmark.find(carousel.landmark_id)
  1383. user =
  1384. case
  1385. when lm.user_id.match(/server/i)
  1386. "Server Owned"
  1387. when member = event.server.member(lm.user_id)
  1388. member
  1389. else
  1390. nil
  1391. end
  1392. embed = landmark_embed(lm: lm, user: user, section: :layout, event: event)
  1393. event.message.edit("", embed)
  1394. when [:landmark, :people]
  1395. emoji = Emoji::PEOPLE
  1396. users = event.message.reacted_with(emoji)
  1397. users.each do |user|
  1398. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1399. end
  1400. lm = Landmark.find(carousel.landmark_id)
  1401. user =
  1402. case
  1403. when lm.user_id.match(/server/i)
  1404. "Server Owned"
  1405. when member = event.server.member(lm.user_id)
  1406. member
  1407. else
  1408. nil
  1409. end
  1410. embed = landmark_embed(lm: lm, user: user, section: :npcs, event: event)
  1411. event.message.edit("", embed)
  1412. when [:landmark, :cross]
  1413. event.message.delete
  1414. carousel.delete
  1415. when [:team_application, :yes]
  1416. t = Team.find_by(name: app.title)
  1417. if t
  1418. t.update(description: app.description)
  1419. else
  1420. t = Team.create!(name: app.title, description: app.description)
  1421. # create role
  1422. role = event.server.create_role(
  1423. name: t.name,
  1424. colour: 3447003,
  1425. hoist: true,
  1426. mentionable: true,
  1427. reason: "New Team"
  1428. )
  1429. role.sort_above(ENV['TEAM_ROLE'])
  1430. # create channel
  1431. channel = event.server.create_channel(
  1432. t.name,
  1433. parent: 455776627125780489,
  1434. permission_overwrites: [
  1435. { id: event.server.everyone_role.id, deny: 1024 },
  1436. { id: role.id, allow: 1024 }
  1437. ]
  1438. )
  1439. t.update(role: role.id.to_s, channel: channel.id.to_s)
  1440. # embed
  1441. embed = message_embed(
  1442. "Team Approved: #{t.name}!",
  1443. "You can join with ```pkmn-team #{t.name} | apply | character_name```"
  1444. )
  1445. bot.send_message(ENV['TEAM_CH'], "", false, embed)
  1446. event.message.delete
  1447. end
  1448. when [:team_application, :no]
  1449. event.message.delete
  1450. when [:team_request, :yes]
  1451. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1452. char = Character.find(char_id[1].to_i)
  1453. t = Team.find_by(channel: event.message.channel.id.to_s)
  1454. event.message.delete
  1455. if ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  1456. ct.update(active: true)
  1457. else
  1458. CharTeam.create(char_id: char.id, team_id: t.id)
  1459. end
  1460. user = event.server.member(char.user_id)
  1461. user.add_role(t.role.to_i) if user
  1462. embed = message_embed("New Member!", "Welcome #{char.name} to the team!")
  1463. event.send_embed("", embed)
  1464. when [:team_request, :no]
  1465. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1466. char = Character.find(char_id[1])
  1467. t = Team.find_by(channel: event.message.channel.id.to_s)
  1468. bot.send_message(
  1469. ENV['TEAM_CH'],
  1470. "#{char.name} has been declined from team #{t.name}",
  1471. false,
  1472. nil
  1473. )
  1474. event.message.delete
  1475. when [:team_chat, :pin]
  1476. event.message.pin
  1477. end
  1478. end
  1479. # This will trigger when any reaction is removed in discord
  1480. bot.reaction_remove do |event|
  1481. end
  1482. # This will trigger when a member is updated
  1483. bot.member_update do |event|
  1484. end
  1485. # This will trigger when anyone joins the server
  1486. bot.member_join do |event|
  1487. unless User.find_by(id: event.user.id)
  1488. usr = User.create(id: event.user.id)
  1489. usr.make_stats
  1490. end
  1491. end
  1492. # This will trigger when anyone leaves the server
  1493. bot.member_leave do |event|
  1494. end
  1495. # This will trigger when anyone is banned from the server
  1496. bot.user_ban do |event|
  1497. end
  1498. # This will trigger when anyone is un-banned from the server
  1499. bot.user_unban do |event|
  1500. end
  1501. bot.run