Skip to content

Commit

Permalink
Added postToTopic() for MessagingService needs (#793)
Browse files Browse the repository at this point in the history
* Added postToTopic()

* Update publishToTopic.js

* Updated errors and doc example

* Switched error handling to if else

Avoid fall through and use of break in each case

* Updated error message

* Updated error handling

* Add publishToTopic into typings
  • Loading branch information
commonly-ts authored May 25, 2024
1 parent c2dd3d3 commit e1c01e1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
57 changes: 57 additions & 0 deletions lib/games/publishToTopic.js
Original file line number Diff line number Diff line change
@@ -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<boolean>}
* @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) => {

Check failure on line 22 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Block must not be padded by blank lines

const httpOpt = {
url: `//apis.roblox.com/messaging-service/v1/universes/${universeId}/topics/${topic}`,
options: {

Check failure on line 26 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 6 spaces but found 8
json: true,

Check failure on line 27 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 10
resolveWithFullResponse: true,

Check failure on line 28 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 10
jar,

Check failure on line 29 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 10
method: 'POST',

Check failure on line 30 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 10
body: { message: JSON.stringify(data) },

Check failure on line 31 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 10
headers: {

Check failure on line 32 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 10
'Content-Type': 'application/json'

Check failure on line 33 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 10 spaces but found 12
}

Check failure on line 34 in lib/games/publishToTopic.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 8 spaces but found 10
}
}

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)
}
19 changes: 12 additions & 7 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ declare module "noblox.js" {
isFavoritedByUser: boolean;
favoritedCount: number;
}

interface PlaceInformation {
placeId: number;
name: string;
Expand Down Expand Up @@ -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<void>

/**
/**
* ☁️ 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<DatastoreEntry>
Expand Down Expand Up @@ -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<DeveloperProductAddResult>;
Expand Down Expand Up @@ -1871,11 +1871,16 @@ declare module "noblox.js" {
*/
function getUniverseInfo(universeIds: number[] | number, jar?: CookieJar): Promise<UniverseInformation[]>;

/**
/**
* ☁️ Publish a message to a subscribed topic.
*/
function publishToTopic(universeId: number, topic: string, data: (Object | string), jar?: CookieJar): Promise<boolean>;

/**
* 🔐 Returns information about the place(s) in question, such as name, description, etc.
*/
function getPlaceInfo(placeIds: number[] | number, jar?: CookieJar): Promise<PlaceInformation[]>;

/**
* 🔐 Update a developer product.
*/
Expand Down

0 comments on commit e1c01e1

Please sign in to comment.