Skip to content

Understanding `declare module`

Due to the versatility of seyfert, it can be confusing to understand exactly how to include content in the project, so seyfert includes different interfaces for the developer to use when defining their own rules.

Clients

Seyfert includes several types of client to create the instance of a bot, this can create confusion when calling the client anywhere in your code, so you can specify to typescript which one you are using:

1
declare module 'seyfert' {
2
interface UsingClient extends ParseClient<Client<true>> { }
3
// interface UsingClient extends ParseClient<WorkerClient<true>> { }
4
// interface UsingClient extends ParseClient<HttpClient> { }
5
}

Middlewares

In order for you to be able to use your middlewares everywhere, it is again necessary to tell typescript what they are and how they are made up. Export all the middlewares in your code and pass them to seyfert:

1
import type * as allMiddlewares from './middlewares'
2
3
declare module 'seyfert' {
4
interface RegisteredMiddlewares extends ParseMiddlewares<typeof allMiddlewares> { }
5
}

This is the same as above, but applies to global middlewares, those that are executed in every command even without specifying them.

1
import type * as allMiddlewares from './middlewares/globals'
2
3
declare module 'seyfert' {
4
interface GlobalMetadata extends ParseMiddlewares<typeof globalMiddlewares> { }
5
}

Languages

In the Seyfert language system, the default language is prioritized, including the typing provided by typescript.

It is highly recommended to use .ts, .js files for your translations, so you can include functions inside them and autocomplete!

If you use json for this (not recommended), you must add ../ to the path in your seyfert.config.js as typescript ignores json files that are not imported manually.

1
import type * as defaultLang from './langs/en'
2
3
declare module 'seyfert' {
4
interface DefaultLocale extends ParseLocales<typeof defaultLang> { }
5
}

Extends Context

As you may notice when you are extending the CommandContext, the properties you want to be extended or added in are not typed. To do that we have to declare Seyfert’s module.

We have to edit the ExtendContext interface within Seyfert module which expects to receive the data to add.

1
declare module 'seyfert' {
2
//we are using ReturnType which gives us the typeof the whatever the function context returns.
3
interface ExtendContext extends ReturnType<typeof context>;
4
}

Internal Options

Since seyfert accepts different ways of operating, it becomes more complicated to keep the types true to reality. Because of that there are InternalOptions, an interface that expects properties to transform the seyfert types to something more complete.

1
declare module 'seyfert' {
2
interface InternalOptions {
3
withPrefix: true | false;
4
asyncCache: true | false;
5
}
6
}

withPrefix

Setting this property to true tells seyfert that the context can have either message or interaction and both will be optional, by default .interaction is always part of the context.

asyncCache

Setting this property to true tells seyfert whether the cache will return a promise or not, by default seyfert uses MemoryAdapter a RAM cache which does not return a promise, but RedisAdapter does.