Skip to content

Commit 65c3269

Browse files
authored
Update onNotification.js
1 parent 95955e8 commit 65c3269

File tree

1 file changed

+86
-37
lines changed

1 file changed

+86
-37
lines changed

lib/client/onNotification.js

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,93 @@
1-
const events = require('events')
2-
const onNotification = require('../client/onNotification.js').func
3-
exports.optional = ['jar']
1+
// Includes
2+
const http = require('../util/http.js').func
3+
4+
exports.required = ['group']
5+
exports.optional = ['actionType', 'userId', 'sortOrder', 'limit', 'cursor', 'jar']
6+
47
// Docs
58
/**
6-
* 🔐 An event for when a party is deleted.
7-
* @category Party
8-
* @alias onPartyDeleted
9-
* @returns An EventEmitter that emits when a party is deleted.
9+
* 🔐 Get the audit log for the group.
10+
* @category Group
11+
* @alias getAuditLog
12+
* @param {number} group - The id of the group.
13+
* @param {("DeletePost" | "RemoveMember" | "AcceptJoinRequest" | "DeclineJoinRequest" | "PostStatus" | "ChangeRank" | "BuyAd" | "SendAllyRequest" | "CreateEnemy" | "AcceptAllyRequest" | "DeclineAllyRequest" | "DeleteAlly" | "DeleteEnemy" | "AddGroupPlace" | "RemoveGroupPlace" | "CreateItems" | "ConfigureItems" | "SpendGroupFunds" | "ChangeOwner" | "Delete" | "AdjustCurrencyAmounts" | "Abandon" | "Claim" | "Rename" | "ChangeDescription" | "InviteToClan" | "KickFromClan" | "CancelClanInvite" | "BuyClan" | "CreateGroupAsset" | "UpdateGroupAsset" | "ConfigureGroupAsset" | "RevertGroupAsset" | "CreateGroupDeveloperProduct" | "ConfigureGroupGame" | "Lock" | "Unlock" | "CreateGamePass" | "CreateBadge" | "ConfigureBadge" | "SavePlace" | "PublishPlace" | "joinGroup" | "leaveGroup")=} actionType - The action type to filter for.
14+
* @param {number=} userId - The user's id to filter for.
15+
* @param {SortOrder=} sortOrder - The order to sort the logs by.
16+
* @param {Limit=} limit - The maximum logs per a page.
17+
* @param {string=} cursor - The cursor for the page.
18+
* @returns {Promise<AuditPage>}
1019
* @example const noblox = require("noblox.js")
1120
* // Login using your cookie
12-
* const partyDeleted = noblox.onPartyDeleted()
13-
* partyDeleted.on("data", function(data) {
14-
* console.log("Party deleted! ", data)
15-
* })
16-
* partyDeleted.on("error", function(err) {
17-
* console.error("Something went wrong: ", err)
18-
* // Handle error as needed
19-
* })
21+
* const rankLogs = await noblox.getAuditLog(1, "ChangeRank", 2, "Asc")
2022
**/
21-
exports.func = (args) => {
22-
const jar = args.jar
23-
const newEvent = new events.EventEmitter()
24-
const notifications = onNotification({ jar })
25-
notifications.on('data', (name, message) => {
26-
if (name === 'PartyNotifications' && message.Type === 'PartyDeleted') {
27-
newEvent.emit('data', {
28-
PartyId: message.PartyId,
29-
PartyType: message.PartyType
30-
})
23+
24+
function getAuditLog(group, actionType, userId, sortOrder, limit, cursor, jar) {
25+
return new Promise(async (resolve, reject) => {
26+
try {
27+
const fetchLogs = async (type) => {
28+
const httpOpt = {
29+
url: `https://groups.roblox.com/v1/groups/${group}/audit-log?actionType=${type}&cursor=${cursor}&limit=${limit}&sortOrder=${sortOrder}&userId=${userId}`,
30+
options: {
31+
method: 'GET',
32+
resolveWithFullResponse: true,
33+
jar
34+
}
35+
};
36+
37+
const res = await http(httpOpt);
38+
const json = JSON.parse(res.body);
39+
40+
if (res.statusCode !== 200) {
41+
let error = "Unknown error.";
42+
if (json.errors) {
43+
error = json.errors.map((e) => e.message).join("\n");
44+
}
45+
throw new Error(error);
46+
}
47+
48+
json.data = json.data.map(entry => {
49+
entry.created = new Date(entry.created);
50+
entry.created.setMilliseconds(0);
51+
return entry;
52+
});
53+
54+
return json;
55+
};
56+
57+
if (actionType && actionType !== "all") {
58+
const result = await fetchLogs(actionType);
59+
return resolve(result);
60+
}
61+
62+
const allLogs = await fetchLogs("");
63+
64+
const [joinLogs, leaveLogs] = await Promise.all([
65+
fetchLogs("joinGroup"),
66+
fetchLogs("leaveGroup")
67+
]);
68+
69+
const combined = [
70+
...allLogs.data,
71+
...joinLogs.data,
72+
...leaveLogs.data
73+
].sort((a, b) => new Date(b.created) - new Date(a.created));
74+
75+
allLogs.data = combined;
76+
return resolve(allLogs);
77+
78+
} catch (err) {
79+
reject(err);
3180
}
32-
})
33-
notifications.on('error', (err) => {
34-
newEvent.emit('error', err)
35-
})
36-
notifications.on('connect', () => {
37-
newEvent.emit('connect')
38-
})
39-
notifications.on('close', (internal) => {
40-
if (internal) return
41-
notifications.emit('close', true)
42-
})
43-
return newEvent
81+
});
82+
}
83+
84+
// Define
85+
exports.func = function (args) {
86+
const jar = args.jar
87+
const actionType = args.actionType || ''
88+
const userId = args.userId || ''
89+
const sortOrder = args.sortOrder || 'Asc'
90+
const limit = args.limit || (100).toString()
91+
const cursor = args.cursor || ''
92+
return getAuditLog(args.group, actionType, userId, sortOrder, limit, cursor, jar)
4493
}

0 commit comments

Comments
 (0)