Skip to content

Commit

Permalink
feat: move to Bun APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
feenko committed Dec 7, 2024
1 parent 1ca5a38 commit 2ac5984
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ if (!process.versions.bun) {
throw new Error("Bun runtime is required to run this application. Please install Bun from https://bun.sh/.");
}

Bun.env.TZ = "Europe/Warsaw";

export const client = new Client({
intents: Object.values(GatewayIntentBits) as GatewayIntentBits[],
partials: Object.values(Partials) as Partials[],
Expand Down
8 changes: 7 additions & 1 deletion src/commands/info/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
InteractionContextType,
SlashCommandBuilder,
} from "discord.js";
import { client } from "~/app";

import type { I18nFunction } from "~/lib/i18n";
import { Command } from "~/structures/command";
Expand Down Expand Up @@ -39,14 +40,19 @@ export default class Ping extends Command {
},
{
name: $("modules.ping.fields.database"),
value: `0ms`,
value: `${client.prisma.latency}ms`,
inline: true,
},
{
name: $("modules.ping.fields.api"),
value: `0ms`,
inline: true,
},
{
name: "Cobalt",
value: `${client.cobalt.latency}ms`,
inline: true,
},
]);

const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function formatRelativeTimestamp(timestamp: number | null): string {
*/
export function formatArrayWithEllipsis<T>(
arr: T[],
formatter: (item: T) => string,
formatter: (item: T) => string = item => `${item}`,
limit = 3,
): string | null {
if (arr.length === 0) return null;
Expand Down
15 changes: 13 additions & 2 deletions src/services/cobalt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ export interface CobaltResponse {

export class CobaltService {
private instance: string;
public latency = -1;

constructor(instanceUrl = process.env.COBALT_API_URL || "https://cobalt.meteors.cc/") {
constructor(instanceUrl = Bun.env.COBALT_API_URL || "https://cobalt.meteors.cc/") {
this.instance = instanceUrl;

const checkLatency = async () => {
const start = Date.now();
await fetch(this.instance)
.then(res => this.latency = res.ok ? Date.now() - start : -1)
.catch(() => this.latency = -1);
};

void checkLatency();
setInterval(checkLatency, 15 * 60 * 1000);
}

/**
Expand All @@ -33,7 +44,7 @@ export class CobaltService {
"Content-Type": "application/json",
"User-Agent": "meteor-application/1.0.0",
// https://github.com/imputnet/cobalt/blob/main/docs/api.md#authentication
"Authorization": `Api-Key ${process.env.COBALT_API_KEY}`,
"Authorization": `Api-Key ${Bun.env.COBALT_API_KEY}`,
},
// https://github.com/imputnet/cobalt/blob/main/docs/api.md#request-body
body: JSON.stringify({
Expand Down
2 changes: 1 addition & 1 deletion src/structures/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { Command } from "~/structures/command";
import type { Event } from "./event";

export class Client<Ready extends boolean = true> extends DiscordClient<Ready> {
public readonly prisma = new PrismaClient();
public readonly prisma = new PrismaClient() as unknown as typeof PrismaClient & { latency: number; };
public readonly i18n = new I18n();
public readonly cobalt = new CobaltService();
public readonly commands = new Collection<string, Command>();
Expand Down
11 changes: 11 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type NodeEnv = "development" | "production";

declare module "bun" {
interface Env {
DISCORD_TOKEN: string;
DATABASE_URL: string;
COBALT_API_KEY: string;
COBALT_API_URL: string;
NODE_ENV: NodeEnv;
}
}

0 comments on commit 2ac5984

Please sign in to comment.