Skip to content

Commit 8d926ad

Browse files
committed
fix: dont allow duplicated family requests
1 parent 7001089 commit 8d926ad

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

src/commands/family.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ export async function execute(
287287
return;
288288
}
289289

290+
const existingMarriage = await FamilyManager.getRelationship(
291+
interaction.user.id,
292+
targetUser.id,
293+
"spouse",
294+
);
295+
296+
if (existingMarriage) {
297+
await interaction.reply({
298+
content: `**YOU ARE ALREADY MARRIED TO ${targetUser.tag.toUpperCase()}!**`,
299+
flags: MessageFlags.Ephemeral,
300+
});
301+
return;
302+
}
303+
290304
const hasExistingRelationship = await FamilyManager.hasRelationship(
291305
interaction.user.id,
292306
targetUser.id,
@@ -688,6 +702,20 @@ export async function execute(
688702
return;
689703
}
690704

705+
const existingAdoption = await FamilyManager.getRelationship(
706+
interaction.user.id,
707+
targetUser.id,
708+
"parent",
709+
);
710+
711+
if (existingAdoption) {
712+
await interaction.reply({
713+
content: `**YOU HAVE ALREADY ADOPTED ${targetUser.tag.toUpperCase()}!**`,
714+
flags: MessageFlags.Ephemeral,
715+
});
716+
return;
717+
}
718+
691719
const hasExistingRelationship = await FamilyManager.hasRelationship(
692720
interaction.user.id,
693721
targetUser.id,
@@ -1066,6 +1094,20 @@ export async function execute(
10661094
return;
10671095
}
10681096

1097+
const existingSibling = await FamilyManager.getRelationship(
1098+
interaction.user.id,
1099+
targetUser.id,
1100+
"sibling",
1101+
);
1102+
1103+
if (existingSibling) {
1104+
await interaction.reply({
1105+
content: `**YOU ARE ALREADY SIBLINGS WITH ${targetUser.tag.toUpperCase()}!**`,
1106+
flags: MessageFlags.Ephemeral,
1107+
});
1108+
return;
1109+
}
1110+
10691111
const hasExistingRelationship = await FamilyManager.hasRelationship(
10701112
interaction.user.id,
10711113
targetUser.id,

src/utils/familyManager.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,46 @@ export class FamilyManager {
136136
guildId: string,
137137
): Promise<void> {
138138
const data = await this.loadData();
139+
const now = new Date().toISOString();
140+
141+
const hasExact = (
142+
a: string,
143+
b: string,
144+
t: FamilyRelationship["relationshipType"],
145+
g: string,
146+
) =>
147+
data.relationships.some(
148+
(rel) =>
149+
rel.userId === a &&
150+
rel.relatedUserId === b &&
151+
rel.relationshipType === t &&
152+
rel.guildId === g,
153+
);
139154

140-
data.relationships.push({
141-
userId,
142-
relatedUserId,
143-
relationshipType: type,
144-
establishedAt: new Date().toISOString(),
145-
guildId,
146-
});
155+
const addExact = (
156+
a: string,
157+
b: string,
158+
t: FamilyRelationship["relationshipType"],
159+
) => {
160+
if (hasExact(a, b, t, guildId)) return;
147161

148-
if (type === "spouse" || type === "sibling") {
149162
data.relationships.push({
150-
userId: relatedUserId,
151-
relatedUserId: userId,
152-
relationshipType: type,
153-
establishedAt: new Date().toISOString(),
163+
userId: a,
164+
relatedUserId: b,
165+
relationshipType: t,
166+
establishedAt: now,
154167
guildId,
155168
});
169+
};
170+
171+
addExact(userId, relatedUserId, type);
172+
173+
if (type === "spouse" || type === "sibling") {
174+
addExact(relatedUserId, userId, type);
156175
}
157176

158177
if (type === "parent") {
159-
data.relationships.push({
160-
userId: relatedUserId,
161-
relatedUserId: userId,
162-
relationshipType: "child",
163-
establishedAt: new Date().toISOString(),
164-
guildId,
165-
});
178+
addExact(relatedUserId, userId, "child");
166179
}
167180

168181
await this.saveData(data);

0 commit comments

Comments
 (0)