bot.rb 35 KB

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