bot.rb 48 KB

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