diff --git a/lib/games/publishToTopic.js b/lib/games/publishToTopic.js new file mode 100644 index 000000000..a1a964a7d --- /dev/null +++ b/lib/games/publishToTopic.js @@ -0,0 +1,57 @@ +const http = require('../util/http.js').func + +exports.required = ['universeId', 'topic', 'data'] +exports.optional = ['jar'] + +// Docs +/** + * ☁️ Publish a message to a subscribed topic. + * @category Game + * @alias postToTopic + * @param {number} universeId - The id of the universe. + * @param {string} topic - The name of the topic. + * @param {Object | string} data - The data to post. + * @returns {Promise} + * @example const noblox = require("noblox.js") + * const data = { targetUser: 123456789, staffMember: 1210019099, action: "Kick" } + * + * await noblox.publishToTopic(2152417643, "ModerateUser", data) +**/ + +function publishToTopic (universeId, topic, data, jar) { + return new Promise((resolve, reject) => { + + const httpOpt = { + url: `//apis.roblox.com/messaging-service/v1/universes/${universeId}/topics/${topic}`, + options: { + json: true, + resolveWithFullResponse: true, + jar, + method: 'POST', + body: { message: JSON.stringify(data) }, + headers: { + 'Content-Type': 'application/json' + } + } + } + + return http(httpOpt) + .then(function (res) { + if (res.statusCode === 200) { + resolve(true) + } else { + if (typeof(res.body) === "string") { + reject(new Error(`[${res.statusCode}] ${res.statusMessage} ${res.body}`)) + } else { + const data = Object.assign(res.body) + reject(new Error(`[${res.statusCode}] ${data.Error} ${data.Message}`)) + } + } + }) + .catch(reject) + }) +} + +exports.func = function (args) { + return publishToTopic(args.universeId, args.topic, args.data, args.jar) +} diff --git a/typings/index.d.ts b/typings/index.d.ts index cb8fc1392..ac06f21df 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -736,7 +736,7 @@ declare module "noblox.js" { isFavoritedByUser: boolean; favoritedCount: number; } - + interface PlaceInformation { placeId: number; name: string; @@ -1670,12 +1670,12 @@ declare module "noblox.js" { /// DataStores - /** - * ☁️ Marks the entry as deleted by creating a tombstone version. Entries are deleted permanently after 30 days. + /** + * ☁️ Marks the entry as deleted by creating a tombstone version. Entries are deleted permanently after 30 days. */ function deleteDatastoreEntry(universeId: number, datastoreName: string, entryKey: string, scope?: string, jar?: CookieJar): Promise - /** + /** * ☁️ Returns the latest value and metadata associated with an entry, or a specific version if versionId is provided. */ function getDatastoreEntry(universeId: number, datastoreName: string, entryKey: string, scope?: string, versionId?: string, jar?: CookieJar): Promise @@ -1821,7 +1821,7 @@ declare module "noblox.js" { /// Games /** - * 🔐 Adds a developer product to the specified universe. + * 🔐 Adds a developer product to the specified universe. * Warning: The `productId` returned by this function does not match the `productId` used by other endpoints. */ function addDeveloperProduct(universeId: number, name: string, priceInRobux: number, description?: string, jar?: CookieJar): Promise; @@ -1871,11 +1871,16 @@ declare module "noblox.js" { */ function getUniverseInfo(universeIds: number[] | number, jar?: CookieJar): Promise; - /** + /** + * ☁️ Publish a message to a subscribed topic. + */ + function publishToTopic(universeId: number, topic: string, data: (Object | string), jar?: CookieJar): Promise; + + /** * 🔐 Returns information about the place(s) in question, such as name, description, etc. */ function getPlaceInfo(placeIds: number[] | number, jar?: CookieJar): Promise; - + /** * 🔐 Update a developer product. */