bot.rb 52 KB

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