made collect logic actually connect to the utils
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+172
-171
@@ -23,24 +23,159 @@ const {
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
const COLLECT_COOLDOWN =
|
||||
60 * 60;
|
||||
|
||||
const COLLECT_COOLDOWN = 60 * 60;
|
||||
const COLLECT_MIN = 1;
|
||||
const COLLECT_MAX = 1000;
|
||||
|
||||
/*
|
||||
* IMPORTANT:
|
||||
*
|
||||
* Always use exactly the same cooldown key.
|
||||
*/
|
||||
|
||||
const COLLECT_COOLDOWN_KEY =
|
||||
"collect";
|
||||
const COLLECT_COOLDOWN_KEY = "collect";
|
||||
|
||||
|
||||
/*
|
||||
* ============================================================
|
||||
* SHOW COLLECT PAGE
|
||||
* SHARED COLLECT LOGIC
|
||||
* ============================================================
|
||||
*
|
||||
* This is the one authoritative implementation used by:
|
||||
*
|
||||
* - /cookies collect
|
||||
* - Crumb Saucer Cookie Bakery
|
||||
*
|
||||
* It does not reply/update Discord itself.
|
||||
*/
|
||||
|
||||
async function collectCookies(
|
||||
interaction,
|
||||
userId = interaction.user.id
|
||||
) {
|
||||
const cooldown = await claimCooldown(
|
||||
userId,
|
||||
interaction.guildId,
|
||||
COLLECT_COOLDOWN_KEY,
|
||||
COLLECT_COOLDOWN
|
||||
);
|
||||
|
||||
|
||||
if (!cooldown.claimed) {
|
||||
const balance = await getCookieBalance(
|
||||
userId
|
||||
);
|
||||
|
||||
return {
|
||||
collected: false,
|
||||
cooldown,
|
||||
balance,
|
||||
amount: 0
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
const amount =
|
||||
Math.floor(
|
||||
Math.random() *
|
||||
(
|
||||
COLLECT_MAX -
|
||||
COLLECT_MIN +
|
||||
1
|
||||
)
|
||||
) +
|
||||
COLLECT_MIN;
|
||||
|
||||
|
||||
await changeCookies(
|
||||
userId,
|
||||
amount
|
||||
);
|
||||
|
||||
|
||||
await trackProfileEvent(
|
||||
interaction,
|
||||
userId,
|
||||
{
|
||||
type: "cookie_gain",
|
||||
amount
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
const balance = await getCookieBalance(
|
||||
userId
|
||||
);
|
||||
|
||||
|
||||
return {
|
||||
collected: true,
|
||||
cooldown,
|
||||
balance,
|
||||
amount
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ============================================================
|
||||
* SHARED EMBED BUILDERS
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
function buildCollectSuccessEmbed(
|
||||
userId,
|
||||
result,
|
||||
footerType = "crumbSaucerCollect"
|
||||
) {
|
||||
return new EmbedBuilder()
|
||||
.setTitle(
|
||||
"🍪 Fresh Batch!"
|
||||
)
|
||||
.setDescription(
|
||||
[
|
||||
`You collected **${result.amount.toLocaleString()} Cookies**! 🍪`,
|
||||
"",
|
||||
`Your next batch will be ready <t:${result.cooldown.next}:R>.`
|
||||
].join("\n")
|
||||
)
|
||||
.addFields({
|
||||
name: "Your Vault",
|
||||
value:
|
||||
`**${result.balance.toLocaleString()} 🍪**`
|
||||
})
|
||||
.setFooter({
|
||||
text:
|
||||
`${footerType}:${userId}`
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function buildCollectCooldownEmbed(
|
||||
userId,
|
||||
result,
|
||||
footerType = "crumbSaucerCollect"
|
||||
) {
|
||||
return new EmbedBuilder()
|
||||
.setTitle(
|
||||
"🍪 Cookie Bakery"
|
||||
)
|
||||
.setDescription(
|
||||
[
|
||||
"Your next batch isn't ready yet.",
|
||||
"",
|
||||
`Come back <t:${result.cooldown.next}:R>.`
|
||||
].join("\n")
|
||||
)
|
||||
.addFields({
|
||||
name: "Your Vault",
|
||||
value:
|
||||
`**${result.balance.toLocaleString()} 🍪**`
|
||||
})
|
||||
.setFooter({
|
||||
text:
|
||||
`${footerType}:${userId}`
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ============================================================
|
||||
* SHOW SAUCER COLLECT PAGE
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
@@ -78,7 +213,6 @@ async function showCollectPage(
|
||||
.setTitle(
|
||||
"🍪 Crumb Saucer Cookie Bakery"
|
||||
)
|
||||
|
||||
.setDescription(
|
||||
[
|
||||
"Fresh Cookies are prepared regularly.",
|
||||
@@ -88,15 +222,11 @@ async function showCollectPage(
|
||||
"Your collection cooldown is shared with `/cookies collect`."
|
||||
].join("\n")
|
||||
)
|
||||
|
||||
.addFields({
|
||||
name:
|
||||
"Your Vault",
|
||||
|
||||
name: "Your Vault",
|
||||
value:
|
||||
`**${balance.toLocaleString()} 🍪**`
|
||||
})
|
||||
|
||||
.setFooter({
|
||||
text:
|
||||
`crumbSaucerCollect:${interaction.user.id}`
|
||||
@@ -104,10 +234,7 @@ async function showCollectPage(
|
||||
|
||||
|
||||
return interaction.update({
|
||||
embeds: [
|
||||
embed
|
||||
],
|
||||
|
||||
embeds: [embed],
|
||||
components:
|
||||
buildCollectButtons()
|
||||
});
|
||||
@@ -116,7 +243,7 @@ async function showCollectPage(
|
||||
|
||||
/*
|
||||
* ============================================================
|
||||
* COLLECT
|
||||
* SAUCER COLLECT WRAPPER
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
@@ -143,154 +270,27 @@ async function collectCookiesFromSaucer(
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Mongo-backed cooldown.
|
||||
*
|
||||
* Same pattern as Cookie Heist:
|
||||
*
|
||||
* userId
|
||||
* guildId
|
||||
* cooldown key
|
||||
* duration
|
||||
*/
|
||||
|
||||
const cooldown =
|
||||
await claimCooldown(
|
||||
interaction.user.id,
|
||||
interaction.guildId,
|
||||
COLLECT_COOLDOWN_KEY,
|
||||
COLLECT_COOLDOWN
|
||||
);
|
||||
|
||||
|
||||
/*
|
||||
* Already collected.
|
||||
*/
|
||||
|
||||
if (
|
||||
!cooldown.claimed
|
||||
) {
|
||||
const balance =
|
||||
await getCookieBalance(
|
||||
interaction.user.id
|
||||
);
|
||||
|
||||
|
||||
const embed =
|
||||
new EmbedBuilder()
|
||||
.setTitle(
|
||||
"🍪 Cookie Bakery"
|
||||
)
|
||||
|
||||
.setDescription(
|
||||
[
|
||||
"Your next batch isn't ready yet.",
|
||||
"",
|
||||
`Come back <t:${cooldown.next}:R>.`
|
||||
].join("\n")
|
||||
)
|
||||
|
||||
.addFields({
|
||||
name:
|
||||
"Your Vault",
|
||||
|
||||
value:
|
||||
`**${balance.toLocaleString()} 🍪**`
|
||||
})
|
||||
|
||||
.setFooter({
|
||||
text:
|
||||
`crumbSaucerCollect:${interaction.user.id}`
|
||||
});
|
||||
|
||||
|
||||
return interaction.update({
|
||||
embeds: [
|
||||
embed
|
||||
],
|
||||
|
||||
components:
|
||||
buildCollectButtons()
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ========================================================
|
||||
* GENERATE BATCH
|
||||
* ========================================================
|
||||
*/
|
||||
|
||||
const amount =
|
||||
Math.floor(
|
||||
Math.random() *
|
||||
COLLECT_MAX
|
||||
)
|
||||
+
|
||||
1;
|
||||
|
||||
|
||||
await changeCookies(
|
||||
interaction.user.id,
|
||||
amount
|
||||
);
|
||||
|
||||
|
||||
/*
|
||||
* Profile / achievements.
|
||||
*/
|
||||
|
||||
await trackProfileEvent(
|
||||
interaction,
|
||||
interaction.user.id,
|
||||
{
|
||||
type:
|
||||
"cookie_gain",
|
||||
|
||||
amount
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
const balance =
|
||||
await getCookieBalance(
|
||||
const result =
|
||||
await collectCookies(
|
||||
interaction,
|
||||
interaction.user.id
|
||||
);
|
||||
|
||||
|
||||
const embed =
|
||||
new EmbedBuilder()
|
||||
.setTitle(
|
||||
"🍪 Fresh Batch!"
|
||||
result.collected
|
||||
? buildCollectSuccessEmbed(
|
||||
interaction.user.id,
|
||||
result
|
||||
)
|
||||
|
||||
.setDescription(
|
||||
[
|
||||
`You collected **${amount.toLocaleString()} Cookies**! 🍪`,
|
||||
"",
|
||||
`Your next batch will be ready <t:${cooldown.next}:R>.`
|
||||
].join("\n")
|
||||
)
|
||||
|
||||
.addFields({
|
||||
name:
|
||||
"Your Vault",
|
||||
|
||||
value:
|
||||
`**${balance.toLocaleString()} 🍪**`
|
||||
})
|
||||
|
||||
.setFooter({
|
||||
text:
|
||||
`crumbSaucerCollect:${interaction.user.id}`
|
||||
});
|
||||
: buildCollectCooldownEmbed(
|
||||
interaction.user.id,
|
||||
result
|
||||
);
|
||||
|
||||
|
||||
return interaction.update({
|
||||
embeds: [
|
||||
embed
|
||||
],
|
||||
|
||||
embeds: [embed],
|
||||
components:
|
||||
buildCollectButtons()
|
||||
});
|
||||
@@ -361,10 +361,6 @@ function getCollectOwner(
|
||||
[];
|
||||
|
||||
|
||||
/*
|
||||
* Main menu -> Cookie Bakery.
|
||||
*/
|
||||
|
||||
if (
|
||||
parts[0] ===
|
||||
"crumbSaucer"
|
||||
@@ -373,10 +369,6 @@ function getCollectOwner(
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Bakery page/result.
|
||||
*/
|
||||
|
||||
if (
|
||||
parts[0] ===
|
||||
"crumbSaucerCollect"
|
||||
@@ -396,6 +388,15 @@ function getCollectOwner(
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
collectCookies,
|
||||
|
||||
buildCollectSuccessEmbed,
|
||||
buildCollectCooldownEmbed,
|
||||
|
||||
showCollectPage,
|
||||
collectCookiesFromSaucer
|
||||
collectCookiesFromSaucer,
|
||||
|
||||
COLLECT_COOLDOWN,
|
||||
COLLECT_MIN,
|
||||
COLLECT_MAX
|
||||
};
|
||||
Reference in New Issue
Block a user