Skip to content

Commit

Permalink
Merge branch 'master' into bso
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Feb 28, 2024
2 parents 6e9c312 + 6078da7 commit 9006633
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
50 changes: 50 additions & 0 deletions src/lib/premiumPatronTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { Time } from 'e';

import { handleMahojiConfirmation } from './util/handleMahojiConfirmation';
import { formatDuration } from './util/smallUtils';

export async function premiumPatronTime(
timeMs: number,
tier: number,
userToGive: MUser,
interaction: ChatInputCommandInteraction
) {
if (![1, 2, 3, 4, 5, 6].includes(tier)) return 'Invalid input.';
if (timeMs < Time.Second || timeMs > Time.Year * 3) return 'Invalid input.';

const currentBalanceTier = userToGive.user.premium_balance_tier;

if (currentBalanceTier !== null && currentBalanceTier !== tier) {
await handleMahojiConfirmation(
interaction,
`They already have Tier ${currentBalanceTier}; this will replace the existing balance entirely, are you sure?`
);
}
await handleMahojiConfirmation(
interaction,
`Are you sure you want to add ${formatDuration(timeMs)} of Tier ${tier} patron to ${userToGive}?`
);
await userToGive.update({
premium_balance_tier: tier
});

const currentBalanceTime =
userToGive.user.premium_balance_expiry_date === null
? null
: Number(userToGive.user.premium_balance_expiry_date);

let newBalanceExpiryTime = 0;
if (currentBalanceTime !== null && tier === currentBalanceTier) {
newBalanceExpiryTime = currentBalanceTime + timeMs;
} else {
newBalanceExpiryTime = Date.now() + timeMs;
}
await userToGive.update({
premium_balance_expiry_date: newBalanceExpiryTime
});

return `Gave ${formatDuration(timeMs)} of Tier ${tier} patron to ${userToGive}. They have ${formatDuration(
newBalanceExpiryTime - Date.now()
)} remaining.`;
}
10 changes: 9 additions & 1 deletion src/lib/util/userQueues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ export function getUserUpdateQueue(userID: string) {

export async function userQueueFn<T>(userID: string, fn: () => Promise<T>) {
const queue = getUserUpdateQueue(userID);
return queue.add(() => fn());
return queue.add(async () => {
const error = new Error();
try {
return await fn();
} catch (e) {
error.message = (e as Error).message;
throw error;
}
});
}
47 changes: 5 additions & 42 deletions src/mahoji/commands/rp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import { unequipPet } from '../../lib/minions/functions/unequipPet';
import { mahojiUserSettingsUpdate } from '../../lib/MUser';
import { patreonTask } from '../../lib/patreon';
import { allPerkBitfields } from '../../lib/perkTiers';
import { premiumPatronTime } from '../../lib/premiumPatronTime';
import { prisma } from '../../lib/settings/prisma';
import { TeamLoot } from '../../lib/simulation/TeamLoot';
import { ItemBank } from '../../lib/types';
import { dateFm, formatDuration, returnStringOrFile } from '../../lib/util';
import { dateFm, returnStringOrFile } from '../../lib/util';
import getOSItem from '../../lib/util/getOSItem';
import { handleMahojiConfirmation } from '../../lib/util/handleMahojiConfirmation';
import { deferInteraction } from '../../lib/util/interactionReply';
Expand Down Expand Up @@ -479,50 +480,12 @@ ORDER BY item_id ASC;`);

if (options.player?.add_patron_time) {
const { tier, time, user: userToGive } = options.player.add_patron_time;
if (![1, 2, 3, 4, 5, 6].includes(tier)) return 'Invalid input.';
const duration = new Duration(time);
if (![1, 2, 3, 4, 5, 6].includes(tier)) return 'Invalid input.';
const ms = duration.offset;
if (ms < Time.Second || ms > Time.Year * 3) return 'Invalid input.';
const input = await mahojiUsersSettingsFetch(userToGive.user.id, {
premium_balance_tier: true,
premium_balance_expiry_date: true,
id: true
});

const currentBalanceTier = input.premium_balance_tier;

if (currentBalanceTier !== null && currentBalanceTier !== tier) {
await handleMahojiConfirmation(
interaction,
`They already have Tier ${currentBalanceTier}; this will replace the existing balance entirely, are you sure?`
);
}
await handleMahojiConfirmation(
interaction,
`Are you sure you want to add ${formatDuration(ms)} of Tier ${tier} patron to ${
userToGive.user.username
}?`
);
await mahojiUserSettingsUpdate(input.id, {
premium_balance_tier: tier
});

const currentBalanceTime =
input.premium_balance_expiry_date === null ? null : Number(input.premium_balance_expiry_date);

let newBalanceExpiryTime = 0;
if (currentBalanceTime !== null && tier === currentBalanceTier) {
newBalanceExpiryTime = currentBalanceTime + ms;
} else {
newBalanceExpiryTime = Date.now() + ms;
}
await mahojiUserSettingsUpdate(input.id, {
premium_balance_expiry_date: newBalanceExpiryTime
});

return `Gave ${formatDuration(ms)} of Tier ${tier} patron to ${
userToGive.user.username
}. They have ${formatDuration(newBalanceExpiryTime - Date.now())} remaining.`;
const res = await premiumPatronTime(ms, tier, await mUserFetch(userToGive.user.id), interaction);
return res;
}

// Unequip Items
Expand Down

0 comments on commit 9006633

Please sign in to comment.