bot.rb 52 KB

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