bot.rb 47 KB

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