diff --git a/src/lib/premiumPatronTime.ts b/src/lib/premiumPatronTime.ts new file mode 100644 index 0000000000..26f468db00 --- /dev/null +++ b/src/lib/premiumPatronTime.ts @@ -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.`; +} diff --git a/src/mahoji/commands/rp.ts b/src/mahoji/commands/rp.ts index 865218b7b8..155b9ed284 100644 --- a/src/mahoji/commands/rp.ts +++ b/src/mahoji/commands/rp.ts @@ -19,10 +19,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'; @@ -426,50 +427,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