mirror of
https://github.com/SrIzan10/prox2-discord.git
synced 2026-06-06 01:06:57 +00:00
refactor: shitty unstable code
This commit is contained in:
@@ -54,6 +54,4 @@ model ReplyUser {
|
||||
postId Int
|
||||
post Post @relation(fields: [postId], references: [id])
|
||||
userHash String
|
||||
|
||||
@@unique([guildId, userHash])
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { commandModule, CommandType } from '@sern/handler';
|
||||
import { bold, MessageFlags } from 'discord.js';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
export default commandModule({
|
||||
type: CommandType.Modal,
|
||||
@@ -34,39 +35,72 @@ export default commandModule({
|
||||
}
|
||||
|
||||
const isOp = dbPost.posterHash === hashedUser;
|
||||
let userReplyId: number | null = null;
|
||||
let anonNumber: number | null = null;
|
||||
|
||||
if (!isOp) {
|
||||
const replyUserExists = await db.replyUser.findFirst({
|
||||
// try to find existing reply user and use their userId (the anon number)
|
||||
const existing = await db.replyUser.findFirst({
|
||||
where: {
|
||||
userHash: hashedUser,
|
||||
guildId: ctx.guildId!,
|
||||
postId: dbPost.id,
|
||||
},
|
||||
select: { userId: true },
|
||||
});
|
||||
|
||||
if (replyUserExists) {
|
||||
userReplyId = replyUserExists.id;
|
||||
if (existing) {
|
||||
anonNumber = existing.userId;
|
||||
} else {
|
||||
// compute next anon number
|
||||
const maxUserId = await db.replyUser.aggregate({
|
||||
where: { guildId: ctx.guildId!, postId: dbPost.id },
|
||||
_max: { userId: true },
|
||||
});
|
||||
const nextUserId = (maxUserId._max.userId || 0) + 1;
|
||||
const newReplyUser = await db.replyUser.create({
|
||||
data: {
|
||||
guildId: ctx.guildId!,
|
||||
postId: dbPost.id,
|
||||
userHash: hashedUser,
|
||||
userId: nextUserId,
|
||||
},
|
||||
});
|
||||
userReplyId = newReplyUser.id;
|
||||
const nextUserId = (maxUserId._max.userId ?? 0) + 1;
|
||||
|
||||
try {
|
||||
const newReplyUser = await db.replyUser.create({
|
||||
data: {
|
||||
guildId: ctx.guildId!,
|
||||
postId: dbPost.id,
|
||||
userHash: hashedUser,
|
||||
userId: nextUserId,
|
||||
},
|
||||
select: { userId: true },
|
||||
});
|
||||
anonNumber = newReplyUser.userId;
|
||||
} catch (err) {
|
||||
// handle concurrent create -> unique constraint
|
||||
if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002') {
|
||||
const retry = await db.replyUser.findFirst({
|
||||
where: {
|
||||
userHash: hashedUser,
|
||||
guildId: ctx.guildId!,
|
||||
postId: dbPost.id,
|
||||
},
|
||||
select: { userId: true },
|
||||
});
|
||||
if (retry) {
|
||||
anonNumber = retry.userId;
|
||||
} else {
|
||||
return ctx.reply({
|
||||
content: 'Could not register your anonymous reply. Please try again.',
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
console.error('replyUser.create error:', err);
|
||||
return ctx.reply({
|
||||
content: 'An unexpected error occurred.',
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await ctx.channel.send({
|
||||
content: `${bold(isOp ? 'OP' : `Anon #${userReplyId}`)}: ${text}`,
|
||||
content: `${bold(isOp ? 'OP' : `Anon #${anonNumber}`)}: ${text}`,
|
||||
allowedMentions: { parse: [] },
|
||||
});
|
||||
return ctx.deferUpdate();
|
||||
|
||||
Reference in New Issue
Block a user