bot.rb 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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. command = /^pkmn-(\w+)/.match(content)
  859. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  860. if cmd
  861. reply = cmd.call(content, event)
  862. case reply
  863. when Embed
  864. event.send_embed("", reply)
  865. when String
  866. event.respond(reply)
  867. end
  868. #elsif command && !cmd && event.server
  869. #event.send_embed(
  870. #"",
  871. #error_embed("Command not found!")
  872. #)
  873. elsif event.message.channel.id == 644771348073152522 && content.match(/clear chat/i)
  874. msgs = event.message.channel.history(10)
  875. msgs.reject { |msg| msg.author.webhook? || msg.id == 651836628486062081 }
  876. event.message.channel.delete_messages(msgs)
  877. elsif author == ENV['WEBHOOK'].to_i
  878. app = event.message.embeds.first
  879. if app.author.name == 'Character Application'
  880. Character.check_user(event)
  881. else
  882. approval_react(event)
  883. end
  884. elsif event.message.channel == 454082477192118275 || event.message.channel == 613365750383640584
  885. elsif !event.author.bot_account? && !event.author.webhook? && event.server
  886. usr = User.find_by(id: author.to_s)
  887. msg = URL.match(content) ? content.gsub(URL, "x" * 150) : content
  888. file = event.message.attachments.map(&:filename).count
  889. msg = file > 0 ? msg + ("x" * 40) : msg
  890. img = usr.update_xp(msg, event.author)
  891. bot.send_file(event.message.channel, File.open(img, 'r')) if img
  892. end
  893. end
  894. pm_commands = [ image ]
  895. # This will trigger when a dm is sent to the bot from a user
  896. bot.pm do |event|
  897. content = event.message.content
  898. command = /^pkmn-(\w+)/.match(content)
  899. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  900. reply = cmd.call(content, event) if cmd
  901. case reply
  902. when Embed
  903. event.send_embed("", reply)
  904. when String
  905. event.respond(reply)
  906. end
  907. end
  908. # This will trigger when any reaction is added in discord
  909. bot.reaction_add do |event|
  910. reactions = event.message.reactions
  911. app = event.message.embeds.first
  912. carousel = Carousel.find_by(message_id: event.message.id)
  913. team_chat = Team.find_by(channel: event.message.channel.id)
  914. maj = 100
  915. form =
  916. case app&.author&.name
  917. when 'New App' then :new_app
  918. when 'Character Application'
  919. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  920. maj = m.count > 2 ? m.count/2.0 : 2
  921. :character_application
  922. when 'Character Rejection' then :character_rejection
  923. when 'Image Application'
  924. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  925. maj = m.count > 2 ? m.count/2.0 : 2
  926. :image_application
  927. when 'Image Rejection' then :image_rejection
  928. when 'Item Application'
  929. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  930. maj = m.count > 2 ? m.count/2.0 : 2
  931. :item_application
  932. when 'Item Rejection' then :item_rejection
  933. when 'Team Application'
  934. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  935. maj = m.count > 2 ? m.count/2.0 : 2
  936. :team_application
  937. when 'Team Join Request'
  938. team_id = Team.find_by(channel: event.message.channel.id.to_s).role.to_i
  939. m = event.server.roles.find{ |r| r.id == team_id }.members
  940. maj = m.count > 2 ? m.count/2.0 : 2
  941. :team_request
  942. when 'Landmark Application'
  943. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  944. maj = m.count > 2 ? m.count/2.0 : 2
  945. :landmark_application
  946. else
  947. if event.server == nil
  948. :new_app
  949. elsif carousel&.char_id || carousel&.options
  950. :member
  951. elsif carousel&.landmark_id
  952. :landmark
  953. elsif team_chat
  954. :team_chat
  955. end
  956. end
  957. vote =
  958. case
  959. when reactions[Emoji::Y]&.count.to_i > maj then :yes
  960. when reactions[Emoji::N]&.count.to_i > maj then :no
  961. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  962. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  963. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  964. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  965. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  966. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  967. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  968. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  969. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  970. #when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  971. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  972. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  973. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  974. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  975. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  976. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  977. when reactions[Emoji::BOOKS]&.count.to_i > 1 then :books
  978. when reactions[Emoji::SKULL]&.count.to_i > 1 then :skull
  979. when reactions[Emoji::MAP]&.count.to_i > 1 then :map
  980. when reactions[Emoji::HOUSES]&.count.to_i > 1 then :houses
  981. when reactions[Emoji::PEOPLE]&.count.to_i > 1 then :people
  982. when reactions[Emoji::BUST]&.count.to_i > 1 then :bust
  983. when reactions[Emoji::PIN]&.count.to_i > 0 then :pin
  984. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  985. end
  986. case [form, vote]
  987. when [:character_application, :yes]
  988. uid = UID.match(app.description)
  989. user =
  990. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  991. img_url = case
  992. when !app.thumbnail&.url.nil? then app.thumbnail.url
  993. when !app.image&.url.nil? then app.image.url
  994. end
  995. char = CharacterController.edit_character(app)
  996. img = ImageController.default_image(
  997. img_url,
  998. char.id
  999. )if img_url
  1000. color = CharacterController.type_color(char)
  1001. channel = case char.rating
  1002. when /nsfw/i
  1003. ENV['CHAR_NSFW_CH']
  1004. when /hidden/i
  1005. user.dm&.id
  1006. else
  1007. ENV['CHAR_CH']
  1008. end
  1009. embed = character_embed(
  1010. char: char,
  1011. img: img,
  1012. user: user,
  1013. color: color,
  1014. event: event
  1015. )if char
  1016. if embed
  1017. bot.send_message(
  1018. channel.to_i,
  1019. "Good news, #{uid}! Your character was approved",
  1020. false,
  1021. embed
  1022. )
  1023. event.message.delete
  1024. else
  1025. event.respond(
  1026. "",
  1027. admin_error_embed("Something went wrong when saving application")
  1028. )
  1029. end
  1030. when [:character_application, :no]
  1031. embed = reject_app_embed(app, :character)
  1032. event.message.delete
  1033. reject = event.send_embed("", embed)
  1034. Emoji::CHAR_APP.each do |reaction|
  1035. reject.react(reaction)
  1036. end
  1037. reject.react(Emoji::CHECK)
  1038. reject.react(Emoji::CROSS)
  1039. reject.react(Emoji::CRAYON)
  1040. when [:character_application, :cross]
  1041. event.message.delete
  1042. when [:character_application, :crayon]
  1043. event.message.delete
  1044. bot.send_temporary_message(
  1045. event.channel.id,
  1046. "",
  1047. 35,
  1048. false,
  1049. self_edit_embed(app, Url::CHARACTER)
  1050. )
  1051. when [:character_rejection, :check]
  1052. user = event.server.member(UID.match(app.description)[1])
  1053. embed = user_char_app(event)
  1054. event.message.delete
  1055. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  1056. bot.send_message(user.dm.id, "", false, embed)
  1057. when [:character_rejection, :cross]
  1058. event.message.delete
  1059. when [:character_rejection, :crayon]
  1060. event.message.delete
  1061. bot.send_temporary_message(
  1062. event.channel.id,
  1063. "",
  1064. 35,
  1065. false,
  1066. self_edit_embed(app, Url::CHARACTER)
  1067. )
  1068. when [:landmark_application, :yes]
  1069. uid = UID.match(app.description)
  1070. user =
  1071. app.description.match(/server/i) ? 'Server' : event.server.member(uid[1])
  1072. img_url = case
  1073. when !app.thumbnail&.url.nil? then app.thumbnail.url
  1074. when !app.image&.url.nil? then app.image.url
  1075. end
  1076. lm = LandmarkController.edit_landmark(app)
  1077. embed = landmark_embed(lm: lm, user: user, event: event)if lm
  1078. channel = '453277760429883393'
  1079. if embed
  1080. bot.send_message(
  1081. channel.to_i,
  1082. "Good news, #{uid}! Your landmark was approved",
  1083. false,
  1084. embed
  1085. )
  1086. event.message.delete
  1087. else
  1088. event.respond(
  1089. "",
  1090. admin_error_embed("Something went wrong when saving application")
  1091. )
  1092. end
  1093. when [:landmark_application, :cross]
  1094. event.message.delete
  1095. when [:landmark_application, :crayon]
  1096. event.message.delete
  1097. bot.send_temporary_message(
  1098. event.channel.id,
  1099. "",
  1100. 35,
  1101. false,
  1102. self_edit_embed(app, Url::LANDMARK)
  1103. )
  1104. when [:new_app, :phone]
  1105. event.message.delete_own_reaction(Emoji::PHONE)
  1106. user = event.message.reacted_with(Emoji::PHONE).first
  1107. bot.send_message(user.dm.id, user.id, false, nil)
  1108. when [:image_application, :yes]
  1109. img = ImageController.edit_image(app)
  1110. char = Character.find(img.char_id)
  1111. user = event.server.member(char.user_id)
  1112. color = CharacterController.type_color(char)
  1113. embed = char_image_embed(char, img, user, color)
  1114. event.message.delete if embed
  1115. channel = if img.category == 'SFW'
  1116. ENV['CHAR_CH'].to_i
  1117. else
  1118. ENV['CHAR_NSFW_CH'].to_i
  1119. end
  1120. bot.send_message(channel, "Image Approved!", false, embed)
  1121. when [:image_application, :no]
  1122. embed = reject_app_embed(app, :image)
  1123. event.message.delete
  1124. reject = event.send_embed("", embed)
  1125. Emoji::IMG_APP.each do |reaction|
  1126. reject.react(reaction)
  1127. end
  1128. reject.react(Emoji::CHECK)
  1129. reject.react(Emoji::CROSS)
  1130. when [:image_rejection, :check]
  1131. user = event.server.member(UID.match(app.description)[1])
  1132. embed = user_img_app(event)
  1133. event.message.delete
  1134. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  1135. bot.send_message(user.dm.id, "", false, embed)
  1136. when [:image_rejection, :cross]
  1137. event.message.delete
  1138. when [:item_application, :yes]
  1139. item = ItemController.edit_item(app)
  1140. embed = item_embed(item)
  1141. event.message.delete
  1142. #bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  1143. when [:item_application, :no]
  1144. embed = reject_app_embed(app)
  1145. event.message.delete
  1146. reject = event.send_embed("", embed)
  1147. reject.react(Emoji::CRAYON)
  1148. reject.react(Emoji::CROSS)
  1149. when [:item_rejection, :crayon]
  1150. embed = self_edit_embed(app, Url::ITEM)
  1151. event.message.delete
  1152. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  1153. when [:item_rejection, :cross]
  1154. event.message.delete
  1155. when [:member, :picture]
  1156. event.message.delete_all_reactions
  1157. char = Character.find(carousel.char_id)
  1158. img = ImageController.img_scroll(
  1159. char_id: char.id,
  1160. nsfw: event.channel.nsfw?,
  1161. )
  1162. carousel.update(id: carousel.id, image_id: img&.id)
  1163. user =
  1164. case
  1165. when char.user_id.match(/public/i)
  1166. "Public"
  1167. when member = event.server.member(char.user_id)
  1168. member
  1169. else
  1170. nil
  1171. end
  1172. embed = character_embed(
  1173. char: char,
  1174. img: img,
  1175. user: user,
  1176. color: CharacterController.type_color(char),
  1177. section: :image,
  1178. event: event
  1179. )
  1180. event.message.edit("", embed)
  1181. arrow_react(event.message)
  1182. when [:member, :bags]
  1183. emoji = Emoji::BAGS
  1184. users = event.message.reacted_with(emoji)
  1185. users.each do |user|
  1186. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1187. end
  1188. char = Character.find(carousel.char_id)
  1189. user =
  1190. case
  1191. when char.user_id.match(/public/i)
  1192. "Public"
  1193. when member = event.server.member(char.user_id)
  1194. member
  1195. else
  1196. nil
  1197. end
  1198. embed = character_embed(
  1199. char: char,
  1200. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1201. user: user,
  1202. color: CharacterController.type_color(char),
  1203. section: :bags,
  1204. event: event
  1205. )
  1206. event.message.edit("", embed)
  1207. when [:member, :family]
  1208. when [:member, :eyes]
  1209. emoji = Emoji::EYES
  1210. users = event.message.reacted_with(emoji)
  1211. users.each do |user|
  1212. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1213. end
  1214. char = Character.find(carousel.char_id)
  1215. user =
  1216. case
  1217. when char.user_id.match(/public/i)
  1218. "Public"
  1219. when member = event.server.member(char.user_id)
  1220. member
  1221. else
  1222. nil
  1223. end
  1224. embed = character_embed(
  1225. char: char,
  1226. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1227. user: user,
  1228. color: CharacterController.type_color(char),
  1229. section: :all,
  1230. event: event
  1231. )
  1232. event.message.edit("", embed)
  1233. when [:member, :bust]
  1234. event.message.delete_all_reactions
  1235. char = Character.find(carousel.char_id)
  1236. chars = []
  1237. actives = []
  1238. embed = case char.user_id
  1239. when /pulic/i, /server/i
  1240. chars = Character.all
  1241. char_list_embed(chars)
  1242. else
  1243. chars = Character.where(user_id: char.user_id)
  1244. user = event.server.member(char.user_id)
  1245. actives = Character.where(user_id: char.user_id, active: 'Active')
  1246. ids = actives.map(&:id)
  1247. carousel.update(options: ids)
  1248. user_char_embed(chars, user)
  1249. end
  1250. event.message.edit("", embed)
  1251. option_react(event.message, actives) unless actives.empty?
  1252. when [:member, :back]
  1253. event.message.delete_all_reactions
  1254. char = Character.find(carousel.char_id)
  1255. user =
  1256. case
  1257. when char.user_id.match(/public/i)
  1258. "Public"
  1259. when member = event.server.member(char.user_id)
  1260. member
  1261. else
  1262. nil
  1263. end
  1264. embed = character_embed(
  1265. char: char,
  1266. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1267. user: user,
  1268. color: CharacterController.type_color(char),
  1269. section: :default,
  1270. event: event
  1271. )
  1272. event.message.edit("", embed)
  1273. section_react(event.message)
  1274. when [:member, :left], [:member, :right]
  1275. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  1276. users = event.message.reacted_with(emoji)
  1277. users.each do |user|
  1278. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1279. end
  1280. char = Character.find(carousel.char_id)
  1281. img = ImageController.img_scroll(
  1282. char_id: char.id,
  1283. nsfw: event.channel.nsfw?,
  1284. img: carousel.image_id,
  1285. dir: vote
  1286. )
  1287. carousel.update(id: carousel.id, image_id: img.id)
  1288. user =
  1289. case
  1290. when char.user_id.match(/public/i)
  1291. "Public"
  1292. when member = event.server.member(char.user_id)
  1293. member
  1294. else
  1295. nil
  1296. end
  1297. embed = character_embed(
  1298. char: char,
  1299. img: img,
  1300. user: user,
  1301. color: CharacterController.type_color(char),
  1302. section: :image,
  1303. event: event
  1304. )
  1305. event.message.edit("", embed)
  1306. when [:member, :number]
  1307. char_index = nil
  1308. Emoji::NUMBERS.each.with_index do |emoji, i|
  1309. char_index = i if reactions[emoji]&.count.to_i > 1
  1310. end
  1311. if char_index
  1312. event.message.delete_all_reactions
  1313. char = Character.find(carousel.options[char_index])
  1314. carousel.update(id: carousel.id, char_id: char.id)
  1315. user =
  1316. case
  1317. when char.user_id.match(/public/i)
  1318. "Public"
  1319. when member = event.server.member(char.user_id)
  1320. member
  1321. else
  1322. nil
  1323. end
  1324. embed = character_embed(
  1325. char: char,
  1326. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1327. user: user,
  1328. color: CharacterController.type_color(char),
  1329. section: :default,
  1330. event: event
  1331. )
  1332. event.message.edit("", embed)
  1333. section_react(event.message)
  1334. end
  1335. when [:member, :cross]
  1336. event.message.delete
  1337. carousel.delete
  1338. when [:landmark, :books]
  1339. emoji = Emoji::BOOKS
  1340. users = event.message.reacted_with(emoji)
  1341. users.each do |user|
  1342. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1343. end
  1344. lm = Landmark.find(carousel.landmark_id)
  1345. user =
  1346. case
  1347. when lm.user_id.match(/server/i)
  1348. "Server Owned"
  1349. when member = event.server.member(lm.user_id)
  1350. member
  1351. else
  1352. nil
  1353. end
  1354. embed = landmark_embed(lm: lm, user: user, section: :history, event: event)
  1355. event.message.edit("", embed)
  1356. when [:landmark, :skull]
  1357. emoji = Emoji::SKULL
  1358. users = event.message.reacted_with(emoji)
  1359. users.each do |user|
  1360. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1361. end
  1362. lm = Landmark.find(carousel.landmark_id)
  1363. user =
  1364. case
  1365. when lm.user_id.match(/server/i)
  1366. "Server Owned"
  1367. when member = event.server.member(lm.user_id)
  1368. member
  1369. else
  1370. nil
  1371. end
  1372. embed = landmark_embed(lm: lm, user: user, section: :warning, event: event)
  1373. event.message.edit("", embed)
  1374. when [:landmark, :map]
  1375. emoji = Emoji::MAP
  1376. users = event.message.reacted_with(emoji)
  1377. users.each do |user|
  1378. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1379. end
  1380. lm = Landmark.find(carousel.landmark_id)
  1381. user =
  1382. case
  1383. when lm.user_id.match(/server/i)
  1384. "Server Owned"
  1385. when member = event.server.member(lm.user_id)
  1386. member
  1387. else
  1388. nil
  1389. end
  1390. embed = landmark_embed(lm: lm, user: user, section: :map, event: event)
  1391. event.message.edit("", embed)
  1392. when [:landmark, :houses]
  1393. emoji = Emoji::HOUSES
  1394. users = event.message.reacted_with(emoji)
  1395. users.each do |user|
  1396. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1397. end
  1398. lm = Landmark.find(carousel.landmark_id)
  1399. user =
  1400. case
  1401. when lm.user_id.match(/server/i)
  1402. "Server Owned"
  1403. when member = event.server.member(lm.user_id)
  1404. member
  1405. else
  1406. nil
  1407. end
  1408. embed = landmark_embed(lm: lm, user: user, section: :layout, event: event)
  1409. event.message.edit("", embed)
  1410. when [:landmark, :people]
  1411. emoji = Emoji::PEOPLE
  1412. users = event.message.reacted_with(emoji)
  1413. users.each do |user|
  1414. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1415. end
  1416. lm = Landmark.find(carousel.landmark_id)
  1417. user =
  1418. case
  1419. when lm.user_id.match(/server/i)
  1420. "Server Owned"
  1421. when member = event.server.member(lm.user_id)
  1422. member
  1423. else
  1424. nil
  1425. end
  1426. embed = landmark_embed(lm: lm, user: user, section: :npcs, event: event)
  1427. event.message.edit("", embed)
  1428. when [:landmark, :cross]
  1429. event.message.delete
  1430. carousel.delete
  1431. when [:team_application, :yes]
  1432. t = Team.find_by(name: app.title)
  1433. if t
  1434. t.update(description: app.description)
  1435. else
  1436. t = Team.create!(name: app.title, description: app.description)
  1437. # create role
  1438. role = event.server.create_role(
  1439. name: t.name,
  1440. colour: 3447003,
  1441. hoist: true,
  1442. mentionable: true,
  1443. reason: "New Team"
  1444. )
  1445. role.sort_above(ENV['TEAM_ROLE'])
  1446. # create channel
  1447. channel = event.server.create_channel(
  1448. t.name,
  1449. parent: 455776627125780489,
  1450. permission_overwrites: [
  1451. { id: event.server.everyone_role.id, deny: 1024 },
  1452. { id: role.id, allow: 1024 }
  1453. ]
  1454. )
  1455. t.update(role: role.id.to_s, channel: channel.id.to_s)
  1456. # embed
  1457. embed = message_embed(
  1458. "Team Approved: #{t.name}!",
  1459. "You can join with ```pkmn-team #{t.name} | apply | character_name```"
  1460. )
  1461. bot.send_message(ENV['TEAM_CH'], "", false, embed)
  1462. event.message.delete
  1463. end
  1464. when [:team_application, :no]
  1465. event.message.delete
  1466. when [:team_request, :yes]
  1467. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1468. char = Character.find(char_id[1].to_i)
  1469. t = Team.find_by(channel: event.message.channel.id.to_s)
  1470. event.message.delete
  1471. if ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  1472. ct.update(active: true)
  1473. else
  1474. CharTeam.create(char_id: char.id, team_id: t.id)
  1475. end
  1476. user = event.server.member(char.user_id)
  1477. user.add_role(t.role.to_i) if user
  1478. embed = message_embed("New Member!", "Welcome #{char.name} to the team!")
  1479. event.send_embed("", embed)
  1480. when [:team_request, :no]
  1481. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1482. char = Character.find(char_id[1])
  1483. t = Team.find_by(channel: event.message.channel.id.to_s)
  1484. bot.send_message(
  1485. ENV['TEAM_CH'],
  1486. "#{char.name} has been declined from team #{t.name}",
  1487. false,
  1488. nil
  1489. )
  1490. event.message.delete
  1491. when [:team_chat, :pin]
  1492. event.message.pin
  1493. end
  1494. end
  1495. # This will trigger when any reaction is removed in discord
  1496. bot.reaction_remove do |event|
  1497. end
  1498. # This will trigger when a member is updated
  1499. bot.member_update do |event|
  1500. end
  1501. # This will trigger when anyone joins the server
  1502. bot.member_join do |event|
  1503. unless User.find_by(id: event.user.id)
  1504. usr = User.create(id: event.user.id)
  1505. usr.make_stats
  1506. end
  1507. end
  1508. # This will trigger when anyone leaves the server
  1509. bot.member_leave do |event|
  1510. end
  1511. # This will trigger when anyone is banned from the server
  1512. bot.user_ban do |event|
  1513. end
  1514. # This will trigger when anyone is un-banned from the server
  1515. bot.user_unban do |event|
  1516. end
  1517. bot.run