Skip to content

Commit f9a00d2

Browse files
committed
feat: saving to postgresql db
1 parent 160beb3 commit f9a00d2

6 files changed

Lines changed: 60 additions & 3 deletions

File tree

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ services:
66
environment:
77
- BOT_TOKEN=${BOT_TOKEN}
88

9+
910
db:
1011
image: postgres:14.5-alpine
1112
restart: always
@@ -15,7 +16,6 @@ services:
1516
ports:
1617
- '5432:5432'
1718
volumes:
18-
- db:/var/lib/postgresql/data
19+
- botsofcode:/var/lib/postgresql/data
1920
volumes:
20-
db:
21-
driver: local
21+
botsofcode: {}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"@discordx/importer": "^1.1.10",
1717
"@discordx/pagination": "^3.2.0",
18+
"@prisma/client": "^4.4.0",
1819
"discord.js": "^14.4.0",
1920
"discordx": "^11.3.0",
2021
"dotenv": "^16.0.2"
@@ -23,6 +24,7 @@
2324
"@types/node": "^18.7.18",
2425
"nodemon": "^2.0.20",
2526
"prettier": "^2.7.1",
27+
"prisma": "^4.4.0",
2628
"ts-node": "^10.9.1",
2729
"typescript": "4.8.4"
2830
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- CreateTable
2+
CREATE TABLE "VerifyData" (
3+
"discordId" VARCHAR(18) NOT NULL,
4+
"ID" SERIAL NOT NULL,
5+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
6+
"realName" TEXT NOT NULL,
7+
8+
CONSTRAINT "VerifyData_pkey" PRIMARY KEY ("ID")
9+
);
10+
11+
-- CreateIndex
12+
CREATE UNIQUE INDEX "VerifyData_discordId_key" ON "VerifyData"("discordId");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "postgresql"

prisma/schema.prisma

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
generator client {
5+
provider = "prisma-client-js"
6+
}
7+
8+
datasource db {
9+
provider = "postgresql"
10+
url = env("DATABASE_URL")
11+
}
12+
13+
model VerifyData {
14+
discordId String @unique @db.VarChar(18)
15+
ID Int @id @default(autoincrement())
16+
createdAt DateTime @default(now())
17+
realName String
18+
}

src/commands/verify.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ import type {
66
User,
77
TextChannel,
88
} from "discord.js";
9+
910
import {
1011
ActionRowBuilder,
1112
ApplicationCommandOptionType,
1213
ButtonBuilder,
1314
ButtonStyle,
1415
} from "discord.js";
16+
1517
import { ButtonComponent, Discord, Slash, SlashOption } from "discordx";
1618

19+
import { PrismaClient } from "@prisma/client";
20+
import { exists } from "fs";
21+
const prisma = new PrismaClient();
22+
1723
@Discord()
1824
export class Command {
1925
name: string;
@@ -60,6 +66,22 @@ export class Command {
6066
CplusButton
6167
);
6268

69+
// Save / Updatename to DB
70+
const upsertVerify = await prisma.verifyData.upsert({
71+
where: {
72+
discordId: interaction.user.id,
73+
},
74+
update: {
75+
realName: name,
76+
},
77+
create: {
78+
discordId: interaction.user.id,
79+
realName: name!,
80+
},
81+
});
82+
83+
console.log(upsertVerify);
84+
6385
// Selector
6486
await interaction.editReply({
6587
content: `${name}, Select your course!`,

0 commit comments

Comments
 (0)