namespace presence types again

This commit is contained in:
Jacob Nguyen
2024-07-03 22:25:24 -05:00
parent e0f631a8e2
commit 92ca9eb859
3 changed files with 26 additions and 22 deletions

View File

@@ -3,16 +3,9 @@ import type { IntoDependencies } from "./ioc";
import type { Emitter } from "./interfaces";
type Status = 'online' | 'idle' | 'invisible' | 'dnd'
type PresenceReduce = (previous: PresenceResult) => PresenceResult;
type PresenceReduce = (previous: Presence.Result) => Presence.Result;
export interface PresenceResult {
status?: Status;
afk?: boolean;
activities?: ActivitiesOptions[];
shardId?: number[];
repeat?: number | [Emitter, string];
onRepeat?: (previous: PresenceResult) => PresenceResult;
}
export const Presence = {
/**
@@ -20,13 +13,13 @@ export const Presence = {
* Create a Presence module which **MUST** be put in a file called presence.(language-extension)
* adjacent to the file where **Sern.init** is CALLED.
*/
module : <T extends (keyof Dependencies)[]>(conf: PresenceConfig<T>) => conf,
module : <T extends (keyof Dependencies)[]>(conf: Presence.Config<T>) => conf,
/**
* Create a Presence body which can be either:
* - once, the presence is activated only once.
* - repeated, per cycle or event, the presence can be changed.
*/
of : (root: Omit<PresenceResult, 'repeat' | 'onRepeat'>) => {
of : (root: Omit<Presence.Result, 'repeat' | 'onRepeat'>) => {
return {
/**
* @example
@@ -56,9 +49,20 @@ export const Presence = {
};
}
}
export declare namespace Presence {
type Config<T extends (keyof Dependencies)[]> = {
inject?: [...T]
execute: (...v: IntoDependencies<T>) => Presence.Result;
}
export interface Result {
status?: Status;
afk?: boolean;
activities?: ActivitiesOptions[];
shardId?: number[];
repeat?: number | [Emitter, string];
onRepeat?: (previous: Result) => Result;
}
}
export type PresenceConfig <T extends (keyof Dependencies)[]> = {
inject?: [...T]
execute: (...v: IntoDependencies<T>) => PresenceResult;
};

View File

@@ -1,11 +1,11 @@
import { concatMap, from, interval, of, map, scan, startWith, fromEvent, take } from "rxjs"
import { PresenceConfig, PresenceResult } from "../core/presences";
import { Presence } from "../core/presences";
import { Services } from "../core/ioc";
import assert from "node:assert";
import * as Files from "../core/module-loading";
type SetPresence = (conf: PresenceResult) => Promise<unknown>
type SetPresence = (conf: Presence.Result) => Promise<unknown>
const parseConfig = async (conf: Promise<PresenceResult>) => {
const parseConfig = async (conf: Promise<Presence.Result>) => {
return conf.then(s => {
if('repeat' in s) {
const { onRepeat, repeat } = s;
@@ -23,7 +23,7 @@ const parseConfig = async (conf: Promise<PresenceResult>) => {
export const presenceHandler = (path: string, setPresence: SetPresence) => {
const presence = Files
.importModule<PresenceConfig<(keyof Dependencies)[]>>(path)
.importModule<Presence.Config<(keyof Dependencies)[]>>(path)
.then(({ module }) => {
//fetch services with the order preserved, passing it to the execute fn
const fetchedServices = Services(...module.inject ?? []);

View File

@@ -11,7 +11,7 @@ import interactionHandler from './handlers/interaction';
import { presenceHandler } from './handlers/presence';
import { handleCrash } from './handlers/event-utils';
import { UnpackedDependencies } from './types/utility';
import type { PresenceResult } from './core/presences';
import type { Presence} from './core/presences';
interface Wrapper {
commands: string;
@@ -52,7 +52,7 @@ export function init(maybeWrapper: Wrapper = { commands: "./dist/commands" }) {
const time = ((performance.now() - startTime) / 1000).toFixed(2);
deps['@sern/logger']?.info({ message: `sern: registered in ${time} s` });
if(presencePath.exists) {
const setPresence = async (p: PresenceResult) => {
const setPresence = async (p: Presence.Result) => {
return deps['@sern/client'].user?.setPresence(p);
}
presenceHandler(presencePath.path, setPresence).subscribe();