|
| 1 | +import { config } from "@lib/config.js"; |
| 2 | +import { makeCodeBlock } from "@lib/discord/messages.js"; |
| 3 | + |
| 4 | +import { ofetch } from "ofetch"; |
| 5 | + |
| 6 | +import { |
| 7 | + ContextMenuCommandBuilder, |
| 8 | + ApplicationCommandType, |
| 9 | + type MessageContextMenuCommandInteraction, |
| 10 | + MessageFlags, |
| 11 | + ActionRowBuilder, |
| 12 | + ButtonStyle, |
| 13 | + ButtonBuilder, |
| 14 | +} from "discord.js"; |
| 15 | + |
| 16 | +// TODO: try to make the official API package work |
| 17 | +async function createNote(body) { |
| 18 | + return ofetch("https://api.productboard.com/notes", { |
| 19 | + method: "POST", |
| 20 | + headers: { |
| 21 | + Authorization: `Bearer ${config.productBoard.token}`, |
| 22 | + }, |
| 23 | + body, |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +export default { |
| 28 | + data: new ContextMenuCommandBuilder() |
| 29 | + .setName("Add to product notes") |
| 30 | + .setType(ApplicationCommandType.Message), |
| 31 | + |
| 32 | + execute: async (interaction: MessageContextMenuCommandInteraction) => { |
| 33 | + const data = await createNote({ |
| 34 | + title: `Discord message from ${interaction.targetMessage.author.displayName} (in '${interaction.channel.name}')`, // this will only work for threads |
| 35 | + display_url: interaction.targetMessage.url, |
| 36 | + content: interaction.targetMessage.content, |
| 37 | + |
| 38 | + company: { id: config.productBoard.companyId }, |
| 39 | + user: { external_id: `discord:${interaction.targetMessage.author.id}` }, |
| 40 | + |
| 41 | + source: { origin: "discord", record_id: interaction.targetId }, |
| 42 | + }); |
| 43 | + |
| 44 | + const replyComponents = []; |
| 45 | + |
| 46 | + if (data.links?.html) { |
| 47 | + const button = new ButtonBuilder() |
| 48 | + .setLabel("Open in ProductBoard") |
| 49 | + .setStyle(ButtonStyle.Link) |
| 50 | + .setURL(data.links.html); |
| 51 | + |
| 52 | + replyComponents.push( |
| 53 | + new ActionRowBuilder<ButtonBuilder>().addComponents(button), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + await interaction.reply({ |
| 58 | + content: makeCodeBlock(JSON.stringify(data), "json"), |
| 59 | + |
| 60 | + components: replyComponents, |
| 61 | + |
| 62 | + flags: MessageFlags.Ephemeral, |
| 63 | + }); |
| 64 | + }, |
| 65 | +}; |
0 commit comments