Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StoreDriverをClient外部から注入するようにした #409

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/features/config/internal/getConfig.func.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ describe('getConfig', () => {
process.env['DISCORD_APP_TOKEN'] = 'test_token';
process.env['BOT_NAME'] = 'test_bot';
process.env['SET_COMMANDS_TARGET_SERVERS'] = 'server1,server2';
process.env['STORE_DRIVER'] = 'local';

const expectedConfig = {
DISCORD_APP_TOKEN: 'test_token',
BOT_NAME: 'test_bot',
SET_COMMANDS_TARGET_SERVERS: ['server1', 'server2'],
STORE_DRIVER: 'local',
};

expect(getConfig()).toEqual(expectedConfig);
Expand Down
9 changes: 9 additions & 0 deletions src/features/config/internal/getConfig.func.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { createEnv } from '@t3-oss/env-core';
import { z } from 'zod';

import { STORE_TYPES } from '@/features/core/index.js';

const storeDriverKeys: ReadonlyArray<string> = Object.values(STORE_TYPES);

export const getConfig = () => {
const envs = createEnv({
server: {
DISCORD_APP_TOKEN: z.string().min(1).readonly(),
BOT_NAME: z.string().min(1).readonly(),
SET_COMMANDS_TARGET_SERVERS: z.string().min(1).readonly(),
STORE_DRIVER: z
.string()
.min(1)
.refine(value => storeDriverKeys.includes(value))
.readonly(),
} as const,
runtimeEnv: process.env,
});
Expand Down
17 changes: 3 additions & 14 deletions src/features/core/internal/Client/internal/Client.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { exit } from 'process';

import type { CommandBase } from '@/features/commands/index.js';
import type { getConfig } from '@/features/config/index.js';
import {
getInMemoryStoreInstance,
type Store,
STORE_TYPES,
} from '@/features/core/index.js';
import { type Store } from '@/features/core/index.js';
import type { ChatInputCommandInteraction } from '@/features/library/index.js';
import {
chalk,
Expand All @@ -23,7 +19,7 @@ export class Client extends DiscordJsClient {
private interactionCommands: ReadonlyArray<CommandBase> = [];
#store: Store;

constructor({ config, commands, storeDriver }: ClassConstructorArgs) {
constructor({ config, commands, StoreDriver }: ClassConstructorArgs) {
super({
intents: [
GatewayIntentBits.Guilds,
Expand All @@ -33,21 +29,14 @@ export class Client extends DiscordJsClient {
],
});
this.#config = config;
this.#store = StoreDriver;
try {
this.login(this.#config.DISCORD_APP_TOKEN);
} catch (error) {
this.log(chalk.red('Failed to fetch the account'));
exit(1);
}

switch (storeDriver) {
case STORE_TYPES.IN_MEMORY:
this.#store = getInMemoryStoreInstance();
break;
default:
throw new Error('Invalid store driver');
}

const commandsRegisteredResult = this.#installModules(commands);

this.on('ready', client => {
Expand Down
4 changes: 2 additions & 2 deletions src/features/core/internal/Client/internal/Client.types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { CommandBase } from '@/features/commands/index.js';
import type { getConfig } from '@/features/config/index.js';
import type { Store, STORE_TYPES } from '@/features/core/index.js';
import type { Store } from '@/features/core/index.js';

export type ClassConstructorArgs = {
config: ReturnType<typeof getConfig>;
commands: ReadonlyArray<new ({ store }: { store: Store }) => CommandBase>;
storeDriver: keyof typeof STORE_TYPES;
StoreDriver: Store;
};
3 changes: 2 additions & 1 deletion src/features/core/internal/Store/internal/Store.constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const STORE_TYPES = {
IN_MEMORY: 'IN_MEMORY',
IN_MEMORY: 'local',
REDIS: 'redis',
} as const satisfies Record<string, string>;
22 changes: 19 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ import 'dotenv/config';

import { Ping, VoiceChannel, YouTube } from './features/commands/index.js';
import { getConfig } from './features/config/index.js';
import { Client, STORE_TYPES } from './features/core/index.js';
import {
Client,
getInMemoryStoreInstance,
STORE_TYPES,
} from './features/core/index.js';

const config = getConfig();
const storeDriver = (() => {
switch (config.STORE_DRIVER) {
case STORE_TYPES.IN_MEMORY:
return getInMemoryStoreInstance();
case STORE_TYPES.REDIS:
throw new Error('Not implemented yet');
default:
return getInMemoryStoreInstance();
}
})();

new Client({
config: getConfig(),
config,
commands: [Ping, VoiceChannel, YouTube],
storeDriver: STORE_TYPES.IN_MEMORY,
StoreDriver: storeDriver,
});