Skip to content

Commit 19de725

Browse files
committed
feat: delete old bookmarks command
1 parent 01c39dd commit 19de725

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
.wrangler/
3-
.dev.vars
3+
.dev.vars
4+
.env.prod
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {
2+
ApplicationCommandType,
3+
ApplicationIntegrationType,
4+
Command,
5+
type CommandInteraction,
6+
} from "@buape/carbon";
7+
8+
export default class DeleteOldBookmarksCommand extends Command {
9+
name = "Delete Old Bookmarks";
10+
description = "Deletes old bookmarks from V1 of Bookmarker";
11+
type: ApplicationCommandType = ApplicationCommandType.Message;
12+
integrationTypes: ApplicationIntegrationType[] = [
13+
ApplicationIntegrationType.GuildInstall,
14+
ApplicationIntegrationType.UserInstall,
15+
];
16+
17+
defer = true;
18+
ephemeral = true;
19+
20+
async run(interaction: CommandInteraction) {
21+
if (interaction.rawData.data.type !== ApplicationCommandType.Message)
22+
return;
23+
24+
if (!interaction.user) return;
25+
if (!interaction.channel) return;
26+
if (interaction.guild?.id)
27+
return interaction.reply({
28+
content: "This command can only be used in DMs.",
29+
});
30+
31+
const resolvedTargetMessage =
32+
interaction.rawData.data.resolved.messages[
33+
interaction.rawData.data.target_id
34+
];
35+
36+
const message = await interaction.client
37+
.fetchMessage(resolvedTargetMessage.channel_id, resolvedTargetMessage.id)
38+
.catch(() => {
39+
return null;
40+
});
41+
42+
if (!message)
43+
return interaction.reply({
44+
content:
45+
"Failed to fetch message. This command can only be used in your personal DMs with me.",
46+
});
47+
48+
console.log(message.author?.id, process.env.DISCORD_CLIENT_ID);
49+
50+
if (message.author?.id !== process.env.DISCORD_CLIENT_ID) {
51+
return interaction.reply({
52+
content: "I can only delete bookmarks that I created.",
53+
});
54+
}
55+
56+
try {
57+
await message.delete().then(async () => {
58+
return interaction.reply({
59+
content: "Bookmark deleted successfully!",
60+
});
61+
});
62+
} catch (error) {
63+
return interaction.reply({
64+
content: `Failed to delete bookmark. Try again?\n\n${error}`,
65+
});
66+
}
67+
}
68+
}

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Client } from "@buape/carbon";
22
import { createHandler } from "@buape/carbon/adapters/fetch";
33
import BookmarkCommand from "./commands/bookmark.js";
44
import HelpCommand from "./commands/help.js";
5+
import DeleteOldBookmarksCommand from "./commands/delete-old-bookmarks.js";
56
import ApplicationAuthorized from "./events/authorized.js";
67

78
const isBeta = process.env.ENVIRONMENT === "beta";
@@ -24,7 +25,11 @@ const client = new Client(
2425
devGuilds: process.env.DISCORD_DEV_GUILDS?.split(","),
2526
},
2627
{
27-
commands: [new BookmarkCommand(), new HelpCommand()],
28+
commands: [
29+
new BookmarkCommand(),
30+
new HelpCommand(),
31+
new DeleteOldBookmarksCommand(),
32+
],
2833
listeners: [new ApplicationAuthorized()],
2934
},
3035
);

0 commit comments

Comments
 (0)