forked from syntaxbullet/AuroraBot-discord
refactor: initial moves
This commit is contained in:
204
shared/lib/config.ts
Normal file
204
shared/lib/config.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
import { readFileSync, existsSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { z } from 'zod';
|
||||
|
||||
const configPath = join(import.meta.dir, '..', '..', 'config', 'config.json');
|
||||
|
||||
export interface GameConfigType {
|
||||
leveling: {
|
||||
base: number;
|
||||
exponent: number;
|
||||
chat: {
|
||||
cooldownMs: number;
|
||||
minXp: number;
|
||||
maxXp: number;
|
||||
}
|
||||
},
|
||||
economy: {
|
||||
daily: {
|
||||
amount: bigint;
|
||||
streakBonus: bigint;
|
||||
weeklyBonus: bigint;
|
||||
cooldownMs: number;
|
||||
},
|
||||
transfers: {
|
||||
allowSelfTransfer: boolean;
|
||||
minAmount: bigint;
|
||||
},
|
||||
exam: {
|
||||
multMin: number;
|
||||
multMax: number;
|
||||
}
|
||||
},
|
||||
inventory: {
|
||||
maxStackSize: bigint;
|
||||
maxSlots: number;
|
||||
},
|
||||
commands: Record<string, boolean>;
|
||||
lootdrop: {
|
||||
activityWindowMs: number;
|
||||
minMessages: number;
|
||||
spawnChance: number;
|
||||
cooldownMs: number;
|
||||
reward: {
|
||||
min: number;
|
||||
max: number;
|
||||
currency: string;
|
||||
}
|
||||
};
|
||||
studentRole: string;
|
||||
visitorRole: string;
|
||||
colorRoles: string[];
|
||||
welcomeChannelId?: string;
|
||||
welcomeMessage?: string;
|
||||
feedbackChannelId?: string;
|
||||
terminal?: {
|
||||
channelId: string;
|
||||
messageId: string;
|
||||
};
|
||||
moderation: {
|
||||
prune: {
|
||||
maxAmount: number;
|
||||
confirmThreshold: number;
|
||||
batchSize: number;
|
||||
batchDelayMs: number;
|
||||
};
|
||||
cases: {
|
||||
dmOnWarn: boolean;
|
||||
logChannelId?: string;
|
||||
autoTimeoutThreshold?: number;
|
||||
};
|
||||
};
|
||||
system: Record<string, any>;
|
||||
}
|
||||
|
||||
// Initial default config state
|
||||
export const config: GameConfigType = {} as GameConfigType;
|
||||
|
||||
const bigIntSchema = z.union([z.string(), z.number(), z.bigint()])
|
||||
.refine((val) => {
|
||||
try {
|
||||
BigInt(val);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}, { message: "Must be a valid integer" })
|
||||
.transform((val) => BigInt(val));
|
||||
|
||||
const configSchema = z.object({
|
||||
leveling: z.object({
|
||||
base: z.number(),
|
||||
exponent: z.number(),
|
||||
chat: z.object({
|
||||
cooldownMs: z.number(),
|
||||
minXp: z.number(),
|
||||
maxXp: z.number(),
|
||||
})
|
||||
}),
|
||||
economy: z.object({
|
||||
daily: z.object({
|
||||
amount: bigIntSchema,
|
||||
streakBonus: bigIntSchema,
|
||||
weeklyBonus: bigIntSchema.default(50n),
|
||||
cooldownMs: z.number(),
|
||||
}),
|
||||
transfers: z.object({
|
||||
allowSelfTransfer: z.boolean(),
|
||||
minAmount: bigIntSchema,
|
||||
}),
|
||||
exam: z.object({
|
||||
multMin: z.number(),
|
||||
multMax: z.number(),
|
||||
})
|
||||
}),
|
||||
inventory: z.object({
|
||||
maxStackSize: bigIntSchema,
|
||||
maxSlots: z.number(),
|
||||
}),
|
||||
commands: z.record(z.string(), z.boolean()),
|
||||
lootdrop: z.object({
|
||||
activityWindowMs: z.number(),
|
||||
minMessages: z.number(),
|
||||
spawnChance: z.number(),
|
||||
cooldownMs: z.number(),
|
||||
reward: z.object({
|
||||
min: z.number(),
|
||||
max: z.number(),
|
||||
currency: z.string(),
|
||||
})
|
||||
|
||||
}),
|
||||
studentRole: z.string(),
|
||||
visitorRole: z.string(),
|
||||
colorRoles: z.array(z.string()).default([]),
|
||||
welcomeChannelId: z.string().optional(),
|
||||
welcomeMessage: z.string().optional(),
|
||||
feedbackChannelId: z.string().optional(),
|
||||
terminal: z.object({
|
||||
channelId: z.string(),
|
||||
messageId: z.string()
|
||||
}).optional(),
|
||||
moderation: z.object({
|
||||
prune: z.object({
|
||||
maxAmount: z.number().default(100),
|
||||
confirmThreshold: z.number().default(50),
|
||||
batchSize: z.number().default(100),
|
||||
batchDelayMs: z.number().default(1000)
|
||||
}),
|
||||
cases: z.object({
|
||||
dmOnWarn: z.boolean().default(true),
|
||||
logChannelId: z.string().optional(),
|
||||
autoTimeoutThreshold: z.number().optional()
|
||||
})
|
||||
}).default({
|
||||
prune: {
|
||||
maxAmount: 100,
|
||||
confirmThreshold: 50,
|
||||
batchSize: 100,
|
||||
batchDelayMs: 1000
|
||||
},
|
||||
cases: {
|
||||
dmOnWarn: true
|
||||
}
|
||||
}),
|
||||
system: z.record(z.string(), z.any()).default({}),
|
||||
});
|
||||
|
||||
export function reloadConfig() {
|
||||
if (!existsSync(configPath)) {
|
||||
throw new Error(`Config file not found at ${configPath}`);
|
||||
}
|
||||
|
||||
const raw = readFileSync(configPath, 'utf-8');
|
||||
const rawConfig = JSON.parse(raw);
|
||||
|
||||
// Update config object in place
|
||||
// We use Object.assign to keep the reference to the exported 'config' object same
|
||||
const validatedConfig = configSchema.parse(rawConfig);
|
||||
Object.assign(config, validatedConfig);
|
||||
|
||||
console.log("🔄 Config reloaded from disk.");
|
||||
}
|
||||
|
||||
// Initial load
|
||||
reloadConfig();
|
||||
|
||||
// Backwards compatibility alias
|
||||
export const GameConfig = config;
|
||||
|
||||
export function saveConfig(newConfig: unknown) {
|
||||
// Validate and transform input
|
||||
const validatedConfig = configSchema.parse(newConfig);
|
||||
|
||||
const replacer = (key: string, value: any) => {
|
||||
if (typeof value === 'bigint') {
|
||||
return value.toString();
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
const jsonString = JSON.stringify(validatedConfig, replacer, 4);
|
||||
writeFileSync(configPath, jsonString, 'utf-8');
|
||||
reloadConfig();
|
||||
}
|
||||
65
shared/lib/constants.ts
Normal file
65
shared/lib/constants.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Global Constants and Enums
|
||||
*/
|
||||
|
||||
export enum TimerType {
|
||||
COOLDOWN = 'COOLDOWN',
|
||||
EFFECT = 'EFFECT',
|
||||
ACCESS = 'ACCESS',
|
||||
EXAM_SYSTEM = 'EXAM_SYSTEM',
|
||||
}
|
||||
|
||||
export enum EffectType {
|
||||
ADD_XP = 'ADD_XP',
|
||||
ADD_BALANCE = 'ADD_BALANCE',
|
||||
REPLY_MESSAGE = 'REPLY_MESSAGE',
|
||||
XP_BOOST = 'XP_BOOST',
|
||||
TEMP_ROLE = 'TEMP_ROLE',
|
||||
COLOR_ROLE = 'COLOR_ROLE',
|
||||
LOOTBOX = 'LOOTBOX',
|
||||
}
|
||||
|
||||
export enum TransactionType {
|
||||
TRANSFER_IN = 'TRANSFER_IN',
|
||||
TRANSFER_OUT = 'TRANSFER_OUT',
|
||||
DAILY_REWARD = 'DAILY_REWARD',
|
||||
ITEM_USE = 'ITEM_USE',
|
||||
LOOTBOX = 'LOOTBOX',
|
||||
EXAM_REWARD = 'EXAM_REWARD',
|
||||
PURCHASE = 'PURCHASE',
|
||||
TRADE_IN = 'TRADE_IN',
|
||||
TRADE_OUT = 'TRADE_OUT',
|
||||
QUEST_REWARD = 'QUEST_REWARD',
|
||||
}
|
||||
|
||||
export enum ItemTransactionType {
|
||||
TRADE_IN = 'TRADE_IN',
|
||||
TRADE_OUT = 'TRADE_OUT',
|
||||
SHOP_BUY = 'SHOP_BUY',
|
||||
DROP = 'DROP',
|
||||
GIVE = 'GIVE',
|
||||
USE = 'USE',
|
||||
}
|
||||
|
||||
export enum ItemType {
|
||||
MATERIAL = 'MATERIAL',
|
||||
CONSUMABLE = 'CONSUMABLE',
|
||||
EQUIPMENT = 'EQUIPMENT',
|
||||
QUEST = 'QUEST',
|
||||
}
|
||||
|
||||
export enum CaseType {
|
||||
WARN = 'warn',
|
||||
TIMEOUT = 'timeout',
|
||||
KICK = 'kick',
|
||||
BAN = 'ban',
|
||||
NOTE = 'note',
|
||||
PRUNE = 'prune',
|
||||
}
|
||||
|
||||
export enum LootType {
|
||||
NOTHING = 'NOTHING',
|
||||
CURRENCY = 'CURRENCY',
|
||||
XP = 'XP',
|
||||
ITEM = 'ITEM',
|
||||
}
|
||||
19
shared/lib/env.ts
Normal file
19
shared/lib/env.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const envSchema = z.object({
|
||||
DISCORD_BOT_TOKEN: z.string().optional(),
|
||||
DISCORD_CLIENT_ID: z.string().optional(),
|
||||
DISCORD_GUILD_ID: z.string().optional(),
|
||||
DATABASE_URL: z.string().min(1, "Database URL is required"),
|
||||
PORT: z.coerce.number().default(3000),
|
||||
HOST: z.string().default("127.0.0.1"),
|
||||
});
|
||||
|
||||
const parsedEnv = envSchema.safeParse(process.env);
|
||||
|
||||
if (!parsedEnv.success) {
|
||||
console.error("❌ Invalid environment variables:", parsedEnv.error.flatten().fieldErrors);
|
||||
throw new Error("Invalid environment variables");
|
||||
}
|
||||
|
||||
export const env = parsedEnv.data;
|
||||
18
shared/lib/errors.ts
Normal file
18
shared/lib/errors.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export class ApplicationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = this.constructor.name;
|
||||
}
|
||||
}
|
||||
|
||||
export class UserError extends ApplicationError {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class SystemError extends ApplicationError {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
43
shared/lib/types.ts
Normal file
43
shared/lib/types.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { AutocompleteInteraction, ChatInputCommandInteraction, ClientEvents, SlashCommandBuilder, SlashCommandOptionsOnlyBuilder, SlashCommandSubcommandsOnlyBuilder } from "discord.js";
|
||||
import { LootType, EffectType } from "./constants";
|
||||
import { DrizzleClient } from "../db/DrizzleClient";
|
||||
|
||||
export interface Command {
|
||||
data: SlashCommandBuilder | SlashCommandOptionsOnlyBuilder | SlashCommandSubcommandsOnlyBuilder;
|
||||
execute: (interaction: ChatInputCommandInteraction) => Promise<void> | void;
|
||||
autocomplete?: (interaction: AutocompleteInteraction) => Promise<void> | void;
|
||||
category?: string;
|
||||
}
|
||||
|
||||
export interface Event<K extends keyof ClientEvents> {
|
||||
name: K;
|
||||
once?: boolean;
|
||||
execute: (...args: ClientEvents[K]) => Promise<void> | void;
|
||||
}
|
||||
|
||||
export type ItemEffect =
|
||||
| { type: EffectType.ADD_XP; amount: number }
|
||||
| { type: EffectType.ADD_BALANCE; amount: number }
|
||||
| { type: EffectType.XP_BOOST; multiplier: number; durationSeconds?: number; durationMinutes?: number; durationHours?: number }
|
||||
| { type: EffectType.TEMP_ROLE; roleId: string; durationSeconds?: number; durationMinutes?: number; durationHours?: number }
|
||||
| { type: EffectType.REPLY_MESSAGE; message: string }
|
||||
| { type: EffectType.COLOR_ROLE; roleId: string }
|
||||
| { type: EffectType.LOOTBOX; pool: LootTableItem[] };
|
||||
|
||||
export interface LootTableItem {
|
||||
type: LootType;
|
||||
weight: number;
|
||||
amount?: number; // For CURRENCY, XP
|
||||
itemId?: number; // For ITEM
|
||||
minAmount?: number; // Optional range for CURRENCY/XP
|
||||
maxAmount?: number; // Optional range for CURRENCY/XP
|
||||
message?: string; // Optional custom message for this outcome
|
||||
}
|
||||
|
||||
export interface ItemUsageData {
|
||||
consume: boolean;
|
||||
effects: ItemEffect[];
|
||||
}
|
||||
|
||||
export type DbClient = typeof DrizzleClient;
|
||||
export type Transaction = Parameters<Parameters<DbClient['transaction']>[0]>[0];
|
||||
11
shared/lib/utils.ts
Normal file
11
shared/lib/utils.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import type { Command } from "./types";
|
||||
|
||||
/**
|
||||
* Type-safe helper to create a command definition.
|
||||
*
|
||||
* @param command The command definition
|
||||
* @returns The command object
|
||||
*/
|
||||
export function createCommand(command: Command): Command {
|
||||
return command;
|
||||
}
|
||||
Reference in New Issue
Block a user