Skip to content

ProductBoard #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"@uwu/configmasher": "latest",
"discord.js": "^14.15.3",
"octokit": "^4.0.2"
"ofetch": "^1.4.1"
},
"trustedDependencies": [
"@biomejs/biome"
Expand Down
36 changes: 33 additions & 3 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
export { default as close } from "./util/close.js";
export { default as reopen } from "./util/reopen.js";
import type {
SlashCommandBuilder,
ChatInputCommandInteraction,
ContextMenuCommandBuilder,
ContextMenuCommandInteraction,
SlashCommandOptionsOnlyBuilder,
} from "discord.js";

export { default as walkthrough } from "./util/walkthrough.js";
import { default as product_notes } from "./product/notes.js";

import { default as close } from "./util/close.js";
import { default as reopen } from "./util/reopen.js";
import { default as walkthrough } from "./util/walkthrough.js";

type AnyCommandBuilder =
| SlashCommandBuilder
| SlashCommandOptionsOnlyBuilder
| ContextMenuCommandBuilder;
type AnyInteraction =
| ChatInputCommandInteraction
| ContextMenuCommandInteraction;

const commandObject: {
[key: string]: {
data: AnyCommandBuilder;
execute: (interaction: AnyInteraction) => unknown;
};
} = {};

for (const command of [product_notes, close, reopen, walkthrough]) {
commandObject[command.data.name] = command;
}

export default commandObject;
65 changes: 65 additions & 0 deletions src/commands/product/notes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { config } from "@lib/config.js";
import { makeCodeBlock } from "@lib/discord/messages.js";

import { ofetch } from "ofetch";

import {
ContextMenuCommandBuilder,
ApplicationCommandType,
type MessageContextMenuCommandInteraction,
MessageFlags,
ActionRowBuilder,
ButtonStyle,
ButtonBuilder,
} from "discord.js";

// TODO: try to make the official API package work
async function createNote(body) {
return ofetch("https://api.productboard.com/notes", {
method: "POST",
headers: {
Authorization: `Bearer ${config.productBoard.token}`,
},
body,
});
}

export default {
data: new ContextMenuCommandBuilder()
.setName("Add to product notes")
.setType(ApplicationCommandType.Message),

execute: async (interaction: MessageContextMenuCommandInteraction) => {
const data = await createNote({
title: `Discord message from ${interaction.targetMessage.author.displayName} (in '${interaction.channel.name}')`, // this will only work for threads
display_url: interaction.targetMessage.url,
content: interaction.targetMessage.content,

company: { id: config.productBoard.companyId },
user: { external_id: `discord:${interaction.targetMessage.author.id}` },

source: { origin: "discord", record_id: interaction.targetId },
});

const replyComponents = [];

if (data.links?.html) {
const button = new ButtonBuilder()
.setLabel("Open in ProductBoard")
.setStyle(ButtonStyle.Link)
.setURL(data.links.html);

replyComponents.push(
new ActionRowBuilder<ButtonBuilder>().addComponents(button),
);
}

await interaction.reply({
content: makeCodeBlock(JSON.stringify(data), "json"),

components: replyComponents,

flags: MessageFlags.Ephemeral,
});
},
};
2 changes: 1 addition & 1 deletion src/commands/util/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
canMemberInteractWithThread,
getChannelFromInteraction,
isHelpPost,
} from "@lib/channels.js";
} from "@lib/discord/channels.js";

import {
type ThreadChannel,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/util/walkthrough.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config } from "@lib/config.js";

import { isHelpPost as isHelpThread } from "@lib/channels.js";
import { isHelpPost as isHelpThread } from "@lib/discord/channels.js";
import issueCategorySelector from "@components/issueCategorySelector.js";

import {
Expand Down
13 changes: 10 additions & 3 deletions src/deploy-commands.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { config } from "@lib/config.js";
import * as commands from "@commands/index.js";
import { getClientIDFromToken } from "@lib/discord/users.js";

import commands from "@commands/index.js";

import { REST, Routes } from "discord.js";

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(config.token);

const commandData = Object.values(commands).map((command) => command.data);
const commandData = Object.values(commands).map((command) =>
command.data.toJSON(),
);

console.log(
`Started refreshing ${commandData.length} application (/) commands.`,
Expand All @@ -15,7 +19,10 @@ console.log(
// The put method is used to fully refresh all commands in the guild with the current set
// biome-ignore lint/suspicious/noExplicitAny: TODO: need to figure out the proper type
const data: any = await rest.put(
Routes.applicationGuildCommands("1063886601165471814", config.serverId), // TODO: guess client ID from token
Routes.applicationGuildCommands(
getClientIDFromToken(config.token),
config.serverId,
),
{ body: commandData },
);

Expand Down
10 changes: 7 additions & 3 deletions src/events/commands.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import * as commands from "@commands/index.js";
import commands from "@commands/index.js";

import { type Client, Events } from "discord.js";

export default function registerEvents(client: Client) {
return client.on(Events.InteractionCreate, async (interaction) => {
if (interaction.isChatInputCommand()) {
if (
interaction.isChatInputCommand() ||
interaction.isMessageContextMenuCommand()
) {
const command = commands[interaction.commandName];

if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
`No command matching "${interaction.commandName}" was found.`,
);
return;
}
Expand All @@ -20,6 +23,7 @@ export default function registerEvents(client: Client) {
console.error(error);

// TODO: make generic replyOrFollowUp method
// TODO: log error if the user is admin
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: "There was an error while executing this command!",
Expand Down
8 changes: 8 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ interface Config {
vscode: string;
};

productBoard: {
token: string;
companyId: string;
};

presenceDelay: number;
}

Expand Down Expand Up @@ -50,5 +55,8 @@ export const { config, layers } = await loadConfig<Config>({
["emojis", "macos"],
["emojis", "windows"],
["emojis", "vscode"],

["productBoard", "token"],
["productBoard", "companyId"],
],
});
File renamed without changes.
5 changes: 5 additions & 0 deletions src/lib/discord/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function makeCodeBlock(text, language = "") {
return `\`\`\`${language}
${text}
\`\`\``;
}
3 changes: 3 additions & 0 deletions src/lib/discord/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getClientIDFromToken(token: string): string {
return atob(token.split(".")[0]);
}