Skip to content

Commit

Permalink
chore(global): one last commit for HS ship
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhalili2006 committed Jan 29, 2025
1 parent b1487ab commit ea3288e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start": "dotenvx run -f .env.production -- tsx src/index.ts",
"dev": "dotenvx run -f .env.development -- tsx watch src/index.ts",
"dev:commands": "dotenvx run -f .env.development -- tsx src/scripts/deploy-commands.ts",
"commands": "dotenvx run -f .env.production -- tsx src/scripts/deploy-commands.ts",
"build": "tsc --build && npm run sentry:sourcemaps",
"test": "echo \"Error: no test specified\" && exit 1",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org recaptime-dev --project hcb-explorer-discord ./dist && sentry-cli sourcemaps upload --org recaptime-dev --project hcb-explorer-discord ./dist"
Expand Down
8 changes: 7 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { REST, Routes } from "discord.js";
import pkgMetadata from "../package.json";

export default {
const env = {
port: Number(process.env.PORT) || 3000,
appId: process.env.DISCORD_BOT_APP_ID,
botToken: process.env.DISCORD_BOT_TOKEN,
sentryDSN: process.env.SENTRY_DSN || "",
url: process.env.PUBLIC_URL || `http://localhost:${process.env.PORT || 3000}`,
repo: process.env.REPO_URL || pkgMetadata.repository.url,
support: {
discord: process.env.DISCORD_INVITE_CODE || "FyBYQrJtUX",
Expand All @@ -31,3 +33,7 @@ export default {
"org_RRu9K4", // https://hcb.hackclub.com/lorebooks-wiki
]
}

export const discordApIRest = new REST({ version: '10' }).setToken(env?.botToken || "");

export default env
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import "./lib/sentry";
import Sentry from './lib/sentry';

import { Events, GatewayIntentBits, ActivityType, Collection, EmbedBuilder } from 'discord.js';
import { Events, GatewayIntentBits, ActivityType, Collection, EmbedBuilder, Routes } from 'discord.js';
import { ExtendedClient } from './lib/extendedClient';
import { serve as honoServe, } from '@hono/node-server';
import { Hono } from "hono";
import config from './config';
import { Context, Hono } from "hono";
import config, { discordApIRest } from './config';

/** Check if we have the Discord bot token first */
if (!config.botToken) {
Expand Down Expand Up @@ -44,6 +44,11 @@ app.get("/ping", (c) => {
app.get("/invite", (c) => {
return c.redirect(`https://discord.com/oauth2/authorize?client_id=${config.appId}`)
})
app.get("/internals/whoami", async(c: Context) => {
const whoami = await client.user

return c.json(whoami)
})

import { ping, donateLinks, inviteLink } from './commands/utility';
import { hcb } from './commands/hcb';
Expand All @@ -55,6 +60,7 @@ client.once(Events.ClientReady, async readyClient => {
port: config.port
});
console.log('[api-server]', `Hono server running on port ${config.port}`);
console.log('[api-server]', `accessible at ${config.url}`)

client.commands.set("ping", ping);
client.commands.set("donate", donateLinks);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/hcb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ function parseOrgType(org_id: string | null, org_type: Organization["category"])

export async function handleOrgDataEmbed(interact: CommandInteraction, data: HcbOrgResponse) {
const { id, category, logo, background_image, balances, slug } = data.body

// buttons
const donationBtn = new ButtonBuilder()
.setStyle(ButtonStyle.Link).setLabel("Donate")
.setURL(data.body.donation_link || "https://hcb.hackclub.com")
const website = new ButtonBuilder()
.setStyle(ButtonStyle.Link).setLabel("Website")
.setURL(data.body.website || `https://hcb.hackclub.com/${slug}`)

const bal_normalized = {
total_raised: numberWithCommas(Math.abs(balances.total_raised/ 100)),
current: numberWithCommas(Math.abs(balances.balance_cents / 100)),
Expand Down Expand Up @@ -194,5 +197,6 @@ export async function handleOrgBalanceEmbed(interact: CommandInteraction, data:
}

export async function handleTxnDataEmbed(_interact: CommandInteraction, data: HcbTxn) {
// to be implemented soon
console.log(data)
}
10 changes: 5 additions & 5 deletions src/scripts/deploy-commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DiscordAPIError, REST, Routes } from 'discord.js';
import config from '../config';
import { DiscordAPIError, Routes } from 'discord.js';
import config, { discordApIRest } from '../config';

const commands = [];

Expand All @@ -13,12 +13,11 @@ commands.push(inviteLink.data.toJSON());

(async () => {
try {
if (!config.botToken || !config.appId) {
if (config.botToken == undefined || config.appId == undefined) {
throw new Error('Bot token and/or Discord application is not defined in the configuration.');
}
const rest = new REST({ version: '10' }).setToken(config.botToken);

const result = await rest.put(
const result = await discordApIRest.put(
Routes.applicationCommands(config.appId),
{ body: commands }
);
Expand All @@ -33,5 +32,6 @@ commands.push(inviteLink.data.toJSON());
} else {
console.log(error);
}
process.exit(1)
}
})();

0 comments on commit ea3288e

Please sign in to comment.