Skip to content

Commit e122b0d

Browse files
committed
Separate tag creation from editing
Upserts prove to be quite a pain
1 parent 2acd41b commit e122b0d

4 files changed

Lines changed: 127 additions & 127 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { escapeMarkdown } from "#common/discord/markdown.ts";
2+
import { OptionType } from "#plugin/core/public/command.ts";
3+
import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
4+
import { permissionsGuard } from "#plugin/core/public/helper/commandGuards.ts";
5+
import { icons } from "#plugin/core/public/icons.ts";
6+
import {
7+
MAX_TAG_CONTENT_LENGTH,
8+
MAX_TAG_NAME_LENGTH,
9+
} from "#plugin/tags/constants.ts";
10+
import { tagsConfigStore } from "#plugin/tags/index.ts";
11+
import { createTag } from "#plugin/tags/storage/tags.ts";
12+
13+
export default defineCommand({
14+
name: ["tagcreate", "createtag", "tagnew", "newtag"],
15+
description: "Create a new tag.",
16+
17+
options: {
18+
name: {
19+
type: OptionType.String,
20+
name: ["name", "n"],
21+
description: "The name of the tag to create.",
22+
required: true,
23+
position: 0,
24+
maxLength: MAX_TAG_NAME_LENGTH,
25+
greedy: false,
26+
},
27+
content: {
28+
type: OptionType.String,
29+
name: ["content", "c"],
30+
required: true,
31+
position: 1,
32+
maxLength: MAX_TAG_CONTENT_LENGTH,
33+
},
34+
},
35+
36+
preRun: (ctx) =>
37+
permissionsGuard(
38+
ctx,
39+
tagsConfigStore,
40+
(permissions) => permissions.tag_create,
41+
),
42+
async run(ctx, args) {
43+
const success = await createTag(
44+
ctx.squirrelCtx.db,
45+
ctx.guild.id,
46+
args.name,
47+
args.content,
48+
);
49+
50+
if (success) {
51+
await ctx.respond(
52+
`${icons.success} Created tag '${escapeMarkdown(args.name)}'!`,
53+
);
54+
} else {
55+
await ctx.respond(
56+
`${icons.error} Tag '${escapeMarkdown(args.name)}' already exists!`,
57+
);
58+
}
59+
},
60+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { escapeMarkdown } from "#common/discord/markdown.ts";
2+
import { OptionType } from "#plugin/core/public/command.ts";
3+
import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
4+
import { permissionsGuard } from "#plugin/core/public/helper/commandGuards.ts";
5+
import { icons } from "#plugin/core/public/icons.ts";
6+
import {
7+
MAX_TAG_CONTENT_LENGTH,
8+
MAX_TAG_NAME_LENGTH,
9+
} from "#plugin/tags/constants.ts";
10+
import { autocompleteTags } from "#plugin/tags/helper/command.ts";
11+
import { tagsConfigStore } from "#plugin/tags/index.ts";
12+
import { updateTag } from "#plugin/tags/storage/tags.ts";
13+
14+
export default defineCommand({
15+
name: ["tagedit", "edittag", "tagupdate", "updatetag"],
16+
description: "Edit an existing tag.",
17+
18+
options: {
19+
name: {
20+
type: OptionType.String,
21+
name: ["name", "n"],
22+
description: "The name of the tag to modify.",
23+
required: true,
24+
position: 0,
25+
maxLength: MAX_TAG_NAME_LENGTH,
26+
greedy: false,
27+
28+
autocomplete: (ctx, value) => autocompleteTags(ctx, value),
29+
},
30+
content: {
31+
type: OptionType.String,
32+
name: ["content", "c"],
33+
required: true,
34+
position: 1,
35+
maxLength: MAX_TAG_CONTENT_LENGTH,
36+
},
37+
},
38+
39+
preRun: (ctx) =>
40+
permissionsGuard(
41+
ctx,
42+
tagsConfigStore,
43+
(permissions) => permissions.tag_edit,
44+
),
45+
async run(ctx, args) {
46+
const success = await updateTag(
47+
ctx.squirrelCtx.db,
48+
ctx.guild.id,
49+
args.name,
50+
args.content,
51+
);
52+
53+
if (success) {
54+
await ctx.respond(
55+
`${icons.success} Edited tag '${escapeMarkdown(args.name)}'!`,
56+
);
57+
} else {
58+
await ctx.respond(
59+
`${icons.error} Tag '${escapeMarkdown(args.name)}' does not exist!`,
60+
);
61+
}
62+
},
63+
});

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

Lines changed: 0 additions & 125 deletions
This file was deleted.

backend/src/plugin/tags/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { definePlugin } from "#plugin.ts";
22
import { ConfigStore } from "#plugin/core/public/configStore.ts";
33
import { defineConfig } from "#plugin/core/public/extensionPoints.ts";
44
import tag from "#plugin/tags/command/tag.ts";
5+
import tagCreate from "#plugin/tags/command/tagCreate.ts";
56
import tagDelete from "#plugin/tags/command/tagDelete.ts";
6-
import tagSet from "#plugin/tags/command/tagSet.ts";
7+
import tagEdit from "#plugin/tags/command/tagEdit.ts";
78
import { TagsConfig } from "#plugin/tags/config.ts";
89

910
export const tagsConfigStore = new ConfigStore(TagsConfig);
@@ -28,7 +29,8 @@ export default definePlugin({
2829
defaultValue: defaultConfig,
2930
}),
3031
tag,
31-
tagSet,
32+
tagCreate,
33+
tagEdit,
3234
tagDelete,
3335
],
3436
});

0 commit comments

Comments
 (0)