Skip to content

Commit

Permalink
Updated packages
Browse files Browse the repository at this point in the history
  • Loading branch information
raphdcst committed Jun 26, 2024
1 parent 121a952 commit 69a85b5
Show file tree
Hide file tree
Showing 29 changed files with 441 additions and 151 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
"error",
{
"ignore": [
"^#app$"
"^#app$",
"^#config$",
"^#env$",
"^#client$",
"^#logger$"
]
}
],
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/dist
/temp
/data
/src/archives

# node

Expand Down
25 changes: 14 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"imports": {
"#app": "./dist/app.js",
"#app/*": "./dist/*",
"#tables/*": "./dist/tables/*"
"#tables/*": "./dist/tables/*",
"#env": "./dist/app/env.js",
"#config": "./dist/config.js",
"#client": "./dist/app/client.js",
"#logger": "./dist/app/logger.js"
},
"scripts": {
"build": "gulp build",
Expand Down Expand Up @@ -38,7 +42,7 @@
"dependencies": {
"@discordjs/rest": "^0.5.0",
"@ghom/handler": "^3.1.0",
"@ghom/logger": "^1.2.0",
"@ghom/logger": "^2.0.1",
"@ghom/orm": "^1.7.0",
"boxen": "^7.0.0",
"chalk": "^5.0.1",
Expand All @@ -58,7 +62,8 @@
"simple-git": "^3.24.0",
"tims": "^2.1.0",
"types-package-json": "^2.0.39",
"yargs-parser": "^21.0.1"
"yargs-parser": "^21.0.1",
"zod": "^3.23.8"
},
"devDependencies": {
"@esbuild/linux-x64": "^0.20.1",
Expand Down
6 changes: 4 additions & 2 deletions src/app.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export * from "./app/database.ts"
export * from "./app/listener.ts"
export * from "./app/command.ts"
export * from "./app/config.ts"
export * from "./app/client.ts"
export * from "./app/logger.ts"
export * from "./app/slash.ts"
export * from "./app/util.ts"
Expand All @@ -15,6 +14,9 @@ export * as argument from "./app/argument.ts"
export * as database from "./app/database.ts"
export * as listener from "./app/listener.ts"
export * as command from "./app/command.ts"
export * as logger from "./app/logger.ts"
export * as slash from "./app/slash.ts"
export * as util from "./app/util.ts"

export { default as env } from "./app/env.ts"
export { default as client } from "./app/client.ts"
export { default as logger } from "./app/logger.ts"
2 changes: 1 addition & 1 deletion src/app/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export async function resolveType(
case "message":
if (baseValue) {
const match =
/^https?:\/\/discord\.com\/channels\/\d+\/(\d+)\/(\d+)$/.exec(
/^https?:\/\/(?:www\.)?(?:ptb\.|canary\.)?(?:discord|discordapp)\.com\/channels\/\d+\/(\d+)\/(\d+)$/.exec(
baseValue,
)
if (match) {
Expand Down
15 changes: 3 additions & 12 deletions src/app/client.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// system file, please don't modify it

import discord from "discord.js"
import * as config from "./config.ts"
import config from "#config"

Check failure on line 4 in src/app/client.ts

View workflow job for this annotation

GitHub Actions / test

Module '"/home/runner/work/lg-bot-v1/lg-bot-v1/src/config"' has no default export. Did you mean to use 'import { config } from "/home/runner/work/lg-bot-v1/lg-bot-v1/src/config"' instead?

export class ClientSingleton {
private static instance: discord.Client
const client = new discord.Client(config.client)

private constructor() {}

static get(): discord.Client {
if (!ClientSingleton.instance) {
ClientSingleton.instance = new discord.Client(config.getConfig().client)
}
return ClientSingleton.instance
}
}
export default client
11 changes: 6 additions & 5 deletions src/app/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import * as handler from "@ghom/handler"
import * as util from "./util.ts"
import * as logger from "./logger.ts"
import * as argument from "./argument.ts"
import * as config from "./config.ts"

import { filename } from "dirname-filename-esm"
import env from "#env"
import config from "#config"

Check failure on line 17 in src/app/command.ts

View workflow job for this annotation

GitHub Actions / test

Module '"/home/runner/work/lg-bot-v1/lg-bot-v1/src/config"' has no default export. Did you mean to use 'import { config } from "/home/runner/work/lg-bot-v1/lg-bot-v1/src/config"' instead?

import { filename } from "dirname-filename-esm"
const __filename = filename(import.meta)

export const commandHandler = new handler.Handler(
Expand Down Expand Up @@ -476,7 +477,7 @@ export async function prepareCommand(
if (util.scrap(cmd.options.guildOwnerOnly, message))
if (
message.guild.ownerId !== message.member.id &&
process.env.BOT_OWNER !== message.member.id
env.BOT_OWNER !== message.member.id
)
return util.getSystemMessage("error", {
author: {
Expand Down Expand Up @@ -569,7 +570,7 @@ export async function prepareCommand(
})

if (await util.scrap(cmd.options.botOwnerOnly, message))
if (process.env.BOT_OWNER !== message.author.id)
if (env.BOT_OWNER !== message.author.id)
return util.getSystemMessage("error", {
author: {
name: "You must be my owner.",
Expand Down Expand Up @@ -840,7 +841,7 @@ export async function sendCommandDetails(
message: IMessage,
cmd: ICommand,
): Promise<void> {
const { detailCommand, openSource } = config.getConfig()
const { detailCommand, openSource } = config

if (detailCommand) {
const options = await detailCommand(message, cmd)
Expand Down
24 changes: 6 additions & 18 deletions src/app/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import type * as discord from "discord.js"

import type * as pagination from "./pagination.ts"
import type * as command from "./command.ts"
import type * as logger from "./logger.ts"
import type * as slash from "./slash.ts"

import * as util from "./util.ts"

import { config } from "../config.ts"
import type * as util from "./util.ts"

export interface Config {
/**
Expand Down Expand Up @@ -67,19 +65,9 @@ export interface Config {
* Custom messages for the system
*/
systemMessages?: Partial<util.SystemMessages>
}

const finalConfig: {
data: Config | null
} = {
data: null,
}

export function getConfig() {
if (!finalConfig.data) throw new Error("Config not initialized")
return finalConfig.data
}

export async function initConfig() {
finalConfig.data = await util.scrap(config)
/**
* Custom options for the system logger
*/
logger?: logger.LoggerOptions
}
15 changes: 8 additions & 7 deletions src/app/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// system file, please don't modify it

import { ORM } from "@ghom/orm"
import { logger } from "@ghom/logger"
import * as logger from "./logger.ts"
import env from "./env.ts"
import path from "path"

export const orm = new ORM({
Expand All @@ -10,12 +11,12 @@ export const orm = new ORM({
client: "pg",
useNullAsDefault: true,
connection: {
port: +(process.env.DB_PORT ?? 5432),
host: process.env.DB_HOST ?? "localhost",
user: process.env.DB_USER ?? "postgres",
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE ?? "postgres",
timezone: process.env.BOT_TIMEZONE || "UTC",
port: env.DB_PORT ?? 5432,
host: env.DB_HOST ?? "localhost",
user: env.DB_USER ?? "postgres",
password: env.DB_PASSWORD,
database: env.DB_DATABASE ?? "postgres",
timezone: env.BOT_TIMEZONE || "UTC",
},
},
logger,
Expand Down
87 changes: 87 additions & 0 deletions src/app/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import "dotenv/config"

import { z, ZodError } from "zod"
import fs from "fs"
import path from "path"
import chalk from "chalk"

import * as logger from "./logger.ts"

const localeList: { key: string; name: string }[] = JSON.parse(
fs.readFileSync(
path.join(process.cwd(), "node_modules", "dayjs", "locale.json"),
"utf-8",
),
)

const localeKeys = localeList.map((locale) => locale.key) as [
string,
...string[],
]

const timezones = Intl.supportedValuesOf("timeZone") as [string, ...string[]]

const envSchema = z.object({
BOT_TOKEN: z.string({
message: `You need to add your ${chalk.bold("BOT_TOKEN")} in the .env file. You can found this token on this page in the "Bot" tab: ${
process.env.BOT_ID
? `https://discord.com/developers/applications/${process.env.BOT_ID}/information`
: "https://discord.com/developers/applications"
}`,
}),
BOT_PREFIX: z.string({
message: `You need to add a ${chalk.bold("BOT_PREFIX")} in the .env file, for example: BOT_PREFIX="!"`,
}),
BOT_OWNER: z.string({
message: `You need to add your ${chalk.bold("BOT_OWNER")} Discord ID in the .env file, for example: BOT_OWNER="123456789012345678"`,
}),
BOT_ID: z.string({
message: `You need to add the ${chalk.bold("BOT_ID")} in the .env file. You can found this ID on this page in the "General Information" tab: ${
process.env.BOT_ID
? `https://discord.com/developers/applications/${process.env.BOT_ID}/information`
: "https://discord.com/developers/applications"
}`,
}),
BOT_MODE: z.enum(["factory", "test", "development", "production"], {
message: `You need to add a ${chalk.bold("BOT_MODE")} in the .env file, for example: BOT_MODE="development"`,
}),
BOT_LOCALE: z
.enum(localeKeys, {
message: `Your ${chalk.bold("BOT_LOCALE")} in the .env file is not valid. You can choose between this list:\n=> ${localeKeys.join(", ")}`,
})
.optional(),
BOT_TIMEZONE: z
.enum(timezones, {
message: `Your ${chalk.bold("BOT_TIMEZONE")} in the .env file is not valid. You can choose between this list:\n=> ${timezones.join(", ")}`,
})
.optional(),
DB_PORT: z.coerce
.number({
message: `The ${chalk.bold("DB_PORT")} must be a valid port number between 0 and 65535`,
})
.max(
65535,
`The ${chalk.bold("DB_PORT")} must be a valid port number between 0 and 65535`,
)
.optional(),
DB_HOST: z.string().optional(),
DB_USER: z.string().optional(),
DB_PASSWORD: z.string().optional(),
DB_DATABASE: z.string().optional(),
})

let env: z.infer<typeof envSchema>

if (process.env.BOT_MODE !== "test") {
try {
env = envSchema.parse(process.env)
} catch (error) {
const { errors } = error as ZodError
errors.forEach((err) => logger.error(err.message, ".env"))
process.exit(1)
}
} else {
env = process.env as unknown as z.infer<typeof envSchema>
}

export default env
Loading

0 comments on commit 69a85b5

Please sign in to comment.