bot.rb 53 KB

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