bot.rb 44 KB

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