bot.rb 55 KB

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