Source

lib/trades/counterTrade.js

  1. // Includes
  2. const http = require('../util/http.js').func
  3. const getGeneralToken = require('../util/getGeneralToken.js').func
  4. const getCurrentUser = require('../util/getCurrentUser.js').func
  5. // Args
  6. exports.required = ['tradeId', 'targetUserId', 'sendingOffer', 'receivingOffer']
  7. exports.optional = ['jar']
  8. // Docs
  9. /**
  10. * 🔐 Counter an active incoming trade.
  11. * @category Trade
  12. * @alias counterTrade
  13. * @param {number} tradeId - The id of the active trade
  14. * @param {number} targetUserId - The user to send the trade to.
  15. * @param {TradeOffer} sendingOffer - The offer to send to the other user.
  16. * @param {TradeOffer} recievingOffer - The offer you are requesting from the other user.
  17. * @returns {Promise<SendTradeResponse>}
  18. * @example const noblox = require("noblox.js")
  19. * // Login using your cookie
  20. * noblox.counterTrade(1234, 80231025, { userAssetIds: [23289506393] }, { userAssetIds: [32924150919] })
  21. **/
  22. // Define
  23. function counterTrade (tradeId, targetUserId, sendingOffer, receivingOffer, jar, xcsrf, loggedInUser) {
  24. return new Promise((resolve, reject) => {
  25. if (!sendingOffer.userAssetIds || !receivingOffer.userAssetIds) {
  26. reject(new Error('Both offers must includes userAssetIds.'))
  27. }
  28. if (!sendingOffer.robux) sendingOffer.robux = 0
  29. if (!receivingOffer.robux) receivingOffer.robux = 0
  30. http({
  31. url: '//trades.roblox.com/v1/trades/' + tradeId + '/counter',
  32. options: {
  33. method: 'POST',
  34. resolveWithFullResponse: true,
  35. jar,
  36. headers: {
  37. 'X-CSRF-TOKEN': xcsrf,
  38. 'Content-Type': 'application/json'
  39. },
  40. body: JSON.stringify({
  41. offers: [
  42. {
  43. userId: targetUserId,
  44. ...receivingOffer
  45. },
  46. {
  47. userId: loggedInUser,
  48. ...sendingOffer
  49. }
  50. ]
  51. })
  52. }
  53. }).then((res) => {
  54. if (res.statusCode === 200) {
  55. resolve(JSON.parse(res.body))
  56. } else {
  57. const body = JSON.parse(res.body) || {}
  58. if (body.errors && body.errors.length > 0) {
  59. const errors = body.errors.map((e) => {
  60. return e.message
  61. })
  62. reject(new Error(`${res.statusCode} ${errors.join(', ')}`))
  63. }
  64. }
  65. }).catch(error => reject(error))
  66. })
  67. }
  68. exports.func = function (args) {
  69. const jar = args.jar
  70. return Promise.all([
  71. getGeneralToken({ jar }),
  72. getCurrentUser({ jar, option: 'UserID' })
  73. ])
  74. .then(function (resolvedPromises) {
  75. return counterTrade(args.tradeId, args.targetUserId, args.sendingOffer, args.receivingOffer, jar, resolvedPromises[0], resolvedPromises[1])
  76. })
  77. }