mirror of
https://github.com/ahmadk953/poixpixel-discord-bot.git
synced 2025-07-04 19:36:00 +00:00
fix(bot): fixed command achievements and progress updates
This commit is contained in:
parent
86e0a59188
commit
d71d9f0dcc
2 changed files with 25 additions and 19 deletions
|
@ -129,7 +129,6 @@ export async function updateAchievementProgress(
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
await ensureDbInitialized();
|
await ensureDbInitialized();
|
||||||
|
|
||||||
if (!db) {
|
if (!db) {
|
||||||
console.error(
|
console.error(
|
||||||
'Database not initialized, cannot update achievement progress',
|
'Database not initialized, cannot update achievement progress',
|
||||||
|
@ -149,21 +148,15 @@ export async function updateAchievementProgress(
|
||||||
.then((rows) => rows[0]);
|
.then((rows) => rows[0]);
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
if (existing.earnedAt) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.update(schema.userAchievementsTable)
|
.update(schema.userAchievementsTable)
|
||||||
.set({
|
.set({ progress })
|
||||||
progress: Math.floor(progress) > 100 ? 100 : Math.floor(progress),
|
|
||||||
})
|
|
||||||
.where(eq(schema.userAchievementsTable.id, existing.id));
|
.where(eq(schema.userAchievementsTable.id, existing.id));
|
||||||
} else {
|
} else {
|
||||||
await db.insert(schema.userAchievementsTable).values({
|
await db.insert(schema.userAchievementsTable).values({
|
||||||
discordId: userId,
|
discordId: userId,
|
||||||
achievementId: achievementId,
|
achievementId,
|
||||||
progress: Math.floor(progress) > 100 ? 100 : Math.floor(progress),
|
progress,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,15 +34,13 @@ async function handleProgress(
|
||||||
(a) => a.achievementId === achievement.id && a.earnedAt !== null,
|
(a) => a.achievementId === achievement.id && a.earnedAt !== null,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (progress >= 100) {
|
await updateAchievementProgress(userId, achievement.id, progress);
|
||||||
if (!existing && !skipAward) {
|
|
||||||
const awarded = await awardAchievement(userId, achievement.id);
|
if (progress === 100 && !existing && !skipAward) {
|
||||||
if (awarded && guild) {
|
const awarded = await awardAchievement(userId, achievement.id);
|
||||||
await announceAchievement(guild, userId, achievement);
|
if (awarded && guild) {
|
||||||
}
|
await announceAchievement(guild, userId, achievement);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
await updateAchievementProgress(userId, achievement.id, progress);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,8 +105,23 @@ export async function processCommandAchievements(
|
||||||
(a.requirement as any).command === commandName,
|
(a.requirement as any).command === commandName,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// fetch the user’s current achievement entries
|
||||||
|
const userAchievements = await getUserAchievements(userId);
|
||||||
|
|
||||||
for (const ach of commandAchievements) {
|
for (const ach of commandAchievements) {
|
||||||
await handleProgress(userId, guild, ach, 100);
|
// find existing progress, default to 0
|
||||||
|
const userAch = userAchievements.find((u) => u.achievementId === ach.id);
|
||||||
|
const oldProgress = userAch?.progress ?? 0;
|
||||||
|
|
||||||
|
// compute how many times they've run this command so far
|
||||||
|
const timesRanSoFar = (oldProgress / 100) * ach.threshold;
|
||||||
|
const newCount = timesRanSoFar + 1;
|
||||||
|
|
||||||
|
// convert back into a percentage
|
||||||
|
const newProgress = Math.min(100, (newCount / ach.threshold) * 100);
|
||||||
|
|
||||||
|
// Delegate to handleProgress which will update or award
|
||||||
|
await handleProgress(userId, guild, ach, newProgress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue