bot.rb 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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. maj = 1
  992. :reactivation
  993. else
  994. if event.server == nil
  995. :new_app
  996. elsif carousel&.char_id || carousel&.options
  997. :member
  998. elsif carousel&.landmark_id
  999. :landmark
  1000. elsif team_chat
  1001. :team_chat
  1002. end
  1003. end
  1004. vote =
  1005. case
  1006. when reactions[Emoji::Y]&.count.to_i > maj && star then :yes
  1007. when reactions[Emoji::N]&.count.to_i > maj then :no
  1008. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  1009. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  1010. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  1011. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  1012. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  1013. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  1014. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  1015. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  1016. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  1017. #when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  1018. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  1019. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  1020. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  1021. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  1022. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  1023. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  1024. when reactions[Emoji::BOOKS]&.count.to_i > 1 then :books
  1025. when reactions[Emoji::SKULL]&.count.to_i > 1 then :skull
  1026. when reactions[Emoji::MAP]&.count.to_i > 1 then :map
  1027. when reactions[Emoji::HOUSES]&.count.to_i > 1 then :houses
  1028. when reactions[Emoji::PEOPLE]&.count.to_i > 1 then :people
  1029. when reactions[Emoji::BUST]&.count.to_i > 1 then :bust
  1030. when reactions[Emoji::PIN]&.count.to_i > 0 then :pin
  1031. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  1032. end
  1033. case [form, vote]
  1034. when [:character_application, :yes]
  1035. uid = UID.match(app.description)
  1036. user =
  1037. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  1038. img_url = case
  1039. when !app.thumbnail&.url.nil? then app.thumbnail.url
  1040. when !app.image&.url.nil? then app.image.url
  1041. end
  1042. char = CharacterController.edit_character(app)
  1043. img = ImageController.default_image(
  1044. img_url,
  1045. char.id,
  1046. char.rating
  1047. )if img_url
  1048. color = CharacterController.type_color(char)
  1049. channel = case char.rating
  1050. when /nsfw/i
  1051. ENV['CHAR_NSFW_CH']
  1052. when /hidden/i
  1053. if user&.current_bot?
  1054. event.channel.id
  1055. else
  1056. user.dm&.id
  1057. end
  1058. else
  1059. ENV['CHAR_CH']
  1060. end
  1061. embed = character_embed(
  1062. char: char,
  1063. img: img,
  1064. user: user,
  1065. color: color,
  1066. event: event
  1067. )if char
  1068. if embed
  1069. bot.send_message(
  1070. channel.to_i,
  1071. "Good news, #{uid}! Your character was approved",
  1072. false,
  1073. embed
  1074. )
  1075. event.message.delete
  1076. else
  1077. event.respond(
  1078. "",
  1079. admin_error_embed("Something went wrong when saving application")
  1080. )
  1081. end
  1082. when [:character_application, :no]
  1083. embed = reject_app_embed(app, :character)
  1084. event.message.delete
  1085. reject = event.send_embed("", embed)
  1086. Emoji::CHAR_APP.each do |reaction|
  1087. reject.react(reaction)
  1088. end
  1089. reject.react(Emoji::CHECK)
  1090. reject.react(Emoji::CROSS)
  1091. reject.react(Emoji::CRAYON)
  1092. when [:character_application, :cross]
  1093. event.message.delete
  1094. when [:character_application, :crayon]
  1095. event.message.delete
  1096. bot.send_temporary_message(
  1097. event.channel.id,
  1098. "",
  1099. 35,
  1100. false,
  1101. self_edit_embed(app, Url::CHARACTER)
  1102. )
  1103. when [:character_rejection, :check]
  1104. user = event.server.member(UID.match(app.description)[1])
  1105. embed = user_char_app(event)
  1106. event.message.delete
  1107. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  1108. bot.send_message(user.dm.id, "", false, embed)
  1109. when [:character_rejection, :cross]
  1110. event.message.delete
  1111. when [:character_rejection, :crayon]
  1112. event.message.delete
  1113. bot.send_temporary_message(
  1114. event.channel.id,
  1115. "",
  1116. 35,
  1117. false,
  1118. self_edit_embed(app, Url::CHARACTER)
  1119. )
  1120. when [:reactivation, :yes]
  1121. ch_id = app.author.name.match(/\[(\d)+\]/)
  1122. char = Character.find(ch_id[1])
  1123. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  1124. color = CharacterController.type_color(char)
  1125. user = case char.user_id
  1126. when /server/i
  1127. char.user_id
  1128. else
  1129. event.server.member(char.user_id.to_i)
  1130. end
  1131. channel = case char.rating
  1132. when /nsfw/i
  1133. ENV['CHAR_NSFW_CH']
  1134. when /hidden/i
  1135. user.dm&.id
  1136. else
  1137. ENV['CHAR_CH']
  1138. end
  1139. char.update(active: 'Active')
  1140. char.reload
  1141. embed = character_embed(
  1142. char: char,
  1143. img: img,
  1144. user: user,
  1145. color: color,
  1146. event: event
  1147. )if char
  1148. if embed
  1149. bot.send_message(
  1150. channel.to_i,
  1151. "Good news, <@#{char.user_id}>! Your character was approved",
  1152. false,
  1153. embed
  1154. )
  1155. event.message.delete
  1156. else
  1157. event.respond(
  1158. "",
  1159. admin_error_embed("Something went wrong when saving application")
  1160. )
  1161. end
  1162. when [:reactivation, :no]
  1163. ch_id = app.author.name.match(/\[(\d)+\]/)
  1164. char = Character.find(ch_id[1])
  1165. user = event.server.member(char.user_id)
  1166. embed = char_reactive(Url::CHARACTER, char.edit_url)
  1167. event.message.delete
  1168. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  1169. bot.send_message(user.dm.id, "", false, embed)
  1170. when [:reactivation, :cross]
  1171. event.message.delete
  1172. when [:reactivation, :crayon]
  1173. ch_id = app.author.name.match(/\[(\d)+\]/)
  1174. char = Character.find(ch_id[1])
  1175. event.message.delete
  1176. bot.send_temporary_message(
  1177. event.channel.id,
  1178. "",
  1179. 35,
  1180. false,
  1181. char_reactive(Url::CHARACTER, char.edit_url)
  1182. )
  1183. when [:landmark_application, :yes]
  1184. uid = UID.match(app.description)
  1185. user =
  1186. app.description.match(/server/i) ? 'Server' : event.server.member(uid[1])
  1187. img_url = case
  1188. when !app.thumbnail&.url.nil? then app.thumbnail.url
  1189. when !app.image&.url.nil? then app.image.url
  1190. end
  1191. lm = LandmarkController.edit_landmark(app)
  1192. embed = landmark_embed(lm: lm, user: user, event: event)if lm
  1193. channel = '453277760429883393'
  1194. if embed
  1195. bot.send_message(
  1196. channel.to_i,
  1197. "Good news, #{uid}! Your landmark was approved",
  1198. false,
  1199. embed
  1200. )
  1201. event.message.delete
  1202. else
  1203. event.respond(
  1204. "",
  1205. admin_error_embed("Something went wrong when saving application")
  1206. )
  1207. end
  1208. when [:landmark_application, :cross]
  1209. event.message.delete
  1210. when [:landmark_application, :crayon]
  1211. event.message.delete
  1212. bot.send_temporary_message(
  1213. event.channel.id,
  1214. "",
  1215. 35,
  1216. false,
  1217. self_edit_embed(app, Url::LANDMARK)
  1218. )
  1219. when [:new_app, :phone]
  1220. event.message.delete_own_reaction(Emoji::PHONE)
  1221. user = event.message.reacted_with(Emoji::PHONE).first
  1222. bot.send_message(user.dm.id, user.id, false, nil)
  1223. when [:image_application, :yes]
  1224. img = ImageController.edit_image(app)
  1225. char = Character.find(img.char_id)
  1226. user = event.server.member(char.user_id)
  1227. color = CharacterController.type_color(char)
  1228. embed = char_image_embed(char, img, user, color)
  1229. event.message.delete if embed
  1230. channel = if img.category == 'SFW'
  1231. ENV['CHAR_CH'].to_i
  1232. else
  1233. ENV['CHAR_NSFW_CH'].to_i
  1234. end
  1235. bot.send_message(channel, "Image Approved!", false, embed)
  1236. when [:image_application, :no]
  1237. embed = reject_app_embed(app, :image)
  1238. event.message.delete
  1239. reject = event.send_embed("", embed)
  1240. Emoji::IMG_APP.each do |reaction|
  1241. reject.react(reaction)
  1242. end
  1243. reject.react(Emoji::CHECK)
  1244. reject.react(Emoji::CROSS)
  1245. when [:image_rejection, :check]
  1246. user = event.server.member(UID.match(app.description)[1])
  1247. embed = user_img_app(event)
  1248. event.message.delete
  1249. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  1250. bot.send_message(user.dm.id, "", false, embed)
  1251. when [:image_rejection, :cross]
  1252. event.message.delete
  1253. when [:item_application, :yes]
  1254. item = ItemController.edit_item(app)
  1255. embed = item_embed(item)
  1256. event.message.delete
  1257. #bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  1258. when [:item_application, :no]
  1259. embed = reject_app_embed(app)
  1260. event.message.delete
  1261. reject = event.send_embed("", embed)
  1262. reject.react(Emoji::CRAYON)
  1263. reject.react(Emoji::CROSS)
  1264. when [:item_rejection, :crayon]
  1265. embed = self_edit_embed(app, Url::ITEM)
  1266. event.message.delete
  1267. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  1268. when [:item_rejection, :cross]
  1269. event.message.delete
  1270. when [:member, :picture]
  1271. event.message.delete_all_reactions
  1272. char = Character.find(carousel.char_id)
  1273. img = ImageController.img_scroll(
  1274. char_id: char.id,
  1275. nsfw: event.channel.nsfw?,
  1276. )
  1277. carousel.update(id: carousel.id, image_id: img&.id)
  1278. user =
  1279. case
  1280. when char.user_id.match(/public/i)
  1281. "Public"
  1282. when member = event.server.member(char.user_id)
  1283. member
  1284. else
  1285. nil
  1286. end
  1287. embed = character_embed(
  1288. char: char,
  1289. img: img,
  1290. user: user,
  1291. color: CharacterController.type_color(char),
  1292. section: :image,
  1293. event: event
  1294. )
  1295. event.message.edit("", embed)
  1296. arrow_react(event.message)
  1297. when [:member, :bags]
  1298. emoji = Emoji::BAGS
  1299. users = event.message.reacted_with(emoji)
  1300. users.each do |user|
  1301. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1302. end
  1303. char = Character.find(carousel.char_id)
  1304. user =
  1305. case
  1306. when char.user_id.match(/public/i)
  1307. "Public"
  1308. when member = event.server.member(char.user_id)
  1309. member
  1310. else
  1311. nil
  1312. end
  1313. embed = character_embed(
  1314. char: char,
  1315. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1316. user: user,
  1317. color: CharacterController.type_color(char),
  1318. section: :bags,
  1319. event: event
  1320. )
  1321. event.message.edit("", embed)
  1322. when [:member, :family]
  1323. when [:member, :eyes]
  1324. emoji = Emoji::EYES
  1325. users = event.message.reacted_with(emoji)
  1326. users.each do |user|
  1327. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1328. end
  1329. char = Character.find(carousel.char_id)
  1330. color = CharacterController.type_color(char)
  1331. user =
  1332. case
  1333. when char.user_id.match(/public/i)
  1334. "Public"
  1335. when member = event.server.member(char.user_id)
  1336. member
  1337. else
  1338. nil
  1339. end
  1340. embed = if char.rating == 'NSFW' && !event.channel.nsfw?
  1341. nsfw_char_embed(char: char, user: user, color: color, event: event)
  1342. else
  1343. character_embed(
  1344. char: char,
  1345. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1346. user: user,
  1347. color: color,
  1348. section: :bags,
  1349. event: event
  1350. )
  1351. end
  1352. event.message.edit("", embed)
  1353. when [:member, :bust]
  1354. event.message.delete_all_reactions
  1355. char = Character.find(carousel.char_id)
  1356. chars = []
  1357. actives = []
  1358. embed = case char.user_id
  1359. when /pulic/i, /server/i
  1360. chars = Character.all
  1361. char_list_embed(chars)
  1362. else
  1363. chars = Character.where(user_id: char.user_id)
  1364. user = event.server.member(char.user_id)
  1365. actives = Character.where(user_id: char.user_id, active: 'Active')
  1366. ids = actives.map(&:id)
  1367. carousel.update(options: ids)
  1368. user_char_embed(chars, user)
  1369. end
  1370. event.message.edit("", embed)
  1371. option_react(event.message, actives) unless actives.empty?
  1372. when [:member, :back]
  1373. event.message.delete_all_reactions
  1374. char = Character.find(carousel.char_id)
  1375. color = CharacterController.type_color(char)
  1376. user =
  1377. case
  1378. when char.user_id.match(/public/i)
  1379. "Public"
  1380. when member = event.server.member(char.user_id)
  1381. member
  1382. else
  1383. nil
  1384. end
  1385. embed = if char.rating == 'NSFW' && !event.channel.nsfw?
  1386. nsfw_char_embed(char: char, user: user, color: color, event: event)
  1387. else
  1388. character_embed(
  1389. char: char,
  1390. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1391. user: user,
  1392. color: color,
  1393. section: :bags,
  1394. event: event
  1395. )
  1396. end
  1397. event.message.edit("", embed)
  1398. section_react(event.message)
  1399. when [:member, :left], [:member, :right]
  1400. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  1401. users = event.message.reacted_with(emoji)
  1402. users.each do |user|
  1403. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1404. end
  1405. char = Character.find(carousel.char_id)
  1406. img = ImageController.img_scroll(
  1407. char_id: char.id,
  1408. nsfw: event.channel.nsfw?,
  1409. img: carousel.image_id,
  1410. dir: vote
  1411. )
  1412. carousel.update(id: carousel.id, image_id: img.id)
  1413. user =
  1414. case
  1415. when char.user_id.match(/public/i)
  1416. "Public"
  1417. when member = event.server.member(char.user_id)
  1418. member
  1419. else
  1420. nil
  1421. end
  1422. embed = character_embed(
  1423. char: char,
  1424. img: img,
  1425. user: user,
  1426. color: CharacterController.type_color(char),
  1427. section: :image,
  1428. event: event
  1429. )
  1430. event.message.edit("", embed)
  1431. when [:member, :number]
  1432. char_index = nil
  1433. Emoji::NUMBERS.each.with_index do |emoji, i|
  1434. char_index = i if reactions[emoji]&.count.to_i > 1
  1435. end
  1436. if char_index
  1437. event.message.delete_all_reactions
  1438. char = Character.find(carousel.options[char_index])
  1439. carousel.update(id: carousel.id, char_id: char.id)
  1440. color = CharacterController.type_color(char)
  1441. user =
  1442. case
  1443. when char.user_id.match(/public/i)
  1444. "Public"
  1445. when member = event.server.member(char.user_id)
  1446. member
  1447. else
  1448. nil
  1449. end
  1450. embed = if char.rating == 'NSFW' && !event.channel.nsfw?
  1451. nsfw_char_embed(char: char, user: user, color: color, event: event)
  1452. else
  1453. character_embed(
  1454. char: char,
  1455. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1456. user: user,
  1457. color: color,
  1458. section: :bags,
  1459. event: event
  1460. )
  1461. end
  1462. event.message.edit("", embed)
  1463. section_react(event.message)
  1464. end
  1465. when [:member, :cross]
  1466. event.message.delete
  1467. carousel.delete
  1468. when [:landmark, :books]
  1469. emoji = Emoji::BOOKS
  1470. users = event.message.reacted_with(emoji)
  1471. users.each do |user|
  1472. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1473. end
  1474. lm = Landmark.find(carousel.landmark_id)
  1475. user =
  1476. case
  1477. when lm.user_id.match(/server/i)
  1478. "Server Owned"
  1479. when member = event.server.member(lm.user_id)
  1480. member
  1481. else
  1482. nil
  1483. end
  1484. embed = landmark_embed(lm: lm, user: user, section: :history, event: event)
  1485. event.message.edit("", embed)
  1486. when [:landmark, :skull]
  1487. emoji = Emoji::SKULL
  1488. users = event.message.reacted_with(emoji)
  1489. users.each do |user|
  1490. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1491. end
  1492. lm = Landmark.find(carousel.landmark_id)
  1493. user =
  1494. case
  1495. when lm.user_id.match(/server/i)
  1496. "Server Owned"
  1497. when member = event.server.member(lm.user_id)
  1498. member
  1499. else
  1500. nil
  1501. end
  1502. embed = landmark_embed(lm: lm, user: user, section: :warning, event: event)
  1503. event.message.edit("", embed)
  1504. when [:landmark, :map]
  1505. emoji = Emoji::MAP
  1506. users = event.message.reacted_with(emoji)
  1507. users.each do |user|
  1508. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1509. end
  1510. lm = Landmark.find(carousel.landmark_id)
  1511. user =
  1512. case
  1513. when lm.user_id.match(/server/i)
  1514. "Server Owned"
  1515. when member = event.server.member(lm.user_id)
  1516. member
  1517. else
  1518. nil
  1519. end
  1520. embed = landmark_embed(lm: lm, user: user, section: :map, event: event)
  1521. event.message.edit("", embed)
  1522. when [:landmark, :houses]
  1523. emoji = Emoji::HOUSES
  1524. users = event.message.reacted_with(emoji)
  1525. users.each do |user|
  1526. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1527. end
  1528. lm = Landmark.find(carousel.landmark_id)
  1529. user =
  1530. case
  1531. when lm.user_id.match(/server/i)
  1532. "Server Owned"
  1533. when member = event.server.member(lm.user_id)
  1534. member
  1535. else
  1536. nil
  1537. end
  1538. embed = landmark_embed(lm: lm, user: user, section: :layout, event: event)
  1539. event.message.edit("", embed)
  1540. when [:landmark, :people]
  1541. emoji = Emoji::PEOPLE
  1542. users = event.message.reacted_with(emoji)
  1543. users.each do |user|
  1544. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1545. end
  1546. lm = Landmark.find(carousel.landmark_id)
  1547. user =
  1548. case
  1549. when lm.user_id.match(/server/i)
  1550. "Server Owned"
  1551. when member = event.server.member(lm.user_id)
  1552. member
  1553. else
  1554. nil
  1555. end
  1556. embed = landmark_embed(lm: lm, user: user, section: :npcs, event: event)
  1557. event.message.edit("", embed)
  1558. when [:landmark, :cross]
  1559. event.message.delete
  1560. carousel.delete
  1561. when [:team_application, :yes]
  1562. t = Team.find_by(name: app.title)
  1563. if t
  1564. t.update(description: app.description)
  1565. else
  1566. t = Team.create!(name: app.title, description: app.description)
  1567. # create role
  1568. role = event.server.create_role(
  1569. name: t.name,
  1570. colour: 3447003,
  1571. hoist: true,
  1572. mentionable: true,
  1573. reason: "New Team"
  1574. )
  1575. role.sort_above(ENV['TEAM_ROLE'])
  1576. # create channel
  1577. channel = event.server.create_channel(
  1578. t.name,
  1579. parent: 455776627125780489,
  1580. permission_overwrites: [
  1581. { id: event.server.everyone_role.id, deny: 1024 },
  1582. { id: role.id, allow: 1024 }
  1583. ]
  1584. )
  1585. t.update(role: role.id.to_s, channel: channel.id.to_s)
  1586. # embed
  1587. embed = message_embed(
  1588. "Team Approved: #{t.name}!",
  1589. "You can join with ```pkmn-team #{t.name} | apply | character_name```"
  1590. )
  1591. bot.send_message(ENV['TEAM_CH'], "", false, embed)
  1592. event.message.delete
  1593. end
  1594. when [:team_application, :no]
  1595. event.message.delete
  1596. when [:team_request, :yes]
  1597. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1598. char = Character.find(char_id[1].to_i)
  1599. t = Team.find_by(channel: event.message.channel.id.to_s)
  1600. event.message.delete
  1601. if ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  1602. ct.update(active: true)
  1603. else
  1604. CharTeam.create(char_id: char.id, team_id: t.id)
  1605. end
  1606. user = event.server.member(char.user_id)
  1607. user.add_role(t.role.to_i) if user
  1608. embed = message_embed("New Member!", "Welcome #{char.name} to the team!")
  1609. event.send_embed("", embed)
  1610. when [:team_request, :no]
  1611. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1612. char = Character.find(char_id[1])
  1613. t = Team.find_by(channel: event.message.channel.id.to_s)
  1614. bot.send_message(
  1615. ENV['TEAM_CH'],
  1616. "#{char.name} has been declined from team #{t.name}",
  1617. false,
  1618. nil
  1619. )
  1620. event.message.delete
  1621. when [:team_chat, :pin]
  1622. event.message.pin
  1623. end
  1624. end
  1625. # This will trigger when any reaction is removed in discord
  1626. bot.reaction_remove do |event|
  1627. end
  1628. # This will trigger when a member is updated
  1629. bot.member_update do |event|
  1630. end
  1631. # This will trigger when anyone joins the server
  1632. bot.member_join do |event|
  1633. unless User.find_by(id: event.user.id)
  1634. usr = User.create(id: event.user.id)
  1635. usr.make_stats
  1636. end
  1637. end
  1638. # This will trigger when anyone leaves the server
  1639. bot.member_leave do |event|
  1640. updated = []
  1641. chars = Character.where(user_id: event.user.id)
  1642. chars.each do |char|
  1643. unless char.active == 'NPC'
  1644. char.update(active: 'Deleted')
  1645. char.reload
  1646. end
  1647. updated.push("#{char.name} -- #{char.active}")
  1648. end
  1649. embed = Embed.new(
  1650. title: "I've lost track of a user!",
  1651. description: "It seems <@#{event.user.name}>, (#{event.user.nickname}) has left the server!",
  1652. fields: [
  1653. { name: "```Flagging Guild Members......```", value: updated.join("\n") }
  1654. ]
  1655. )
  1656. bot.send_message(588464466048581632, "", false, embed)
  1657. end
  1658. # This will trigger when anyone is banned from the server
  1659. bot.user_ban do |event|
  1660. end
  1661. # This will trigger when anyone is un-banned from the server
  1662. bot.user_unban do |event|
  1663. end
  1664. bot.run