-
Notifications
You must be signed in to change notification settings - Fork 13
feat: make modules isolated using creation function #81
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
08a4bed
temp
potb 4dc53e2
Merge branch 'master' into feat/isolated_modules
potb 0bfd13d
use createModule when possible
potb 2c47a97
update env and naming
potb b8892e6
Merge branch 'master' into feat/isolated_modules
potb b665169
remove log
potb d44a43d
Merge branch 'master' into feat/isolated_modules
potb f2cb0ba
prettier
potb 4c37640
Merge branch 'master' into feat/isolated_modules
potb 227abf9
remove unused type
potb 2d747a5
move ready call at the beginning of loadModules
potb 02ce375
remove env var and move config to core
potb 9e89cb2
refactor out function
potb 2a08d4e
prettier
potb 0830f37
add token
potb 07c7150
Merge branch 'master' into feat/isolated_modules
potb fa7bcf4
Merge branch 'master' into feat/isolated_modules
potb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { constantCase } from 'constant-case'; | ||
|
||
import type { CreatedModule, ModuleFactory } from './createModule'; | ||
|
||
const createEnvForModule = (constantName: string) => | ||
Object.entries(process.env) | ||
.filter(([key]) => key.startsWith(constantName)) | ||
.reduce<Record<string, string>>((acc, [key, value]) => { | ||
const envKey = key.replace(`${constantName}_`, ''); | ||
|
||
if (value === undefined) { | ||
return acc; | ||
} | ||
|
||
acc[envKey] = value; | ||
|
||
return acc; | ||
}, {}); | ||
|
||
export const createAllModules = async ( | ||
modules: Record<string, ModuleFactory>, | ||
): Promise<CreatedModule[]> => { | ||
const createdModules: CreatedModule[] = []; | ||
|
||
for (const [name, factory] of Object.entries(modules)) { | ||
const moduleConstantName = constantCase(name); | ||
const moduleEnv = createEnvForModule(moduleConstantName); | ||
const module = await factory({ env: moduleEnv }); | ||
|
||
createdModules.push(module); | ||
} | ||
|
||
return createdModules; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { ClientEvents, ClientOptions } from 'discord.js'; | ||
import type { ZodTypeAny } from 'zod'; | ||
import { z } from 'zod'; | ||
|
||
import type { BotCommand, EventHandler } from '../types/bot'; | ||
|
||
type InferredZodShape<Shape extends Record<string, ZodTypeAny>> = { | ||
[K in keyof Shape]: Shape[K]['_type']; | ||
}; | ||
|
||
interface Context<Env extends Record<string, ZodTypeAny>> { | ||
env: InferredZodShape<Env>; | ||
} | ||
|
||
type ModuleFunction<Env extends Record<string, ZodTypeAny>, ReturnType> = ( | ||
context: Context<Env>, | ||
) => ReturnType; | ||
|
||
type EventHandlers = { | ||
[K in keyof ClientEvents]?: EventHandler<K>; | ||
}; | ||
|
||
type BotModule<Env extends Record<string, ZodTypeAny>> = { | ||
env?: Env; | ||
neolectron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
intents?: ClientOptions['intents']; | ||
slashCommands?: ModuleFunction<Env, Array<BotCommand>>; | ||
eventHandlers?: ModuleFunction<Env, EventHandlers>; | ||
}; | ||
|
||
interface CreatedModuleInput { | ||
env: unknown; | ||
} | ||
|
||
export interface CreatedModule { | ||
intents: ClientOptions['intents']; | ||
slashCommands: Array<BotCommand>; | ||
eventHandlers: EventHandlers; | ||
} | ||
|
||
export type ModuleFactory = (input: CreatedModuleInput) => Promise<CreatedModule>; | ||
|
||
export const createModule = <Env extends Record<string, ZodTypeAny>>( | ||
module: BotModule<Env>, | ||
): ModuleFactory => { | ||
return async (input) => { | ||
const env = await z.object(module.env ?? ({} as Env)).parseAsync(input.env); | ||
|
||
const context = { | ||
env, | ||
}; | ||
|
||
return { | ||
intents: module.intents ?? [], | ||
slashCommands: module.slashCommands?.(context) ?? [], | ||
eventHandlers: module.eventHandlers?.(context) ?? {}, | ||
}; | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { z } from 'zod'; | ||
|
||
const envShape = z | ||
.object({ | ||
DISCORD_TOKEN: z.string().nonempty(), | ||
REDIS_URL: z.string().url(), | ||
}) | ||
.transform((object) => ({ | ||
discordToken: object.DISCORD_TOKEN, | ||
redisUrl: object.REDIS_URL, | ||
})); | ||
|
||
export const env = envShape.parse(process.env); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { BotModule } from '../types/bot'; | ||
import type { CreatedModule } from './createModule'; | ||
|
||
export const getIntentsFromModules = (modules: Record<string, BotModule>) => { | ||
const intents = Object.values(modules).flatMap((module) => module.intents ?? []); | ||
export const getIntentsFromModules = (modules: CreatedModule[]) => { | ||
const intents = modules.flatMap((module) => module.intents ?? []); | ||
return [...new Set(intents)] as const; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.