Skip to content

Commit

Permalink
feat: migrate to drizzle, add starter svelte stuff; optimize bot client
Browse files Browse the repository at this point in the history
Signed-off-by: Espi Marisa <[email protected]>
  • Loading branch information
espimarisa committed Mar 14, 2024
1 parent 62aaa42 commit 8db1462
Show file tree
Hide file tree
Showing 50 changed files with 624 additions and 236 deletions.
Binary file modified bun.lockb
Binary file not shown.
25 changes: 21 additions & 4 deletions locales/en-US/bot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
{
"COMMAND_TEST_NAME": "test",
"COMMAND_TEST_DESCRIPTION": "This is a test in English.",
"COMMAND_PING_NAME": "Ping",
"COMMAND_PING_DESCRIPTION": "Checks the current status and latency.",
"COMMAND_PING_PINGING": "⏳ Pinging...",
"COMMAND_PING_PONG": "🏓 Pong!",
"COMMAND_PING_LATENCY": "This message took {{latency}}ms to send. All seems OK!",

"COMMAND_TESTING_NAME": "testing",
"COMMAND_TESTING_DESCRIPTION": "This is testing something in English."
"COMMAND_ABOUT_NAME": "About",
"COMMAND_ABOUT_DESCRIPTION": "Returns information and statistics about the bot.",

"COMMAND_ABOUT_TITLE": "✨ About {{username}}",
"COMMAND_ABOUT_DETAILS": "{{username}}, made with 💖 by [Espi Marisa](https://espi.me).",
"COMMAND_ABOUT_CACHED_GUILDS": "Cached Guilds",
"COMMAND_ABOUT_CACHED_USERS": "Cached Users",
"COMMAND_ABOUT_UPTIME": "Uptime",
"COMMAND_ABOUT_HIBIKI_VERSION": "Hibiki Version",
"COMMAND_ABOUT_DISCORDJS_VERSION": "Discord.js Version",
"COMMAND_ABOUT_BUN_VERSION": "Bun Version",

"DAYS": "{{days}} dasy",
"HOURS": "{{amount}} hours",
"MINUTES": "{{minutes}} minutes",
"SECONDS": "{{amount}} seconds"
}
7 changes: 0 additions & 7 deletions locales/pl/bot.json

This file was deleted.

19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"scripts": {
"build": "bun run --env-file .env turbo run build",
"check": "tsc --noEmit",
"db:studio": "bun run --env-file .env -- turbo run studio --filter=@espimarisa/hibiki-db",
"dev:bot": "bun run --env-file .env turbo run dev --filter=@espimarisa/hibiki-bot --filter=@espimarisa/hibiki-db",
"dev:web": "bun run --env-file .env -- turbo run dev --filter=@espimarisa/hibiki-web --filter=@espimarisa/hibiki-db",
"format": "prettier --write .",
Expand All @@ -34,23 +35,23 @@
},
"devDependencies": {
"@espimarisa/eslint-config": "^4.2.4",
"@types/bun": "^1.0.5",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"dotenv-cli": "^7.3.0",
"eslint": "^8.56.0",
"@types/bun": "^1.0.8",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"dotenv-cli": "^7.4.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "npm:eslint-plugin-i@^2.29.1",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-security": "^2.1.0",
"eslint-plugin-security": "^2.1.1",
"eslint-plugin-unicorn": "^51.0.1",
"prettier": "^3.2.5",
"prettier-plugin-pkg": "^0.18.1",
"prettier-plugin-sh": "^0.14.0",
"prettier-plugin-svelte": "^3.2.1",
"turbo": "^1.12.3",
"typescript": "^5.3.3"
"prettier-plugin-svelte": "^3.2.2",
"turbo": "^1.12.5",
"typescript": "^5.4.2"
}
}
4 changes: 2 additions & 2 deletions packages/bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
},
"dependencies": {
"@discordjs/rest": "^2.2.0",
"discord-api-types": "^0.37.69",
"discord-api-types": "^0.37.71",
"discord.js": "^14.14.1",
"i18next": "^23.8.2"
"i18next": "^23.10.0"
}
}
14 changes: 12 additions & 2 deletions packages/bot/src/classes/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import type { HibikiEvent } from "$classes/Event.ts";
import { loadCommands, loadEvents, registerInteractions } from "$utils/loader.ts";
import env from "$shared/env.ts";
import logger from "$shared/logger.ts";
import { Client, type ClientOptions } from "discord.js";
import { ActivityType, Client, type ClientOptions } from "discord.js";
import path from "node:path";

// List of custom statuses to cycle thru
const activities = ["read if h", "meow", "guh"];
let activityState = 0;

// __dirname replacement in ESM
const pathDirname = path.dirname(Bun.fileURLToPath(new URL(import.meta.url)));

Expand Down Expand Up @@ -43,11 +47,17 @@ export class HibikiClient extends Client {

// Registers commands; pushes to only one guild if we're in development
await registerInteractions(this, env.NODE_ENV === "production" ? undefined : env.BOT_TEST_GUILD_ID);

// Cycles through statuses
setInterval(() => {
activityState = (activityState + 1) % activities.length;
const presence = activities[`${activityState}`];
this.user?.setActivity(`${presence} | v${env.npm_package_version}`, { type: ActivityType.Custom });
}, 60_000);
});
} catch (error) {
logger.error(`An error occured while starting:`);
throw new Error(Bun.inspect(error));
}
}
}
//
60 changes: 60 additions & 0 deletions packages/bot/src/commands/about.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { HibikiCommand } from "$classes/Command.ts";
import { fetchTotalCachedUsers, fetchTotalCachedGuilds } from "$classes/Sharder.ts";
import { HibikiColors } from "$shared/constants.ts";
import env from "$shared/env.ts";
import { t } from "$shared/i18n.ts";
import { version, type ChatInputCommandInteraction } from "discord.js";

export class HibikiPingCommand extends HibikiCommand {
public async runWithInteraction(interaction: ChatInputCommandInteraction, locale: string) {
// Gets the amount of cached guilds and users
const totalCachedGuilds = await fetchTotalCachedGuilds(this.bot.shard);
const totalCachedUsers = await fetchTotalCachedUsers(this.bot.shard);

await interaction.followUp({
embeds: [
{
title: t("COMMAND_ABOUT_TITLE", { username: this.bot.user?.username, lng: locale }),
description: t("COMMAND_ABOUT_DETAILS", { username: this.bot.user?.username, lng: locale }),
color: HibikiColors.GENERAL,
fields: [
{
name: t("COMMAND_ABOUT_CACHED_GUILDS", { lng: locale }),
value: `${totalCachedGuilds}`,
inline: true,
},
{
name: t("COMMAND_ABOUT_CACHED_USERS", { lng: locale }),
value: `${totalCachedUsers}`,
inline: true,
},
{
// TODO: Localize
name: t("COMMAND_ABOUT_UPTIME", { lng: locale }),
value: `${Math.floor(process.uptime())} seconds`,
inline: true,
},
{
name: t("COMMAND_ABOUT_HIBIKI_VERSION", { lng: locale }),
value: env.npm_package_version,
inline: true,
},
{
name: t("COMMAND_ABOUT_DISCORDJS_VERSION", { lng: locale }),
value: version,
inline: true,
},
{
name: t("COMMAND_ABOUT_BUN_VERSION", { lng: locale }),
value: Bun.version,
inline: true,
},
],
thumbnail: {
url: this.bot.user?.displayAvatarURL() ?? "",
},
},
],
});
}
}
31 changes: 31 additions & 0 deletions packages/bot/src/commands/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { ChatInputCommandInteraction } from "discord.js";
import { HibikiCommand } from "$classes/Command.ts";
import { HibikiColors } from "$shared/constants.ts";
import { t } from "$shared/i18n.ts";

export class HibikiPingCommand extends HibikiCommand {
public async runWithInteraction(interaction: ChatInputCommandInteraction, locale: string) {
const initialInteraction = await interaction.followUp({
fetchReply: true,
embeds: [
{
title: t("COMMAND_PING_PINGING", { lng: locale }),
color: HibikiColors.GENERAL,
},
],
});

await interaction.editReply({
embeds: [
{
title: t("COMMAND_PING_PONG", { lng: locale }),
description: t("COMMAND_PING_LATENCY", {
lng: locale,
latency: initialInteraction.createdTimestamp - interaction.createdTimestamp,
}),
color: HibikiColors.GENERAL,
},
],
});
}
}
14 changes: 0 additions & 14 deletions packages/bot/src/commands/test.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/bot/src/commands/testing.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/bot/src/events/Interaction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { CommandInteraction } from "discord.js";
import { HibikiEvent } from "$classes/Event.ts";
import { getUserConfig } from "$db/index.ts";
import env from "$shared/env.ts";
import logger from "$shared/logger.ts";

export class HibikiInteractionEvent extends HibikiEvent {
locale?: string;
events: HibikiEventListener[] = ["interactionCreate"];

public async run(_event: HibikiEventListener[], interaction: CommandInteraction) {
Expand All @@ -13,6 +16,11 @@ export class HibikiInteractionEvent extends HibikiEvent {
const command = this.bot.commands.get(interaction.commandName);
if (!command) return;

// Gets the user's locale and appends it to the class
// TODO: Caching
const userConfig = await getUserConfig(interaction.user.id);
this.locale = userConfig?.locale ?? env.DEFAULT_LOCALE;

// Logs when an interaction is ran
// TODO: Implement arguments
logger.info(
Expand Down
23 changes: 18 additions & 5 deletions packages/bot/src/hibiki.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { HibikiClient } from "./classes/Client.ts";
import { GatewayIntentBits } from "discord.js";
import { GatewayIntentBits, Options } from "discord.js";

export const subscribedIntents = [
const subscribedIntents = [
// Required for guild, channel, and role objects
GatewayIntentBits.Guilds,

// Required for incoming messages
GatewayIntentBits.GuildMessages,

// PRIVILEGED: Required for getting guild member data
GatewayIntentBits.GuildMembers,
] satisfies GatewayIntentBits[];

// Creates a new Hibiki client
new HibikiClient({
// Cache sweeping options
sweepers: {
...Options.DefaultSweeperSettings,
},

// Cache options
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
ReactionManager: 0,
GuildMemberManager: {
maxSize: 200,
keepOverLimit: (member) => member.id === member.client.user.id,
},
}),

// Intents
intents: subscribedIntents,
}).init();
2 changes: 1 addition & 1 deletion packages/bot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["./src/**/*"],
"include": ["./src/**/*", "../shared/src/typings/**/*"],
"extends": ["../../tsconfig.json"],

"compilerOptions": {
Expand Down
58 changes: 0 additions & 58 deletions packages/db/.prismalintrc.json

This file was deleted.

17 changes: 17 additions & 0 deletions packages/db/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import env from "$shared/env.ts";
import { defineConfig, type Config } from "drizzle-kit";

export default defineConfig({
out: "./drizzle",
schema: "./src/schema/*",
driver: "pg",
dbCredentials: {
host: env.POSTGRES_HOST,
port: env.POSTGRES_PORT,
user: env.POSTGRES_USER,
password: env.POSTGRES_PASSWORD,
database: env.POSTGRES_DB,
},
verbose: true,
strict: true,
}) satisfies Config;
Loading

0 comments on commit 8db1462

Please sign in to comment.