codinglab-io / discord-bot

Discord bot for Codinglab's server.
https://discord.com/invite/codinglab-174169014568878080
MIT License
19 stars 14 forks source link

Modules abstraction is leaking #51

Open neolectron opened 1 year ago

neolectron commented 1 year ago

Problem

I've notices that even after the new module abstraction we still have to wire some of the stuff outside of the module.

to name a few :

Solution

To fix the leaky abstraction, we should allow users to provide this configuration within the scope of their module.

I'm keen into taking a shot at this next time I work on the bot.

neolectron commented 1 year ago

See #57 .

I think we will need sooner that I thought the module-creator. Many benefits, one of them is you don't have to import the type BotModule anymore, and it will be far easier to pass something to a feature (especially if the thing passed to the feature depends on the module definition).

For now, I imagine something like :

export const myModule = createModule((ctx) => ({
  env: {
    MY_ENV_VAR: z.string().optionnal()
  },
  storage: {
    channelList: z.string().array()
  },
  slashCommands: {/*...*/},
  eventHandlers: {
    messageCreate: async () => {
        // Here I would be able to use ctx.env and ctx.storage
        console.log(ctx.storage.channelList) // typesafe + can be validated
    }
  },
}));