-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters