138 lines
5.4 KiB
JavaScript
138 lines
5.4 KiB
JavaScript
require("dotenv").config();
|
|
const noblox = require('noblox.js')
|
|
const Discord = require('discord.js')
|
|
const colors = require('colors')
|
|
|
|
const discord = new Discord.Client(
|
|
{
|
|
intents: [
|
|
"Guilds",
|
|
"GuildWebhooks",
|
|
"GuildMessages"
|
|
]
|
|
}
|
|
)
|
|
const currency = process.env.CURRENCY
|
|
const pending = process.env.PENDING
|
|
function makeSummaryString(summaryData) {
|
|
const grandTotal = summaryData.recurringRobuxStipend + summaryData.itemSaleRobux + summaryData.purchasedRobux + summaryData.tradeSystemRobux + summaryData.pendingRobux + summaryData.groupPayoutRobux + summaryData.individualToGroupRobux + summaryData.premiumPayouts + summaryData.groupPremiumPayouts + summaryData.adjustmentRobux + summaryData.immersiveAdPayouts + summaryData.subscriptionPayouts + summaryData.subscriptionClawbacks + summaryData.commissionRobux + summaryData.publishingAdvanceRebates
|
|
const total = grandTotal - summaryData.pendingRobux
|
|
const summaryString = `Sales: **${currency}${summaryData.itemSaleRobux.toLocaleString()}**
|
|
Pending Robux: **${currency}${pending}${summaryData.pendingRobux.toLocaleString()}**
|
|
Group Revenue Payouts: **${currency}${summaryData.groupPayoutRobux.toLocaleString()}**
|
|
Group Premium Payouts: **${currency}${summaryData.groupPremiumPayouts.toLocaleString()}**
|
|
|
|
__Total: **${currency}${grandTotal.toLocaleString()}**__
|
|
__**Grand Total: ${currency}${total.toLocaleString()}**__`;
|
|
return summaryString
|
|
}
|
|
|
|
const updateWebhook = async function () {
|
|
const fundsSummaryEmbeds = {
|
|
title: `Funds Summary for ${groupData.name}`,
|
|
url: `https://www.roblox.com/groups/${process.env.GROUPID}`,
|
|
color: 0x00ff00,
|
|
thumbnail: {
|
|
url: groupLogoUrl
|
|
},
|
|
author: {
|
|
name: `Group Owner, ${groupData.owner.displayName}`,
|
|
icon_url: groupData.owner.avatar
|
|
},
|
|
timestamp: new Date(),
|
|
footer: {
|
|
text: "Last updated"
|
|
},
|
|
fields: [
|
|
{
|
|
name: "Today",
|
|
value: makeSummaryString(await noblox.getGroupRevenueSummary(process.env.GROUPID, "Day")),
|
|
},
|
|
{
|
|
name: "This Week",
|
|
value: makeSummaryString(await noblox.getGroupRevenueSummary(process.env.GROUPID, "Week")),
|
|
},
|
|
{
|
|
name: "This Month",
|
|
value: makeSummaryString(await noblox.getGroupRevenueSummary(process.env.GROUPID, "Month")),
|
|
},
|
|
{
|
|
name: "This Year",
|
|
value: makeSummaryString(await noblox.getGroupRevenueSummary(process.env.GROUPID, "Year")),
|
|
},
|
|
{
|
|
name: "All Time",
|
|
value: makeSummaryString(await noblox.getGroupRevenueSummary(process.env.GROUPID))
|
|
}
|
|
]
|
|
}
|
|
|
|
webhook.editMessage(message.id, {
|
|
embeds: [fundsSummaryEmbeds]
|
|
}).then(() => {
|
|
console.log(`${colors.green("[DISCORD]")} Updated webhook message with new funds summary`);
|
|
}).catch((err) => {
|
|
console.log(`${colors.red("[DISCORD]")} Error: ${err.stack}`);
|
|
})
|
|
|
|
};
|
|
var groupLogoUrl;
|
|
var groupData;
|
|
var webhook;
|
|
var message;
|
|
var channel;
|
|
discord.on('ready', async () => {
|
|
console.log(`${colors.cyan("[DISCORD]")} Logged in as ${discord.user.displayName}`);
|
|
channel = await discord.channels.cache.get(process.env.SUMMARY_CHANNEL_ID);
|
|
if (!channel) {
|
|
console.log(`${colors.red("[DISCORD]")} Channel not found, Make sure the channel ID is correct in the .env file`);
|
|
process.exit(1);
|
|
}
|
|
noblox.setCookie(process.env.ROBLOSECURITY).then(async (current) => {
|
|
console.log(`${colors.cyan("[ROBLOX]")} Logged in as ${current.displayName}`);
|
|
console.log(`${colors.cyan("[ROBLOX]")} Fetching group data...`);
|
|
groupData = await noblox.getGroup(process.env.GROUPID);
|
|
await fetch(`https://thumbnails.roblox.com/v1/users/avatar-bust?userIds=${groupData.owner.userId}&size=420x420&format=Png&isCircular=true`).then(async res => {
|
|
groupData.owner.avatar = (await res.json()).data[0].imageUrl
|
|
})
|
|
groupLogoUrl = await noblox.getLogo(process.env.GROUPID);
|
|
console.log(`${colors.cyan("[ROBLOX]")} Fetched group data for ${groupData.name}`);
|
|
const transactionEvent = noblox.onGroupTransaction(process.env.GROUPID, "Sale")
|
|
transactionEvent.on("data", async function (data) {
|
|
console.log(`${colors.green("[ROBLOX]")} New Transaction: ${data.idHash}`);
|
|
updateWebhook();
|
|
})
|
|
transactionEvent.on("error", function (err) {
|
|
console.log(`${colors.red("[ROBLOX]")} Error: ${err.stack}`);
|
|
});
|
|
// Try to find an existing webhook made by discord.user.id, if not exists make one named ${groupData.name} Funds Summary with the group logo as the icon
|
|
const webhooks = await channel.fetchWebhooks();
|
|
webhook = webhooks.find(w => w.owner.id === discord.user.id && w.channelId == process.env.SUMMARY_CHANNEL_ID)
|
|
if (!webhook) {
|
|
console.log(`${colors.yellow("[DISCORD]")} No existing webhook found, creating a new one...`);
|
|
webhook = await channel.createWebhook({
|
|
name: `${groupData.name} Funds Summary`,
|
|
avatar: groupLogoUrl,
|
|
reason: "Funds Summary Webhook"
|
|
});
|
|
} else {
|
|
console.log(`${colors.green("[DISCORD]")} Found existing webhook`);
|
|
}
|
|
// Try to find the latest message from our webhook, if it doesnt exist send a new one
|
|
message = await channel.messages.fetch().then(messages => messages.find(m => m.author.id === webhook.id));
|
|
if (!message) {
|
|
console.log(`${colors.yellow("[DISCORD]")} No existing message found, sending a new one...`);
|
|
message = await webhook.send({
|
|
embeds: [{
|
|
title: "Please wait..."
|
|
}]
|
|
})
|
|
} else {
|
|
console.log(`${colors.green("[DISCORD]")} Found existing message`);
|
|
}
|
|
updateWebhook();
|
|
setInterval(updateWebhook, 1 * 60 * 60 * 1000); // Update every hour
|
|
});
|
|
})
|
|
|
|
discord.login(process.env.DISCORD_TOKEN) |