Skip to content
GitHub

ThemeResolver

interface in meocord/interface Since 4.1.0

interface ThemeResolver

A class, decorated with @Service(), that looks themes up with the app's services, as @MeoCord({ themeFor }) takes it in place of ThemeResolvers.

Its methods are the resolvers: guild() for a server's theme and user() for a user's, each optional. The class is resolved from the app's container, so its constructor injects the app's services and providers, and it runs OnReady and OnShutdown as a service does. Without a decorator, TypeScript records none of its constructor's types, and a constructor that injects is refused.

Examples

TypeScript
@Service()
export class PrefsService {
  private readonly choices = new Map<string, ThemeOverride>()

  constructor(private readonly themes: ThemeCache) {}

  async themeOf(userId: string): Promise<ThemeOverride | undefined> {
    return this.choices.get(userId)
  }

  async choose(userId: string, theme: ThemeOverride) {
    this.choices.set(userId, theme)
    // The user's next call looks their theme up again
    this.themes.invalidateUser(userId)
  }
}

@Service()
export class UserThemes implements ThemeResolver {
  constructor(private readonly prefs: PrefsService) {}

  user({ user }: UserThemeTarget) {
    return this.prefs.themeOf(user.id)
  }
}

@MeoCord({ controllers: [], clientOptions: { intents: [] }, themeFor: UserThemes })
export class App {}

Members

guild

guild?(
  target: GuildThemeTarget,
):
  ThemeOverride | null | undefined | Promise<ThemeOverride | null | undefined>

The theme for calls from a server; not asked for a call from a DM.

Parameters

NameTypeDescription
targetGuildThemeTarget

Returns

ThemeOverride | null | undefined | Promise<ThemeOverride | null | undefined>

user

user?(
  target: UserThemeTarget,
):
  ThemeOverride | null | undefined | Promise<ThemeOverride | null | undefined>

The theme for calls from a user, in a server or a DM.

Parameters

NameTypeDescription
targetUserThemeTarget

Returns

ThemeOverride | null | undefined | Promise<ThemeOverride | null | undefined>