bot.rb 48 KB

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