bot.rb 55 KB

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