yaejunyang/packages/bot/db.ts
2026-02-09 18:31:56 +00:00

134 lines
3.3 KiB
TypeScript

import { prisma } from "../db/prisma";
import { DiscordUserProfile, DiscordGuildProfile, Voice } from "../db/generated/prisma/client";
export function getUserProfile(userId: string): Promise<DiscordUserProfile> {
return prisma.discordUserProfile.upsert({
where: {
userId: userId
},
update: {},
create: {
userId: userId,
},
});
}
export async function setUserProfile(userId: string, profile: DiscordUserProfile): Promise<void> {
await prisma.discordUserProfile.upsert({
where: {
userId: userId
},
update: {
voice: profile.voice,
nya: profile.nya
},
create: {
userId: userId,
}
});
}
export async function setUserNya(userId: string, nya: boolean): Promise<void> {
await prisma.discordUserProfile.upsert({
where: {
userId: userId
},
update: {
nya: nya
},
create: {
userId: userId,
}
});
}
export async function setUserVoice(userId: string, voice: Voice): Promise<void> {
await prisma.discordUserProfile.upsert({
where: {
userId: userId
},
update: {
voice: voice
},
create: {
userId: userId,
voice: voice
}
});
}
export async function setUserCanTypecast(userId: string, canTypecast: boolean): Promise<void> {
await prisma.discordUserProfile.upsert({
where: {
userId: userId
},
update: {
canTypecast: canTypecast
},
create: {
userId: userId,
}
});
}
export function getGuildProfile(guildId: string): Promise<DiscordGuildProfile> {
return prisma.discordGuildProfile.upsert({
where: { guildId: guildId },
update: {},
create: {
guildId: guildId,
},
});
}
export async function hasGuildReadChannel(guildId: string, channelId: string): Promise<boolean> {
return (
await prisma.discordGuildProfile.findFirst({
where: {
guildId: guildId,
readChannel: { has: channelId }
}
})
) != null;
}
export async function removeGuildReadChannel(guildId: string, channelId: string): Promise<void> {
const guildProfile = await prisma.discordGuildProfile.findUnique({
where: {
guildId: guildId
},
});
if (guildProfile) {
await prisma.discordGuildProfile.update({
where: {
guildId: guildId
},
data: {
readChannel: guildProfile.readChannel.filter(channel => channel != channelId),
},
});
}
}
export async function insertGuildReadChannel(guildId: string, channelId: string): Promise<void> {
await prisma.discordGuildProfile.upsert({
where: {
guildId: guildId,
NOT: {
readChannel: {
has: channelId,
},
}
},
update: {
readChannel: {
push: channelId,
},
},
create: {
guildId: guildId,
readChannel: [channelId],
},
});
}