sl-banbot/helpers.js

74 lines
1.8 KiB
JavaScript

module.exports = {}
const change = 62167219200; // Subtract a year, whoops
module.exports.convertSLTimestamp = (timestamp) => {
// Convert SCP SL timestamp to seconds since SCP epoch
const ourTs = Number(BigInt(timestamp) / 10000000n);
// Convert to Unix timestamp (seconds since Jan 1, 1970 UTC)
const unixTs = ourTs - change;
// Adjust for local timezone (Mountain Time in this case)
const date = new Date(unixTs * 1000); // Convert to milliseconds for JavaScript Date constructor
return date;
}
module.exports.toSLTimestamp = (timestamp) => {
return String(((Number(timestamp) / 1000) + change) * 10000000)
}
module.exports.slToJSON = (slDataLine) => {
raw = slDataLine.split(";");
data = {
banned_username: raw[0],
banned_id: raw[1],
expires: module.exports.convertSLTimestamp(raw[2]),
reason: raw[3],
moderator: raw[4],
banned_timestamp: module.exports.convertSLTimestamp(raw[5])
}
return data;
}
module.exports.makeBansTable = (newContents) => {
tmp = {}
lines = newContents.split("\n");
lines.forEach(line => {
if (line === "") return;
data = module.exports.slToJSON(line)
tmp[data.banned_id] = data
})
return tmp
}
module.exports.diff = (oldData, newData) => {
let added = {};
let removed = {};
// Find added keys (in newData but not in oldData)
Object.keys(newData).forEach(key => {
if (!oldData.hasOwnProperty(key)) {
added[key] = newData[key];
}
});
// Find removed keys (in oldData but not in newData)
Object.keys(oldData).forEach(key => {
if (!newData.hasOwnProperty(key)) {
removed[key] = oldData[key];
}
});
// Construct the result object with added and removed changes
let result = {
added: added,
removed: removed
};
return result;
}
return module.exports