Source

lib/party/onPartyJoinedGame.js

const events = require('events')

const onNotification = require('../client/onNotification.js').func

exports.optional = ['jar']

// Docs
/**
 * 🔐 An event for when a party joins a game.
 * @category Party
 * @alias onPartyJoinedGame
 * @returns An EventEmitter that emits when a party joins a game.
 * @example const noblox = require("noblox.js")
 * // Login using your cookie
 * const partyJoinedGame = noblox.onPartyJoinedGame()
 * partyJoinedGame.on("data", function(data) {
 *  console.log("Party joined game! ", data)
 * })
 * partyJoinedGame.on("error", function(err) {
 *  console.error("Something went wrong: ", err)
 *  // Handle error as needed
 * })
**/

exports.func = (args) => {
  const jar = args.jar
  const newEvent = new events.EventEmitter()
  const notifications = onNotification({ jar })

  notifications.on('data', (name, message) => {
    if (name === 'PartyNotifications' && message.Type === 'PartyJoinedGame') {
      newEvent.emit('data', {
        PartyId: message.PartyId,
        PartyType: message.PartyType
      })
    }
  })

  notifications.on('error', (err) => {
    newEvent.emit('error', err)
  })

  notifications.on('connect', () => {
    newEvent.emit('connect')
  })

  notifications.on('close', (internal) => {
    if (internal) return
    notifications.emit('close', true)
  })

  return newEvent
}