Skip to content

Commit 2bb2d7a

Browse files
committed
feat: multi-guild compatible
1 parent 2a05e4b commit 2bb2d7a

File tree

8 files changed

+97
-24
lines changed

8 files changed

+97
-24
lines changed

prisma/schema.prisma

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,39 @@ datasource db {
77
url = env("DATABASE_URL")
88
}
99

10+
model Guild {
11+
id String @id
12+
createdAt DateTime @default(now())
13+
updatedAt DateTime @updatedAt
14+
15+
tags Tag[]
16+
}
17+
18+
model User {
19+
id String @id
20+
createdAt DateTime @default(now())
21+
updatedAt DateTime @updatedAt
22+
23+
tags Tag[]
24+
lastEditedTags Tag[] @relation("LastEditedBy")
25+
}
26+
1027
model Tag {
1128
id String @id @default(uuid())
1229
createdAt DateTime @default(now())
1330
updatedAt DateTime @updatedAt
31+
32+
guild Guild @relation(fields: [guildId], references: [id])
33+
guildId String
34+
35+
author User @relation(fields: [authorId], references: [id])
36+
authorId String
37+
1438
trigger String
1539
content String
1640
uses Int @default(0)
41+
lastUsed DateTime?
42+
lastEditedBy User? @relation(fields: [lastEditedById], references: [id], name: "LastEditedBy")
43+
lastEditedById String?
44+
lastEditedAt DateTime?
1745
}

src/commands/create-tag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default class CreateTagCommand extends SlashCommand {
9393
});
9494
}
9595

96-
const createdTag = await createTag(trigger, content);
96+
const createdTag = await createTag(ctx.guildID, ctx.user.id, trigger, content);
9797
if (!createdTag) {
9898
return ctx.send({
9999
content: 'Something went wrong!',

src/commands/delete-tag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class DeleteTagCommand extends SlashCommand {
4343
}
4444

4545
async autocomplete(ctx: AutocompleteContext) {
46-
const allTags = await getAllTags();
46+
const allTags = await getAllTags(ctx.guildID);
4747
const tag = allTags
4848
.map((tag) => ({
4949
name: tag.trigger,

src/commands/edit-tag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default class EditTagCommand extends SlashCommand {
9494
});
9595
}
9696

97-
const editedTag = await editTag(tagId, trigger, content);
97+
const editedTag = await editTag(tagId, ctx.user.id, trigger, content);
9898
if (!editedTag) {
9999
return ctx.send({
100100
content: 'Something went wrong!',
@@ -109,7 +109,7 @@ export default class EditTagCommand extends SlashCommand {
109109
}
110110

111111
async autocomplete(ctx: AutocompleteContext) {
112-
const allTags = await getAllTags();
112+
const allTags = await getAllTags(ctx.guildID);
113113
const tag = allTags
114114
.map((tag) => ({
115115
name: tag.trigger,

src/commands/list-tags.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,27 @@ export default class ListTagsCommand extends SlashCommand {
2121
async run(ctx: CommandContext) {
2222
const privateReply = ctx.options.private ?? false;
2323

24-
const tags = await getAllTags();
24+
const tags = await getAllTags(ctx.guildID);
2525
if (!tags || tags.length === 0) {
2626
return ctx.send({
2727
content: 'No tags exist!',
2828
ephemeral: true
2929
});
3030
}
3131

32-
const tagList = tags.map((tag) => `**${tag.trigger}** - ${tag.uses} uses - \`${tag.id}\``).join('\n');
32+
const tagList = tags
33+
.map((tag) => {
34+
const isLastUsed = tag.lastUsed ? `<t:${Math.floor(new Date(tag.lastUsed).getTime() / 1000)}:R>` : 'Never';
35+
const isLastEditedAt = tag.lastEditedAt
36+
? `<t:${Math.floor(new Date(tag.lastEditedAt).getTime() / 1000)}:R>`
37+
: 'Never';
38+
return `**__${tag.trigger}__**\n- **Uses**: ${tag.uses}\n- **Last Used**: ${isLastUsed}\n- **Author**: <@${
39+
tag.authorId
40+
}>\n- **Last Edited By**: <@${tag.lastEditedById}>\n- **Created At**: <t:${Math.floor(
41+
new Date(tag.createdAt).getTime() / 1000
42+
)}:f>\n- **Last Edited At**: ${isLastEditedAt}`;
43+
})
44+
.join('\n\n');
3345
const embed = new EmbedBuilder().setTitle('All Tags').setDescription(tagList).setColor(0x6472e0);
3446

3547
return ctx.send({
@@ -40,7 +52,7 @@ export default class ListTagsCommand extends SlashCommand {
4052
}
4153

4254
async autocomplete(ctx: AutocompleteContext) {
43-
const allTags = await getAllTags();
55+
const allTags = await getAllTags(ctx.guildID);
4456
const tag = allTags
4557
.map((tag) => ({
4658
name: tag.trigger,

src/commands/send-tag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default class SendTagCommand extends SlashCommand {
4848
}
4949

5050
async autocomplete(ctx: AutocompleteContext) {
51-
const allTags = await getAllTags();
51+
const allTags = await getAllTags(ctx.guildID);
5252
const tag = allTags
5353
.map((tag) => ({
5454
name: tag.trigger,

src/db.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,77 @@
11
import { PrismaClient } from '@prisma/client';
22

3-
const db = new PrismaClient({
3+
export const db = new PrismaClient({
44
errorFormat: 'pretty'
55
});
66

7-
export const createTag = async (trigger: string, content: string) => {
8-
return db.tag.create({
7+
export const createTag = async (guildId: string, authorId: string, trigger: string, content: string) => {
8+
return await db.tag.create({
99
data: {
10+
guildId,
11+
authorId,
1012
trigger,
11-
content
13+
content,
14+
lastEditedById: authorId
1215
}
1316
});
1417
};
1518

1619
export const getTag = async (tagId: string) => {
17-
return db.tag.findUnique({
20+
return await db.tag.findUnique({
1821
where: {
1922
id: tagId
2023
}
2124
});
2225
};
2326

2427
export const getTagByTrigger = async (trigger: string) => {
25-
return db.tag.findFirst({
28+
return await db.tag.findFirst({
2629
where: {
2730
trigger
2831
}
2932
});
3033
};
3134

32-
export const getAllTags = async () => {
33-
return db.tag.findMany();
35+
export const getAllTags = async (guildId: string) => {
36+
return await db.tag.findMany({
37+
where: {
38+
guildId
39+
}
40+
});
3441
};
3542

36-
export const editTag = async (tagId: string, trigger: string, content: string) => {
37-
return db.tag.update({
43+
export const editTag = async (tagId: string, editorId: string, trigger: string, content: string) => {
44+
return await db.tag.update({
3845
where: {
3946
id: tagId
4047
},
4148
data: {
49+
lastEditedById: editorId,
50+
lastEditedAt: new Date(),
4251
trigger,
4352
content
4453
}
4554
});
4655
};
4756

4857
export const deleteTag = async (tagId: string) => {
49-
return db.tag.delete({
58+
return await db.tag.delete({
5059
where: {
5160
id: tagId
5261
}
5362
});
5463
};
5564

5665
export const incrementTagUses = async (tagId: string) => {
57-
return db.tag.update({
66+
return await db.tag.update({
5867
where: {
5968
id: tagId
6069
},
6170
data: {
6271
uses: {
6372
increment: 1
64-
}
73+
},
74+
lastUsed: new Date()
6575
}
6676
});
6777
};

src/index.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import dotenv from 'dotenv';
22
import { SlashCreator, FastifyServer } from 'slash-create';
33
import path from 'path';
44
import CatLoggr from 'cat-loggr/ts';
5+
import { db } from './db';
56

67
let dotenvPath = path.join(process.cwd(), '.env');
78
if (path.parse(process.cwd()).name === 'dist') dotenvPath = path.join(process.cwd(), '..', '.env');
@@ -21,9 +22,31 @@ creator.on('debug', (message) => logger.log(message));
2122
creator.on('warn', (message) => logger.warn(message));
2223
creator.on('error', (error) => logger.error(error));
2324
creator.on('synced', () => logger.info('Commands synced!'));
24-
creator.on('commandRun', (command, _, ctx) =>
25-
logger.info(`${ctx.user.username} (${ctx.user.id}) ran command ${command.commandName}`)
26-
);
25+
creator.on('commandRun', async (command, _, ctx) => {
26+
await db.guild.upsert({
27+
where: {
28+
id: ctx.guildID
29+
},
30+
create: {
31+
id: ctx.guildID
32+
},
33+
update: {
34+
id: ctx.guildID
35+
}
36+
});
37+
await db.user.upsert({
38+
where: {
39+
id: ctx.user.id
40+
},
41+
create: {
42+
id: ctx.user.id
43+
},
44+
update: {
45+
id: ctx.user.id
46+
}
47+
});
48+
logger.info(`${ctx.user.username} (${ctx.user.id}) ran command ${command.commandName}`);
49+
});
2750
creator.on('commandRegister', (command) => logger.info(`Registered command ${command.commandName}`));
2851
creator.on('commandError', (command, error) => logger.error(`Command ${command.commandName}:`, error));
2952

0 commit comments

Comments
 (0)