mirror of
https://github.com/ahmadk953/poixpixel-discord-bot.git
synced 2025-07-04 03:16:00 +00:00
fix(bot): fixed duplicate achievement sending
This commit is contained in:
parent
1eb068a634
commit
86e0a59188
2 changed files with 64 additions and 154 deletions
1
.github/FUNDING.yml
vendored
1
.github/FUNDING.yml
vendored
|
@ -1,3 +1,2 @@
|
||||||
github: ahmadk953
|
github: ahmadk953
|
||||||
patreon: poixpixel
|
|
||||||
thanks_dev: u/gh/ahmadk953
|
thanks_dev: u/gh/ahmadk953
|
||||||
|
|
|
@ -1,11 +1,4 @@
|
||||||
import {
|
import { Message, EmbedBuilder, TextChannel, Guild } from 'discord.js';
|
||||||
Message,
|
|
||||||
Client,
|
|
||||||
EmbedBuilder,
|
|
||||||
GuildMember,
|
|
||||||
TextChannel,
|
|
||||||
Guild,
|
|
||||||
} from 'discord.js';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
addXpToUser,
|
addXpToUser,
|
||||||
|
@ -21,99 +14,65 @@ import { loadConfig } from './configLoader.js';
|
||||||
import { generateAchievementCard } from './achievementCardGenerator.js';
|
import { generateAchievementCard } from './achievementCardGenerator.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check and process achievements for a user based on a message
|
* Handle achievement progress updates
|
||||||
* @param message - The message that triggered the check
|
* @param userId - ID of the user
|
||||||
|
* @param guild - Guild instance (can be null if not applicable)
|
||||||
|
* @param achievement - Achievement definition
|
||||||
|
* @param progress - Progress percentage (0-100)
|
||||||
|
* @param options - Additional options
|
||||||
|
*/
|
||||||
|
async function handleProgress(
|
||||||
|
userId: string,
|
||||||
|
guild: Guild | null,
|
||||||
|
achievement: schema.achievementDefinitionsTableTypes,
|
||||||
|
progress: number,
|
||||||
|
options: { skipAward?: boolean } = {},
|
||||||
|
): Promise<void> {
|
||||||
|
const { skipAward = false } = options;
|
||||||
|
const userAchievements = await getUserAchievements(userId);
|
||||||
|
const existing = userAchievements.find(
|
||||||
|
(a) => a.achievementId === achievement.id && a.earnedAt !== null,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (progress >= 100) {
|
||||||
|
if (!existing && !skipAward) {
|
||||||
|
const awarded = await awardAchievement(userId, achievement.id);
|
||||||
|
if (awarded && guild) {
|
||||||
|
await announceAchievement(guild, userId, achievement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await updateAchievementProgress(userId, achievement.id, progress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process message achievements based on user activity
|
||||||
|
* @param message - The message object from Discord
|
||||||
*/
|
*/
|
||||||
export async function processMessageAchievements(
|
export async function processMessageAchievements(
|
||||||
message: Message,
|
message: Message,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (message.author.bot) return;
|
if (message.author.bot) return;
|
||||||
|
|
||||||
const userData = await getUserLevel(message.author.id);
|
const userData = await getUserLevel(message.author.id);
|
||||||
const allAchievements = await getAllAchievements();
|
const allAchievements = await getAllAchievements();
|
||||||
|
|
||||||
const messageAchievements = allAchievements.filter(
|
for (const ach of allAchievements.filter(
|
||||||
(a) => a.requirementType === 'message_count',
|
(a) => a.requirementType === 'message_count',
|
||||||
);
|
)) {
|
||||||
|
|
||||||
for (const achievement of messageAchievements) {
|
|
||||||
const progress = Math.min(
|
const progress = Math.min(
|
||||||
100,
|
100,
|
||||||
(userData.messagesSent / achievement.threshold) * 100,
|
(userData.messagesSent / ach.threshold) * 100,
|
||||||
);
|
);
|
||||||
|
await handleProgress(message.author.id, message.guild!, ach, progress);
|
||||||
if (progress >= 100) {
|
|
||||||
const userAchievements = await getUserAchievements(message.author.id);
|
|
||||||
const existingAchievement = userAchievements.find(
|
|
||||||
(a) => a.achievementId === achievement.id && a.earnedAt !== null,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!existingAchievement) {
|
|
||||||
const awarded = await awardAchievement(
|
|
||||||
message.author.id,
|
|
||||||
achievement.id,
|
|
||||||
);
|
|
||||||
if (awarded) {
|
|
||||||
await announceAchievement(
|
|
||||||
message.guild!,
|
|
||||||
message.author.id,
|
|
||||||
achievement,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await updateAchievementProgress(
|
|
||||||
message.author.id,
|
|
||||||
achievement.id,
|
|
||||||
progress,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const levelAchievements = allAchievements.filter(
|
|
||||||
(a) => a.requirementType === 'level',
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const achievement of levelAchievements) {
|
|
||||||
const progress = Math.min(
|
|
||||||
100,
|
|
||||||
(userData.level / achievement.threshold) * 100,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (progress >= 100) {
|
|
||||||
const userAchievements = await getUserAchievements(message.author.id);
|
|
||||||
const existingAchievement = userAchievements.find(
|
|
||||||
(a) => a.achievementId === achievement.id && a.earnedAt !== null,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!existingAchievement) {
|
|
||||||
const awarded = await awardAchievement(
|
|
||||||
message.author.id,
|
|
||||||
achievement.id,
|
|
||||||
);
|
|
||||||
if (awarded) {
|
|
||||||
await announceAchievement(
|
|
||||||
message.guild!,
|
|
||||||
message.author.id,
|
|
||||||
achievement,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await updateAchievementProgress(
|
|
||||||
message.author.id,
|
|
||||||
achievement.id,
|
|
||||||
progress,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check achievements for level-ups
|
* Process level-up achievements when a user levels up
|
||||||
* @param memberId - Member ID who leveled up
|
* @param memberId - ID of the member who leveled up
|
||||||
* @param newLevel - New level value
|
* @param newLevel - The new level the member has reached
|
||||||
* @guild - Guild instance
|
* @param guild - Guild instance where the member belongs
|
||||||
*/
|
*/
|
||||||
export async function processLevelUpAchievements(
|
export async function processLevelUpAchievements(
|
||||||
memberId: string,
|
memberId: string,
|
||||||
|
@ -121,37 +80,19 @@ export async function processLevelUpAchievements(
|
||||||
guild: Guild,
|
guild: Guild,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const allAchievements = await getAllAchievements();
|
const allAchievements = await getAllAchievements();
|
||||||
|
for (const ach of allAchievements.filter(
|
||||||
const levelAchievements = allAchievements.filter(
|
|
||||||
(a) => a.requirementType === 'level',
|
(a) => a.requirementType === 'level',
|
||||||
);
|
)) {
|
||||||
|
const progress = Math.min(100, (newLevel / ach.threshold) * 100);
|
||||||
for (const achievement of levelAchievements) {
|
await handleProgress(memberId, guild, ach, progress);
|
||||||
const progress = Math.min(100, (newLevel / achievement.threshold) * 100);
|
|
||||||
|
|
||||||
if (progress >= 100) {
|
|
||||||
const userAchievements = await getUserAchievements(memberId);
|
|
||||||
const existingAchievement = userAchievements.find(
|
|
||||||
(a) => a.achievementId === achievement.id && a.earnedAt !== null,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!existingAchievement) {
|
|
||||||
const awarded = await awardAchievement(memberId, achievement.id);
|
|
||||||
if (awarded) {
|
|
||||||
await announceAchievement(guild, memberId, achievement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await updateAchievementProgress(memberId, achievement.id, progress);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process achievements for command usage
|
* Process command usage achievements when a command is invoked
|
||||||
* @param userId - User ID who used the command
|
* @param userId - ID of the user who invoked the command
|
||||||
* @param commandName - Name of the command
|
* @param commandName - Name of the command invoked
|
||||||
* @param client - Guild instance
|
* @param guild - Guild instance where the command was invoked
|
||||||
*/
|
*/
|
||||||
export async function processCommandAchievements(
|
export async function processCommandAchievements(
|
||||||
userId: string,
|
userId: string,
|
||||||
|
@ -159,7 +100,6 @@ export async function processCommandAchievements(
|
||||||
guild: Guild,
|
guild: Guild,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const allAchievements = await getAllAchievements();
|
const allAchievements = await getAllAchievements();
|
||||||
|
|
||||||
const commandAchievements = allAchievements.filter(
|
const commandAchievements = allAchievements.filter(
|
||||||
(a) =>
|
(a) =>
|
||||||
a.requirementType === 'command_usage' &&
|
a.requirementType === 'command_usage' &&
|
||||||
|
@ -167,26 +107,16 @@ export async function processCommandAchievements(
|
||||||
(a.requirement as any).command === commandName,
|
(a.requirement as any).command === commandName,
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const achievement of commandAchievements) {
|
for (const ach of commandAchievements) {
|
||||||
const userAchievements = await getUserAchievements(userId);
|
await handleProgress(userId, guild, ach, 100);
|
||||||
const existingAchievement = userAchievements.find(
|
|
||||||
(a) => a.achievementId === achievement.id && a.earnedAt !== null,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!existingAchievement) {
|
|
||||||
const awarded = await awardAchievement(userId, achievement.id);
|
|
||||||
if (awarded) {
|
|
||||||
await announceAchievement(guild, userId, achievement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process achievements for reaction events (add or remove)
|
* Process reaction achievements when a user reacts to a message
|
||||||
* @param userId - User ID who added/removed the reaction
|
* @param userId - ID of the user who reacted
|
||||||
* @param guild - Guild instance
|
* @param guild - Guild instance where the reaction occurred
|
||||||
* @param isRemoval - Whether this is a reaction removal (true) or addition (false)
|
* @param isRemoval - Whether the reaction was removed (default: false)
|
||||||
*/
|
*/
|
||||||
export async function processReactionAchievements(
|
export async function processReactionAchievements(
|
||||||
userId: string,
|
userId: string,
|
||||||
|
@ -198,40 +128,21 @@ export async function processReactionAchievements(
|
||||||
if (member.user.bot) return;
|
if (member.user.bot) return;
|
||||||
|
|
||||||
const allAchievements = await getAllAchievements();
|
const allAchievements = await getAllAchievements();
|
||||||
|
|
||||||
const reactionAchievements = allAchievements.filter(
|
const reactionAchievements = allAchievements.filter(
|
||||||
(a) => a.requirementType === 'reactions',
|
(a) => a.requirementType === 'reactions',
|
||||||
);
|
);
|
||||||
|
if (!reactionAchievements.length) return;
|
||||||
if (reactionAchievements.length === 0) return;
|
|
||||||
|
|
||||||
const reactionCount = await getUserReactionCount(userId);
|
const reactionCount = await getUserReactionCount(userId);
|
||||||
|
|
||||||
for (const achievement of reactionAchievements) {
|
for (const ach of reactionAchievements) {
|
||||||
const progress = Math.max(
|
const progress = Math.max(
|
||||||
0,
|
0,
|
||||||
Math.min(100, (reactionCount / achievement.threshold) * 100),
|
Math.min(100, (reactionCount / ach.threshold) * 100),
|
||||||
);
|
);
|
||||||
|
await handleProgress(userId, guild, ach, progress, {
|
||||||
if (progress >= 100 && !isRemoval) {
|
skipAward: isRemoval,
|
||||||
const userAchievements = await getUserAchievements(userId);
|
});
|
||||||
const existingAchievement = userAchievements.find(
|
|
||||||
(a) =>
|
|
||||||
a.achievementId === achievement.id &&
|
|
||||||
a.earnedAt !== null &&
|
|
||||||
a.earnedAt !== undefined &&
|
|
||||||
new Date(a.earnedAt).getTime() > 0,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!existingAchievement) {
|
|
||||||
const awarded = await awardAchievement(userId, achievement.id);
|
|
||||||
if (awarded) {
|
|
||||||
await announceAchievement(guild, userId, achievement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await updateAchievementProgress(userId, achievement.id, progress);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing reaction achievements:', error);
|
console.error('Error processing reaction achievements:', error);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue