bot.rb 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. # Commands: structure basic bot commands here
  35. commands = []
  36. pm_commands = []
  37. hello = Command.new(:hello, "Says hello!") do |event|
  38. user = event.author.nickname || event.author.name
  39. img = ImageUrl.find_by(name: 'happy')
  40. greetings = [
  41. "Hi there, #{user}",
  42. "Greetings #{user}, you lovable bum",
  43. "Alola, #{user}",
  44. "Hello, #{user}! The Guildmasters have been waiting",
  45. "#{user} would like to battle!"
  46. ]
  47. Embed.new(
  48. description: greetings.sample,
  49. color: event.author.color.combined,
  50. thumbnail: {
  51. url: img.url
  52. }
  53. )
  54. end
  55. opts = {
  56. "" => "displays a list of all commands",
  57. "command" => "displays info and usage for specified command"
  58. }
  59. desc = "Displays help information for the commands"
  60. help = Command.new(:help, desc, opts) do |event, command|
  61. if command
  62. short = /pkmn-(\w+)/.match(command)
  63. command = short ? short[1] : command
  64. cmd = commands.detect { |c| c.name == command.to_sym }
  65. pm_cmd = pm_commands.detect { |pc| pc.name == command.to_sym }
  66. end
  67. if command && cmd
  68. command_embed(cmd)
  69. elsif command && pm_cmd
  70. command_embed(pm_cmd, "PM Command")
  71. elsif !command
  72. embed = command_list_embed(
  73. pm_commands,
  74. "Can only be used in a pm with the bot",
  75. "PM Commands"
  76. )
  77. event.send_embed("", embed)
  78. command_list_embed(commands)
  79. else
  80. command_error_embed("Command not found!", help)
  81. end
  82. end
  83. opts = {
  84. "primary" => "Single Display",
  85. "primary | secondary" => "Double Display"
  86. }
  87. desc = "Displays a chart of effectiveness for the given type"
  88. matchup = Command.new(:matchup, desc, opts) do |event, primary, secondary|
  89. channel = event.channel.id
  90. image_out = 'images/Type Double.png'
  91. file_p = "images/Type #{primary.capitalize}.png" if primary
  92. file_s = "images/Type #{secondary.capitalize}.png" if secondary
  93. case
  94. when !file_p
  95. command_error_embed("There was an error processing your request!", matchup)
  96. when !file_s && File.exists?(file_p)
  97. bot.send_file(channel, File.open(file_p, 'r'))
  98. when File.exists?(file_p) && File.exists?(file_s)
  99. append_image(file_p, file_s, image_out)
  100. bot.send_file(channel, File.open(image_out, 'r'))
  101. else
  102. error_embed("Type(s) not found!")
  103. end
  104. end
  105. opts = {
  106. "@user" => "List all user stats",
  107. }
  108. desc = "Shows ones stats, level, rank, and experience"
  109. stats = Command.new(:stats, desc, opts) do |event, name|
  110. case name
  111. when UID
  112. user_id = UID.match(name)
  113. user = event.server.member(user_id[1])
  114. user_url = user.avatar_url if user
  115. when String
  116. #See if you can find the name some other way?
  117. end
  118. channel = event.channel.id
  119. size_width = 570;
  120. size_height = 376;
  121. stats_frame = "images/LevelUp.png"
  122. level_up = "images/LevelUpFont.png"
  123. user_url_img = "images/Image_Builder/user_url_img.png"
  124. output_file = "images/Image_Builder/LevelUp"
  125. Down.download(user_url, destination: user_url_img)
  126. #Gif Destroyer
  127. i = Magick::ImageList.new(user_url_img)
  128. binding.pry
  129. if(i.count > 1)
  130. i[0].write(user_url_img);
  131. end
  132. #End Gif Destroyer
  133. merge_image(
  134. [stats_frame, level_up, user_url_img],
  135. output_file,
  136. size_width,
  137. size_height,
  138. [nil, nil, 19],
  139. [nil, nil, 92],
  140. [size_width, size_width, 165],
  141. [size_height, size_height, 165]
  142. )
  143. # C# code for getting the percantage to the next level
  144. #int levelprior = LevelUp[task].NextLevel - (10 * (LevelUp[task].CurrentLevel - 1) ^ 2);
  145. #double ratio = (double)(LevelUp[task].Messages - levelprior) / (double)(LevelUp[task].NextLevel - levelprior);
  146. ratio = 0.5
  147. gc = Draw.new
  148. gc.font('OpenSans-SemiBold.ttf')
  149. gc.stroke('#39c4ff').fill('#39c4ff')
  150. gc.rectangle(42, 48, 42 + (95 * ratio), 48 + 3)
  151. gc.stroke('none').fill('black')
  152. gc.pointsize('15')
  153. name_sm = "Test Name 1"
  154. gc.text(15,25, name_sm)
  155. level = 'Lv.4'
  156. gc.text(40, 45, level)
  157. level_rank = 'Rank 104'
  158. gc.text(15, 290, level_rank)
  159. exp = '12340'
  160. gc.text(40, 65, exp)
  161. gc.stroke('white').fill('white')
  162. gc.pointsize('30')
  163. name_lg = "Test Name 2"
  164. gc.text(40,330, name_lg)
  165. reached = "reached level 4!"
  166. gc.text(40,360, reached)
  167. gc.stroke('none').fill('black')
  168. gc.pointsize('18')
  169. lvl_max_hp = '10'
  170. gc.text(450, 97, lvl_max_hp)
  171. lvl_atk = '11'
  172. gc.text(450, 127, lvl_atk)
  173. lvl_def = '12'
  174. gc.text(450, 159, lvl_def)
  175. lvl_sp_atk = '13'
  176. gc.text(450, 191, lvl_sp_atk)
  177. lvl_sp_def = '14'
  178. gc.text(450, 222, lvl_sp_def)
  179. lvl_speed = '15'
  180. gc.text(450, 255, lvl_speed)
  181. gc.stroke('none').fill('maroon')
  182. iv_max_hp = '+10'
  183. gc.text(505, 97, iv_max_hp)
  184. iv_atk = '+11'
  185. gc.text(505, 127, iv_atk)
  186. iv_def = '+12'
  187. gc.text(505, 159, iv_def)
  188. iv_sp_atk = '+13'
  189. gc.text(505, 191, iv_sp_atk)
  190. iv_sp_def = '+14'
  191. gc.text(505, 222, iv_sp_def)
  192. iv_speed = '+15'
  193. gc.text(505, 255, iv_speed)
  194. u = Magick::ImageList.new("#{output_file}.png")
  195. gc.draw(u[0])
  196. u.write("#{output_file}.png")
  197. msg = bot.send_file(channel, File.open("#{output_file}.png", 'r'))
  198. end
  199. opts = {
  200. "" => "starts a new app",
  201. "name" => "edits an existing app",
  202. "name | (in)active" => "sets app to active or inactive"
  203. }
  204. desc = "Everything to do with character applications"
  205. app = Command.new(:app, desc, opts) do |event, name, status|
  206. user = event.author
  207. user_name = user.nickname || user.name
  208. color = user.color.combined if user.color
  209. chars = []
  210. character =
  211. if user.roles.map(&:name).include?('Guild Masters')
  212. chars = Character.where(name: name)
  213. chars.first if chars.length == 1
  214. else
  215. Character.where(user_id: user.id).find_by(name: name) if name
  216. end
  217. active = status.match(/(in)?active/i) if status
  218. case
  219. when !chars.empty? && !character
  220. chars.each do |char|
  221. edit_url = Url::CHARACTER + char.edit_url
  222. embed = edit_app_dm(name, edit_url, color)
  223. bot.send_message(
  224. user.dm.id,
  225. "<@#{char.user_id}>'s character:",
  226. false,
  227. embed
  228. )
  229. end
  230. when name && !character
  231. app_not_found_embed(user_name, name)
  232. when status && active && character
  233. character.update!(active: active[0].capitalize)
  234. character.reload
  235. success_embed("Successfully updated #{name} to be #{active[0].downcase}")
  236. when name && character && !status
  237. edit_url = Url::CHARACTER + character.edit_url
  238. embed = edit_app_dm(name, edit_url, color)
  239. bot.send_message(user.dm.id, "", false, embed)
  240. edit_app_embed(user_name, name, color)
  241. when !name && !status
  242. embed = new_app_dm(user_name, user.id, color)
  243. message = bot.send_message(user.dm.id, "", false, embed)
  244. message.react(Emoji::PHONE)
  245. new_app_embed(user_name, color)
  246. else
  247. command_error_embed("There was an error processing your application!", app)
  248. end
  249. end
  250. opts = { "question | option1, option2, etc" => ""}
  251. desc = "Creates a dynamic poll in any channel"
  252. poll = Command.new(:poll, desc, opts) do |event, question, options|
  253. if options
  254. options_array = options.split(/\s?,\s?/)
  255. new_poll_embed(event, question, options_array)
  256. else
  257. command_error_embed("There was an error creating your poll!", poll)
  258. end
  259. end
  260. opts = {
  261. "participants" =>
  262. "Accepts Everyone, Here, or a comma seperated list of names"
  263. }
  264. desc = "Creates a raffle and picks a winner"
  265. raffle = Command.new(:raffle, desc, opts) do |event, participant|
  266. participants =
  267. case participant
  268. when /^everyone$/i
  269. event.server.members
  270. when /^here$/i
  271. event.message.channel.users
  272. else
  273. participant.split(/\s?,\s?/)
  274. end
  275. winner = participants.sample
  276. winner_name =
  277. case winner
  278. when String
  279. winner
  280. else
  281. winner.nickname || winner.username
  282. end
  283. if winner_name
  284. message_embed("Raffle Results!", "Winner: #{winner_name}")
  285. else
  286. command_error_embed("There was an error creating your raffle!", raffle)
  287. end
  288. end
  289. opts = {
  290. "name | keyword | (n)sfw | url" => "add or update image",
  291. "name | keyword | delete" => "remove image",
  292. "name | keyword" => "display image",
  293. "name" => "list all images"
  294. }
  295. desc = "View, add and edit your characters' images"
  296. image = Command.new(:image, desc, opts) do |event, name, keyword, tag, url, id|
  297. user = event.author
  298. char =
  299. if id
  300. Character.where(user_id: id).find_by!(name: name) if name
  301. else
  302. Character.where(user_id: user.id).find_by!(name: name) if name
  303. end
  304. color = CharacterController.type_color(char) if char
  305. img = CharImage.where(char_id: char.id).find_by(keyword: keyword) if keyword
  306. case
  307. when /^Default$/i.match(keyword)
  308. error_embed(
  309. "Cannot update Default here!",
  310. "Use `pkmn-app character` to edit your default image in your form"
  311. )
  312. when char && keyword && url && tag && tag.match(/(n)?sfw/i)
  313. img_app = CharImage.to_form(
  314. char: char,
  315. keyword: keyword,
  316. category: tag,
  317. url: url,
  318. user_id: user.id
  319. )
  320. approval = bot.send_message(ENV['APP_CH'].to_i, "", false, img_app)
  321. approval.react(Emoji::Y)
  322. approval.react(Emoji::N)
  323. success_embed("Your image has been submitted for approval!")
  324. when char && img && tag && tag.match(/delete/i)
  325. CharImage.find(img.id).delete
  326. success_embed("Removed image: #{keyword}")
  327. when char && img && !tag
  328. char_image_embed(char, img, user, color)
  329. when char && !keyword
  330. imgs = CharImage.where(char_id: char.id)
  331. image_list_embed(char, imgs, user, color)
  332. when keyword && !img
  333. error_embed("Could not find your image!")
  334. else
  335. command_error_embed("Could not process your image request!", image)
  336. end
  337. rescue ActiveRecord::RecordNotFound
  338. error_embed(
  339. "Character not Found!",
  340. "I could not find your character named #{name}"
  341. )
  342. end
  343. opts = {
  344. "" => "List all guild members",
  345. "@user" => "List all characters belonging to the user",
  346. "name " => "Display the given character",
  347. "name | section" => "Display the given section for the character",
  348. "name | image | keword" => "Display the given image"
  349. }
  350. desc = "Display info about the guild members"
  351. member = Command.new(:member, desc, opts) do |event, name, section, keyword|
  352. sections = [:all, :default, :bio, :type, :status, :rumors, :image, :bags]
  353. case name
  354. when UID
  355. user_id = UID.match(name)
  356. when String
  357. chars = Character.where(name: name)
  358. char = chars.first if chars.length == 1
  359. if char
  360. img = CharImage.where(char_id: char.id).find_by(keyword: 'Default')
  361. user = char.user_id.match(/public/i) ?
  362. char.user_id : event.server.member(char.user_id)
  363. color = CharacterController.type_color(char)
  364. end
  365. end
  366. case
  367. when !name
  368. chars = Character.all
  369. char_list_embed(chars)
  370. when name && user_id
  371. chars = Character.where(user_id: user_id[1])
  372. user = event.server.member(user_id[1])
  373. chars_id = []
  374. chars.each do |char|
  375. chars_id.push char.id if char.active == 'Active'
  376. end
  377. embed = user_char_embed(chars, user)
  378. msg = event.send_embed("", embed)
  379. Carousel.create(message_id: msg.id, options: chars_id)
  380. option_react(msg, chars_id)
  381. when name && chars.empty?
  382. error_embed(
  383. "Character not found!",
  384. "Could not find a character named #{name}"
  385. )
  386. when name && chars && !char
  387. embed = dup_char_embed(chars, name)
  388. chars_id = chars.map(&:id)
  389. msg = event.send_embed("", embed)
  390. Carousel.create(message_id: msg.id, options: chars_id)
  391. option_react(msg, chars_id)
  392. when name && char && !section
  393. embed = character_embed(
  394. char: char,
  395. img: img,
  396. section: :default,
  397. user: user,
  398. color: color
  399. )
  400. msg = event.send_embed("", embed)
  401. Carousel.create(message_id: msg.id, char_id: char.id)
  402. section_react(msg)
  403. when char && section && keyword
  404. embed = command_error_embed(
  405. "Invalid Arguments",
  406. member
  407. )unless /image/i.match(section)
  408. unless embed
  409. img = CharImage.where(char_id: char.id).find_by!(keyword: keyword)
  410. embed = error_embed(
  411. "Wrong Channel!",
  412. "The requested image is NSFW"
  413. )if img.category == 'NSFW' && !event.channel.nsfw?
  414. end
  415. unless embed
  416. embed = character_embed(
  417. char: char,
  418. img: img,
  419. section: :image,
  420. user: user,
  421. color: color
  422. )
  423. msg = event.send_embed("", embed)
  424. Carousel.create(message_id: msg.id, char_id: char.id, image_id: img.id)
  425. arrow_react(msg)
  426. end
  427. embed
  428. when name && char && section
  429. sect = section.downcase.to_sym
  430. nsfw = event.channel.nsfw?
  431. img = ImageController.img_scroll(
  432. char_id: char.id,
  433. nsfw: nsfw
  434. )if section == :image
  435. if sections.detect{ |s| s == sect }
  436. embed = character_embed(
  437. char: char,
  438. img: img,
  439. section: sect,
  440. user: user,
  441. color: color,
  442. )
  443. msg = event.send_embed("", embed)
  444. Carousel.create(
  445. message_id: msg.id,
  446. char_id: char.id,
  447. image_id: img ? img.id : nil
  448. )
  449. if sect == :image
  450. arrow_react(msg)
  451. else
  452. section_react(msg)
  453. end
  454. else
  455. error_embed("Invalid Section!")
  456. end
  457. end
  458. rescue ActiveRecord::RecordNotFound => e
  459. error_embed("Record Not Found!", e.message)
  460. end
  461. item = Command.new(:item, desc, opts) do |event, name|
  462. i = name ? Item.find_by!(name: name.capitalize) : Item.all
  463. case
  464. when name && i
  465. item_embed(i)
  466. when !name && i
  467. item_list_embed(i)
  468. else
  469. command_error_embed("Error proccessing your request!", item)
  470. end
  471. rescue ActiveRecord::RecordNotFound
  472. error_embed("Item Not Found!")
  473. end
  474. desc = "Add and remove items from characters' inventories"
  475. opts = { "item | (-/+) amount | character" => "" }
  476. inv = Command.new(:inv, desc, opts) do |event, item, amount, name|
  477. char = Character.find_by!(name: name) if name
  478. itm = Item.find_by!(name: item) if item
  479. amt = amount.to_i
  480. case
  481. when char && itm && amt
  482. i = Inventory.update_inv(itm, amt, char)
  483. user = event.server.member(char.user_id.to_i)
  484. color = CharacterController.type_color(char)
  485. case i
  486. when Inventory, true
  487. character_embed(char: char, user: user, color: color, section: :bags)
  488. when Embed
  489. i
  490. else
  491. error_embed("Something went wrong!", "Could not update inventory")
  492. end
  493. else
  494. command_error_embed("Could not proccess your request", inv)
  495. end
  496. rescue ActiveRecord::RecordNotFound => e
  497. error_embed(e.message)
  498. end
  499. desc = "Update or edit statuses"
  500. opts = { "name | effect" => "" }
  501. status = Command.new(:status, desc, opts) do |event, name, effect|
  502. if name && effect
  503. s = StatusController.edit_status(name, effect)
  504. case s
  505. when Status
  506. success_embed("Created Status: #{name}")
  507. when Embed
  508. s
  509. end
  510. else
  511. command_error_embed("Could not create status!", status)
  512. end
  513. end
  514. opts = { "character | ailment | %afflicted" => "" }
  515. afflict = Command.new(:afflict, desc, opts) do |event, name, status, amount|
  516. char = Character.find_by!(name: name) if name
  517. st = Status.find_by!(name: status) if status
  518. user = char.user_id.match(/public/i) ?
  519. 'Public' : event.server.member(char.user_id)
  520. if st && amount && char
  521. user = char.user_id.match(/public/i) ?
  522. 'Public' : event.server.member(char.user_id)
  523. color = CharacterController.type_color(char)
  524. s = StatusController.edit_char_status(st, amount, char)
  525. case s
  526. when CharStatus
  527. character_embed(char: char, user: user, color: color, section: :status)
  528. when Embed
  529. s
  530. end
  531. else
  532. command_error_embed("Error afflicting #{char}", afflict)
  533. end
  534. rescue ActiveRecord::RecordNotFound => e
  535. error_embed(e.message)
  536. end
  537. opts = {
  538. "character | all" => "completely cures all ailments",
  539. "character | ailment" => "completely cures the ailment",
  540. "character | ailment | %cured" => "cures a percentage of ailment"
  541. }
  542. cure = Command.new(:cure, desc, opts) do |event, name, status, amount|
  543. char = Character.find_by!(name: name) if name
  544. st = Status.find_by!(name: status) if status && !status.match(/all/i)
  545. case
  546. when char && st && amount
  547. user = char.user_id.match(/public/i) ?
  548. 'Public' : event.server.member(char.user_id)
  549. color = CharacterController.type_color(char)
  550. s = StatusController.edit_char_status(st, "-#{amount}", char)
  551. case s
  552. when CharStatus
  553. character_embed(char: char, user: user, color: color, section: :status)
  554. when Embed
  555. s
  556. end
  557. when char && st && !amount
  558. CharStatus.where(char_id: char.id).find_by!(status_id: st.id).delete
  559. success_embed("Removed #{status} from #{name}")
  560. when char && status && status.match(/all/i)
  561. csts = CharStatus.where(char_id: char.id)
  562. csts.each do |cst|
  563. cst.delete
  564. end
  565. success_embed("Removed all ailments from #{name}")
  566. else
  567. end
  568. rescue ActiveRecord::RecordNotFound => e
  569. error_embed(e.message)
  570. end
  571. desc = "Everything to do with rescue teams"
  572. opts = {
  573. "" => "list of teams",
  574. "team_name" => "display team info",
  575. "team_name | (leave/apply) | character" => "leave or apply for team",
  576. "team_name | create | description" => "admin only command"
  577. }
  578. team = Command.new(:team, desc, opts) do |event, team_name, action, desc|
  579. unless action&.match(/create/i)
  580. t = Team.find_by!(name: team_name) if team_name
  581. char = if event.author.roles.map(&:name).include?('Guild Masters')
  582. Character.find_by!(name: desc) if desc
  583. else
  584. c = Character.where(user_id: event.author.id).find_by!(name: desc)
  585. ct = CharTeam.where(char_id: c.id).find_by(active: true)
  586. action = "second_team" if ct && action.match(/apply/i)
  587. c
  588. end
  589. end
  590. case action
  591. when /leave/i
  592. ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  593. if ct
  594. ct.update(active: false)
  595. user = event.server.member(char.user_id.to_i)
  596. user.remove_role(t.role.to_i) if user
  597. end
  598. bot.send_message(
  599. t.channel.to_i,
  600. "#{char.name} has left the team",
  601. false,
  602. nil
  603. )
  604. when /apply/i
  605. members = CharTeam.where(team_id: t.id)
  606. if members.count >= 6
  607. embed = team_app_embed(t, char, event.server.member(char.user_id))
  608. msg = bot.send_message(t.channel.to_i, "", false, embed)
  609. msg.react(Emoji::Y)
  610. msg.react(Emoji::N)
  611. success_embed("Your request has been posted to #{t.name}!")
  612. else
  613. error_embed("#{t.name} is full!")
  614. end
  615. when /create/i
  616. team_name = team_name || ""
  617. desc = desc || ""
  618. embed = new_team_embed(event.message.author, team_name, desc)
  619. msg = bot.send_message(ENV['APP_CH'], "", false, embed)
  620. msg.react(Emoji::Y)
  621. msg.react(Emoji::N)
  622. success_embed("Your Team Application has been submitted!")
  623. when nil
  624. t ? team_embed(t) : teams_embed()
  625. when /second_team/i
  626. error_embed("#{char.name} is already in a team!")
  627. else
  628. command_error_embed("Could not process team request!", team)
  629. end
  630. rescue ActiveRecord::RecordNotFound => e
  631. error_embed(e.message)
  632. end
  633. # ---
  634. commands = [
  635. hello,
  636. matchup,
  637. app,
  638. help,
  639. poll,
  640. raffle,
  641. member,
  642. item,
  643. inv,
  644. <<<<<<< 77a766d541086a3ee08efe0eebbbcad8820c7865
  645. status,
  646. afflict,
  647. cure,
  648. team,
  649. ||||||| merged common ancestors
  650. weather,
  651. =======
  652. >>>>>>> level up
  653. stats
  654. ]
  655. #locked_commands = [inv]
  656. # This will trigger on every message sent in discord
  657. bot.message do |event|
  658. content = event.message.content
  659. author = event.author.id
  660. command = /^pkmn-(\w+)/.match(content)
  661. cmd = commands.detect { |c| c.name == command[1].to_sym } if command
  662. if cmd
  663. reply = cmd.call(content, event)
  664. case reply
  665. when Embed
  666. event.send_embed("", reply)
  667. when String
  668. event.respond(reply)
  669. end
  670. elsif command && !cmd && event.server
  671. event.send_embed(
  672. "",
  673. error_embed("Command not found!")
  674. )
  675. elsif author == ENV['WEBHOOK'].to_i
  676. app = event.message.embeds.first
  677. if app.author.name == 'Character Application'
  678. Character.check_user(event)
  679. else
  680. approval_react(event)
  681. end
  682. #elsif content == 'test'
  683. #User.import_user(File.open('users.txt', 'r'))
  684. elsif content == 'pry'
  685. binding.pry
  686. elsif content == 'show me dem illegals'
  687. users = User.all
  688. illegals = []
  689. users.each do |u|
  690. allowed = u.level / 10 + 1
  691. active = Character.where(user_id: u.id, active: 'Active').count
  692. illegals.push("<@#{u.id}>, Level #{u.level}: #{active}/#{allowed}") if active > allowed
  693. end
  694. embed = error_embed("Members with too many pokemon", illegals.join("\n"))
  695. event.send_embed("", embed)
  696. elsif !event.author.current_bot?
  697. usr = User.find_by(id: author.to_s)
  698. msg = URL.match(content) ? content.gsub(URL, "x" * 149) : content
  699. usr = usr.update_xp(msg)
  700. event.respond(usr) if usr.is_a? String
  701. end
  702. end
  703. pm_commands = [ image ]
  704. # This will trigger when a dm is sent to the bot from a user
  705. bot.pm do |event|
  706. content = event.message.content
  707. command = /^pkmn-(\w+)/.match(content)
  708. cmd = pm_commands.detect { |c| c.name == command[1].to_sym } if command
  709. reply = cmd.call(content, event) if cmd
  710. case reply
  711. when Embed
  712. event.send_embed("", reply)
  713. when String
  714. event.respond(reply)
  715. end
  716. end
  717. # This will trigger when any reaction is added in discord
  718. bot.reaction_add do |event|
  719. reactions = event.message.reactions
  720. app = event.message.embeds.first
  721. carousel = Carousel.find_by(message_id: event.message.id)
  722. app = event.message.embeds.first
  723. carousel = Carousel.find_by(message_id: event.message.id)
  724. carousel = Carousel.find_by(message_id: event.message.id)
  725. maj = 100
  726. form =
  727. case app.author&.name
  728. when 'New App' then :new_app
  729. when 'Character Application'
  730. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  731. maj = m.count > 2 ? m.count/2.0 : 2
  732. :character_application
  733. when 'Character Rejection' then :character_rejection
  734. when 'Image Application'
  735. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  736. maj = m.count > 2 ? m.count/2.0 : 2
  737. :image_application
  738. when 'Image Rejection' then :image_rejection
  739. when 'Item Application'
  740. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  741. maj = m.count > 2 ? m.count/2.0 : 2
  742. :item_application
  743. when 'Item Rejection' then :item_rejection
  744. when 'Team Application'
  745. m = event.server.roles.find{ |r| r.id == ENV['ADMINS'].to_i }.members
  746. maj = m.count > 2 ? m.count/2.0 : 2
  747. :team_application
  748. when 'Team Join Request'
  749. team_id = Team.find_by(channel: event.message.channel.id.to_s).role.to_i
  750. m = event.server.roles.find{ |r| r.id == team_id }.members
  751. maj = m.count > 2 ? m.count/2.0 : 2
  752. :team_request
  753. else
  754. :carousel if carousel
  755. end
  756. vote =
  757. case
  758. when reactions[Emoji::Y]&.count.to_i > maj.round then :yes
  759. when reactions[Emoji::N]&.count.to_i > maj.round then :no
  760. when reactions[Emoji::CHECK]&.count.to_i > 1 then :check
  761. when reactions[Emoji::CROSS]&.count.to_i > 1 then :cross
  762. when reactions[Emoji::CRAYON]&.count.to_i > 1 then :crayon
  763. when reactions[Emoji::NOTEBOOK]&.count.to_i > 1 then :notebook
  764. when reactions[Emoji::QUESTION]&.count.to_i > 1 then :question
  765. when reactions[Emoji::PALLET]&.count.to_i > 1 then :pallet
  766. when reactions[Emoji::EAR]&.count.to_i > 1 then :ear
  767. when reactions[Emoji::PICTURE]&.count.to_i > 1 then :picture
  768. when reactions[Emoji::BAGS]&.count.to_i > 1 then :bags
  769. #when reactions[Emoji::FAMILY]&.count.to_i > 1 then :family
  770. when reactions[Emoji::EYES]&.count.to_i > 1 then :eyes
  771. when reactions[Emoji::KEY]&.count.to_i > 1 then :key
  772. when reactions[Emoji::PHONE]&.count.to_i > 1 then :phone
  773. when reactions[Emoji::LEFT]&.count.to_i > 1 then :left
  774. when reactions[Emoji::RIGHT]&.count.to_i > 1 then :right
  775. when reactions[Emoji::UNDO]&.count.to_i > 1 then :back
  776. when reactions.any? { |k,v| Emoji::NUMBERS.include? k } then :number
  777. end
  778. case [form, vote]
  779. when [:character_application, :yes]
  780. uid = UID.match(app.description)
  781. user =
  782. app.description.match(/public/i) ? 'Public' : event.server.member(uid[1])
  783. char = CharacterController.edit_character(app)
  784. img = ImageController.default_image(
  785. app.thumbnail.url,
  786. char.id
  787. )if app.thumbnail
  788. color = CharacterController.type_color(char)
  789. embed = character_embed(
  790. char: char,
  791. img: img,
  792. user: user,
  793. color: color
  794. )if char
  795. if embed
  796. bot.send_message(
  797. ENV['CHAR_CH'].to_i,
  798. "Good news, #{uid}! Your character was approved",
  799. false,
  800. embed
  801. )
  802. event.message.delete
  803. else
  804. event.respond(
  805. "",
  806. admin_error_embed("Something went wrong when saving application")
  807. )
  808. end
  809. when [:character_application, :no]
  810. embed = reject_app_embed(app, :character)
  811. event.message.delete
  812. reject = event.send_embed("", embed)
  813. Emoji::CHAR_APP.each do |reaction|
  814. reject.react(reaction)
  815. end
  816. reject.react(Emoji::CHECK)
  817. reject.react(Emoji::CROSS)
  818. reject.react(Emoji::CRAYON)
  819. when [:character_application, :cross]
  820. event.message.delete
  821. when [:character_rejection, :check]
  822. user = event.server.member(UID.match(app.description)[1])
  823. embed = user_char_app(event)
  824. event.message.delete
  825. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  826. bot.send_message(user.dm.id, "", false, embed)
  827. when [:character_rejection, :cross]
  828. event.message.delete
  829. when [:character_rejection, :crayon]
  830. event.message.delete
  831. bot.send_temporary_message(
  832. event.channel.id,
  833. "",
  834. 35,
  835. false,
  836. self_edit_embed(app, Url::CHARACTER)
  837. )
  838. when [:new_app, :phone]
  839. event.message.delete_own_reaction(Emoji::PHONE)
  840. user = event.message.reacted_with(Emoji::PHONE).first
  841. bot.send_message(user.dm.id, user.id, false, nil)
  842. when [:image_application, :yes]
  843. img = ImageController.edit_image(app)
  844. char = Character.find(img.char_id)
  845. user = event.server.member(char.user_id)
  846. color = CharacterController.type_color(char)
  847. embed = char_image_embed(char, img, user, color)
  848. event.message.delete if embed
  849. channel = if img.category == 'SFW'
  850. ENV['CHAR_CH'].to_i
  851. else
  852. ENV['CHAR_CH_NSFW'].to_i
  853. end
  854. bot.send_message(channel, "Image Approved!", false, embed)
  855. when [:image_application, :no]
  856. embed = reject_app_embed(app, :image)
  857. event.message.delete
  858. reject = event.send_embed("", embed)
  859. Emoji::IMG_APP.each do |reaction|
  860. reject.react(reaction)
  861. end
  862. reject.react(Emoji::CHECK)
  863. reject.react(Emoji::CROSS)
  864. when [:image_rejection, :check]
  865. user = event.server.member(UID.match(app.description)[1])
  866. embed = user_img_app(event)
  867. event.message.delete
  868. bot.send_temporary_message(event.channel.id, "", 5, false, embed)
  869. bot.send_message(user.dm.id, "", false, embed)
  870. when [:image_rejection, :cross]
  871. event.message.delete
  872. when [:item_application, :yes]
  873. item = ItemController.edit_item(app)
  874. embed = item_embed(item)
  875. event.message.delete
  876. bot.send_message(ENV['CHAR_CH'], "New Item!", false, embed)
  877. when [:item_application, :no]
  878. embed = reject_app_embed(app)
  879. event.message.delete
  880. reject = event.send_embed("", embed)
  881. reject.react(Emoji::CRAYON)
  882. reject.react(Emoji::CROSS)
  883. when [:item_rejection, :crayon]
  884. embed = self_edit_embed(app, Url::ITEM)
  885. event.message.delete
  886. bot.send_temporary_message(event.channel.id, "", 25, false, embed)
  887. when [:item_rejection, :cross]
  888. event.message.delete
  889. when [:carousel, :notebook]
  890. emoji = Emoji::NOTEBOOK
  891. users = event.message.reacted_with(emoji)
  892. users.each do |user|
  893. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  894. end
  895. char = Character.find(carousel.char_id)
  896. user =
  897. case
  898. when char.user_id.match(/public/i)
  899. "Public"
  900. when member = event.server.member(char.user_id)
  901. member
  902. else
  903. nil
  904. end
  905. embed = character_embed(
  906. char: char,
  907. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  908. user: user,
  909. color: CharacterController.type_color(char),
  910. section: :bio
  911. )
  912. event.message.edit("", embed)
  913. when [:carousel, :question]
  914. emoji = Emoji::QUESTION
  915. users = event.message.reacted_with(emoji)
  916. users.each do |user|
  917. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  918. end
  919. char = Character.find(carousel.char_id)
  920. user =
  921. case
  922. when char.user_id.match(/public/i)
  923. "Public"
  924. when member = event.server.member(char.user_id)
  925. member
  926. else
  927. nil
  928. end
  929. embed = character_embed(
  930. char: char,
  931. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  932. user: user,
  933. color: CharacterController.type_color(char),
  934. section: :status
  935. )
  936. event.message.edit("", embed)
  937. when [:carousel, :pallet]
  938. emoji = Emoji::PALLET
  939. users = event.message.reacted_with(emoji)
  940. users.each do |user|
  941. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  942. end
  943. char = Character.find(carousel.char_id)
  944. user =
  945. case
  946. when char.user_id.match(/public/i)
  947. "Public"
  948. when member = event.server.member(char.user_id)
  949. member
  950. else
  951. nil
  952. end
  953. embed = character_embed(
  954. char: char,
  955. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  956. user: user,
  957. color: CharacterController.type_color(char),
  958. section: :type
  959. )
  960. event.message.edit("", embed)
  961. when [:carousel, :ear]
  962. emoji = Emoji::EAR
  963. users = event.message.reacted_with(emoji)
  964. users.each do |user|
  965. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  966. end
  967. char = Character.find(carousel.char_id)
  968. user =
  969. case
  970. when char.user_id.match(/public/i)
  971. "Public"
  972. when member = event.server.member(char.user_id)
  973. member
  974. else
  975. nil
  976. end
  977. embed = character_embed(
  978. char: char,
  979. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  980. user: user,
  981. color: CharacterController.type_color(char),
  982. section: :rumors
  983. )
  984. event.message.edit("", embed)
  985. when [:carousel, :picture]
  986. event.message.delete_all_reactions
  987. char = Character.find(carousel.char_id)
  988. img = ImageController.img_scroll(
  989. char_id: char.id,
  990. nsfw: event.channel.nsfw?,
  991. )
  992. carousel.update(id: carousel.id, image_id: img.id)
  993. user =
  994. case
  995. when char.user_id.match(/public/i)
  996. "Public"
  997. when member = event.server.member(char.user_id)
  998. member
  999. else
  1000. nil
  1001. end
  1002. embed = character_embed(
  1003. char: char,
  1004. img: img,
  1005. user: user,
  1006. color: CharacterController.type_color(char),
  1007. section: :image
  1008. )
  1009. event.message.edit("", embed)
  1010. arrow_react(event.message)
  1011. when [:carousel, :bags]
  1012. emoji = Emoji::BAGS
  1013. users = event.message.reacted_with(emoji)
  1014. users.each do |user|
  1015. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1016. end
  1017. char = Character.find(carousel.char_id)
  1018. user =
  1019. case
  1020. when char.user_id.match(/public/i)
  1021. "Public"
  1022. when member = event.server.member(char.user_id)
  1023. member
  1024. else
  1025. nil
  1026. end
  1027. embed = character_embed(
  1028. char: char,
  1029. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1030. user: user,
  1031. color: CharacterController.type_color(char),
  1032. section: :bags
  1033. )
  1034. event.message.edit("", embed)
  1035. when [:carousel, :family]
  1036. when [:carousel, :eyes]
  1037. emoji = Emoji::EYES
  1038. users = event.message.reacted_with(emoji)
  1039. users.each do |user|
  1040. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1041. end
  1042. char = Character.find(carousel.char_id)
  1043. user =
  1044. case
  1045. when char.user_id.match(/public/i)
  1046. "Public"
  1047. when member = event.server.member(char.user_id)
  1048. member
  1049. else
  1050. nil
  1051. end
  1052. embed = character_embed(
  1053. char: char,
  1054. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1055. user: user,
  1056. color: CharacterController.type_color(char),
  1057. section: :all
  1058. )
  1059. event.message.edit("", embed)
  1060. when [:carousel, :key]
  1061. emoji = Emoji::KEY
  1062. users = event.message.reacted_with(emoji)
  1063. users.each do |user|
  1064. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1065. end
  1066. char = Character.find(carousel.char_id)
  1067. user =
  1068. case
  1069. when char.user_id.match(/public/i)
  1070. "Public"
  1071. when member = event.server.member(char.user_id)
  1072. member
  1073. else
  1074. nil
  1075. end
  1076. embed = character_embed(
  1077. char: char,
  1078. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1079. user: user,
  1080. color: CharacterController.type_color(char),
  1081. section: :default
  1082. )
  1083. event.message.edit("", embed)
  1084. when [:carousel, :back]
  1085. event.message.delete_all_reactions
  1086. char = Character.find(carousel.char_id)
  1087. user =
  1088. case
  1089. when char.user_id.match(/public/i)
  1090. "Public"
  1091. when member = event.server.member(char.user_id)
  1092. member
  1093. else
  1094. nil
  1095. end
  1096. embed = character_embed(
  1097. char: char,
  1098. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1099. user: user,
  1100. color: CharacterController.type_color(char),
  1101. section: :default
  1102. )
  1103. event.message.edit("", embed)
  1104. section_react(event.message)
  1105. when [:carousel, :left], [:carousel, :right]
  1106. emoji = vote == :left ? Emoji::LEFT : Emoji::RIGHT
  1107. users = event.message.reacted_with(emoji)
  1108. users.each do |user|
  1109. event.message.delete_reaction(user.id, emoji) unless user.current_bot?
  1110. end
  1111. char = Character.find(carousel.char_id)
  1112. img = ImageController.img_scroll(
  1113. char_id: char.id,
  1114. nsfw: event.channel.nsfw?,
  1115. img: carousel.image_id,
  1116. dir: vote
  1117. )
  1118. carousel.update(id: carousel.id, image_id: img.id)
  1119. user =
  1120. case
  1121. when char.user_id.match(/public/i)
  1122. "Public"
  1123. when member = event.server.member(char.user_id)
  1124. member
  1125. else
  1126. nil
  1127. end
  1128. embed = character_embed(
  1129. char: char,
  1130. img: img,
  1131. user: user,
  1132. color: CharacterController.type_color(char),
  1133. section: :image
  1134. )
  1135. event.message.edit("", embed)
  1136. when [:carousel, :number]
  1137. char_index = nil
  1138. Emoji::NUMBERS.each.with_index do |emoji, i|
  1139. char_index = i if reactions[emoji]&.count.to_i > 1
  1140. end
  1141. if char_index
  1142. event.message.delete_all_reactions
  1143. char = Character.find(carousel.options[char_index])
  1144. carousel.update(id: carousel.id, char_id: char.id)
  1145. user =
  1146. case
  1147. when char.user_id.match(/public/i)
  1148. "Public"
  1149. when member = event.server.member(char.user_id)
  1150. member
  1151. else
  1152. nil
  1153. end
  1154. embed = character_embed(
  1155. char: char,
  1156. img: CharImage.where(char_id: char.id).find_by(keyword: 'Default'),
  1157. user: user,
  1158. color: CharacterController.type_color(char),
  1159. section: :default
  1160. )
  1161. event.message.edit("", embed)
  1162. section_react(event.message)
  1163. end
  1164. when [:carousel, :cross]
  1165. event.message.delete
  1166. carousel.delete
  1167. when [:team_application, :yes]
  1168. t = Team.create!(name: app.title, description: app.description)
  1169. # create role
  1170. role = event.server.create_role(
  1171. name: t.name,
  1172. colour: 3447003,
  1173. hoist: true,
  1174. mentionable: true,
  1175. reason: "New Team"
  1176. )
  1177. #role.sort_above(ENV['TEAM_ROLE'])
  1178. # create channel
  1179. channel = event.server.create_channel(
  1180. t.name,
  1181. parent: 642055732321189928,
  1182. permission_overwrites: [
  1183. { id: event.server.everyone_role.id, deny: 1024 },
  1184. { id: role.id, allow: 1024 }
  1185. ]
  1186. )
  1187. t.update(role: role.id.to_s, channel: channel.id.to_s)
  1188. # embed
  1189. embed = message_embed(
  1190. "Team Approved: #{t.name}!",
  1191. "You can join with ```pkmn-team #{t.name} | apply | character_name```"
  1192. )
  1193. bot.send_message(ENV['TEAM_CH'], "", false, embed)
  1194. event.message.delete
  1195. when [:team_application, :no]
  1196. event.message.delete
  1197. when [:team_request, :yes]
  1198. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1199. char = Character.find(char_id[1].to_i)
  1200. t = Team.find_by(channel: event.message.channel.id.to_s)
  1201. event.message.delete
  1202. if ct = CharTeam.where(team_id: t.id).find_by(char_id: char.id)
  1203. ct.update(active: true)
  1204. else
  1205. CharTeam.create(char_id: char.id, team_id: t.id)
  1206. end
  1207. user = event.server.member(char.user_id)
  1208. user.add_role(t.role.to_i) if user
  1209. embed = message_embed("New Member!", "Welcome #{char.name} to the team!")
  1210. event.send_embed("", embed)
  1211. when [:team_request, :no]
  1212. char_id = /\s\|\s([0-9]+)$/.match(app.footer.text)
  1213. char = Character.find(char_id[1])
  1214. t = Team.find_by(channel: event.message.channel.id.to_s)
  1215. bot.send_message(
  1216. ENV['TEAM_CH'],
  1217. "#{char.name} has been declined from team #{t.name}",
  1218. false,
  1219. nil
  1220. )
  1221. event.message.delete
  1222. end
  1223. end
  1224. # This will trigger when any reaction is removed in discord
  1225. bot.reaction_remove do |event|
  1226. end
  1227. # This will trigger when a member is updated
  1228. bot.member_update do |event|
  1229. end
  1230. # This will trigger when anyone joins the server
  1231. bot.member_join do |event|
  1232. unless User.find_by(id: event.author.id.to_s)
  1233. usr = User.create(id: event.author.id.to_s)
  1234. usr.make_stats
  1235. end
  1236. end
  1237. # This will trigger when anyone leaves the server
  1238. bot.member_leave do |event|
  1239. end
  1240. # This will trigger when anyone is banned from the server
  1241. bot.user_ban do |event|
  1242. end
  1243. # This will trigger when anyone is un-banned from the server
  1244. bot.user_unban do |event|
  1245. end
  1246. bot.run