Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a fun lottery system to auto spend leftover tax yen. #91

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 56 additions & 11 deletions yen.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ async function UpdateTaxChannel() {
await channel.send(threeTicks + message + threeTicks);
}

// Update the tax channel once per hour.
// Update the tax channel once after the bot boots.
setTimeout(async () => {
await UpdateTaxChannel();
}, 60 * 1000);
// Lottery once per hour, which also triggers an update of the tax and yen channels.
setInterval(async () => {
await UpdateTaxChannel();
await DoLottery();
}, 3600 * 1000);

async function CalculateTaxPlan(yenToRaise) {
Expand Down Expand Up @@ -220,9 +221,9 @@ async function CalculateTaxPlan(yenToRaise) {
return plan;
}

async function ImplementTaxPlan(plan, recipient, discordMessage) {
async function ImplementTaxPlan(plan, recipient) {
let totalTax = 0;
let message = 'Mr. President thanks the taxpayers for their generosity.\n\n';
let longMessage = 'Tax Record\n\n';
const sortable = [];
for (const cid in plan) {
const tax = plan[cid];
Expand All @@ -234,14 +235,15 @@ async function ImplementTaxPlan(plan, recipient, discordMessage) {
}
sortable.sort((a, b) => b.tax - a.tax);
for (const line of sortable) {
message += line.text;
longMessage += line.text;
}
message += ' -----\n';
longMessage += ' -----\n';
const recipientName = recipient.getNicknameOrTitleWithInsignia();
message += `+ ¥ ${totalTax} ${recipientName}\n\n`;
message += 'See #tax for more info about tax. Active members are never taxed. You can easily dodge tax by connecting to VC every 3 months. The goal of tax is to give the Government a steady source of revenue by putting inactive yen back into circulation.';
await discordMessage.channel.send(threeTicks + message + threeTicks);
await YenLog(message);
longMessage += `+ ¥ ${totalTax} ${recipientName}\n\n`;
longMessage += 'See #tax for more info about tax. Active members are never taxed. You can easily dodge tax by connecting to VC every 3 months. The goal of tax is to give the Government a steady source of revenue by putting inactive yen back into circulation.';
await YenLog(longMessage);
const shortMessage = `${recipientName} won ${totalTax} yen in the lottery`;
await DiscordUtil.MessagePublicChatChannel(threeTicks + shortMessage + threeTicks);
const r = Math.log(2) / 90;
for (const cid in plan) {
const tax = plan[cid];
Expand Down Expand Up @@ -272,7 +274,7 @@ async function HandleTaxCommand(discordMessage) {
const author = await UserCache.GetCachedUserByDiscordId(discordMessage.author.id);
const isFounder = author.commissar_id === 7;
const isPrez = author.office === 'PREZ';
const isCfo = author.commissar_id === 2799; // EviL ID is 2799. TODO: update this to look for the CFO badge.
const isCfo = author.commissar_id === 2799;
const ok = isFounder || isPrez || isCfo;
console.log('TAX COMMAND', isFounder, isPrez, isCfo);
if (!author || !ok) {
Expand Down Expand Up @@ -314,6 +316,48 @@ async function HandleTaxCommand(discordMessage) {
await UpdateTaxChannel();
}

async function DoLottery() {
console.log('LOTTERY');
const taxBase = await CalculateInactivityTaxBase();
let totalTaxBase = 0;
for (const i in taxBase) {
totalTaxBase += taxBase[i];
}
const maxPrizeYen = 10;
const targetPrize = Math.floor(0.1 * totalTaxBase);
const prizeYen = Math.min(targetPrize, maxPrizeYen);
const plan = await CalculateTaxPlan(prizeYen);
if (!plan) {
console.log('Could not raise enough tax revenue for lottery.');
return;
}
const membersInVoiceChat = [];
const guild = await DiscordUtil.GetMainDiscordGuild();
for (const [channelId, channel] of guild.channels.cache) {
const afkLoungeId = '703716669452714054';
if (channel.type === 2 && channel.id !== afkLoungeId) {
for (const [memberId, member] of channel.members) {
membersInVoiceChat.push(member.id);
}
}
}
const n = membersInVoiceChat.length;
if (n < 2) {
console.log('Not enough people in voice chat for lottery.');
return;
}
const randomIndex = Math.floor(Math.random() * n);
const winnerId = membersInVoiceChat[randomIndex];
const recipient = await UserCache.GetCachedUserByDiscordId(winnerId);
if (!recipient) {
console.log('Error. Invalid lottery winner.');
return;
}
await ImplementTaxPlan(plan, recipient);
await UpdateYenChannel();
await UpdateTaxChannel();
}

async function UpdateYenChannel() {
const users = [];
await UserCache.ForEach((user) => {
Expand Down Expand Up @@ -641,6 +685,7 @@ async function HandleConvertCommand(discordMessage) {
}

module.exports = {
DoLottery,
HandleConvertCommand,
HandlePayCommand,
HandleTaxCommand,
Expand Down
Loading