bot.rb 41 KB

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