Skip to content

Commit f251e46

Browse files
committed
Allow renaming tag
1 parent b6e369d commit f251e46

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

backend/src/plugin/tags/command/tagEdit.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ export default defineCommand({
3434
content: {
3535
type: OptionType.String,
3636
name: ["content", "c"],
37-
required: true,
3837
position: 1,
3938
maxLength: MAX_TAG_CONTENT_LENGTH,
4039
},
40+
newName: {
41+
type: OptionType.String,
42+
name: ["new-name", "rename", "nn", "rn"],
43+
description: "Change the name of the tag to something else.",
44+
maxLength: MAX_TAG_NAME_LENGTH,
45+
},
4146
},
4247

4348
preRun: (ctx) =>
@@ -51,7 +56,10 @@ export default defineCommand({
5156
ctx.squirrelCtx.db,
5257
ctx.guild.id,
5358
args.name,
54-
args.content,
59+
{
60+
name: args.newName ?? undefined,
61+
content: args.content ?? undefined,
62+
},
5563
);
5664

5765
if (oldTag === null) {
@@ -61,8 +69,13 @@ export default defineCommand({
6169
return;
6270
}
6371

72+
const newTag = {
73+
name: args.newName ?? oldTag.name,
74+
content: args.content ?? oldTag.content,
75+
};
76+
6477
onTagEdited
65-
.fire(ctx.squirrelCtx, ctx.guild, ctx.member, oldTag, args)
78+
.fire(ctx.squirrelCtx, ctx.guild, ctx.member, oldTag, newTag)
6679
.catch((error) => logger.error?.("Error in onTagDeleted", error));
6780

6881
await ctx.respond(

backend/src/plugin/tags/storage/tags.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Tag = z.strictObject({
99
});
1010

1111
const JustNameArray = z.strictObject({ name: z.string() }).array();
12+
const OldTag = z.strictObject({ content: z.string() });
1213

1314
export interface TagQuery {
1415
name: string;
@@ -76,7 +77,7 @@ export async function updateTag(
7677
db: Pool,
7778
guildID: string,
7879
name: string,
79-
content: string,
80+
tag: Partial<Tag>,
8081
): Promise<Tag | null> {
8182
const result = await db.query(
8283
`
@@ -85,18 +86,21 @@ export async function updateTag(
8586
WHERE "guildID" = $1 AND "name" = $2
8687
)
8788
UPDATE "tags_tags"
88-
SET "content" = $3
89+
SET "name" = COALESCE($3, $2), "content" = COALESCE($4, (SELECT "content" FROM "old"))
8990
WHERE "guildID" = $1 AND "name" = $2
90-
RETURNING "name", (SELECT "content" FROM "old") AS "content"
91+
RETURNING (SELECT "content" FROM "old") AS "content"
9192
`,
92-
[guildID, name, content],
93+
[guildID, name, tag.name, tag.content],
9394
);
9495

9596
if (result.rowCount !== 1) {
9697
return null;
9798
}
9899

99-
return dbParse(Tag, result.rows[0]);
100+
return {
101+
name,
102+
...dbParse(OldTag, result.rows[0]),
103+
};
100104
}
101105

102106
export async function deleteTag(

0 commit comments

Comments
 (0)