eris/bot/session.ts

83 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-09-10 18:56:17 +00:00
import { Grammy, GrammyKvStorage } from "../deps.ts";
import { SdApi, SdTxt2ImgRequest } from "../sd.ts";
2023-09-06 00:53:34 +00:00
2023-09-10 18:56:17 +00:00
export type SessionFlavor = Grammy.SessionFlavor<SessionData>;
2023-09-06 00:53:34 +00:00
export interface SessionData {
global: GlobalData;
chat: ChatData;
user: UserData;
}
export interface GlobalData {
adminUsernames: string[];
pausedReason: string | null;
maxUserJobs: number;
maxJobs: number;
defaultParams?: Partial<SdTxt2ImgRequest>;
2023-09-10 18:56:17 +00:00
workers: WorkerData[];
}
export interface WorkerData {
name: string;
api: SdApi;
auth?: string;
maxResolution: number;
2023-09-06 00:53:34 +00:00
}
export interface ChatData {
2023-09-10 18:56:17 +00:00
language?: string;
2023-09-06 00:53:34 +00:00
}
export interface UserData {
2023-09-10 18:56:17 +00:00
params?: Partial<SdTxt2ImgRequest>;
2023-09-06 00:53:34 +00:00
}
const globalDb = await Deno.openKv("./app.db");
2023-09-10 18:56:17 +00:00
const globalDbAdapter = new GrammyKvStorage.DenoKVAdapter<GlobalData>(globalDb);
2023-09-06 00:53:34 +00:00
const getDefaultGlobalData = (): GlobalData => ({
2023-09-10 18:56:17 +00:00
adminUsernames: Deno.env.get("TG_ADMIN_USERS")?.split(",") ?? [],
2023-09-06 00:53:34 +00:00
pausedReason: null,
maxUserJobs: 3,
maxJobs: 20,
defaultParams: {
batch_size: 1,
n_iter: 1,
2023-09-10 18:56:17 +00:00
width: 512,
height: 768,
steps: 30,
cfg_scale: 10,
2023-09-06 00:53:34 +00:00
negative_prompt: "boring_e621_fluffyrock_v4 boring_e621_v4",
},
2023-09-10 18:56:17 +00:00
workers: [
{
name: "local",
api: { url: Deno.env.get("SD_API_URL") ?? "http://127.0.0.1:7860/" },
maxResolution: 1024 * 1024,
},
],
2023-09-06 00:53:34 +00:00
});
2023-09-10 18:56:17 +00:00
export const session = Grammy.session<SessionData, Grammy.Context & SessionFlavor>({
2023-09-06 00:53:34 +00:00
type: "multi",
global: {
getSessionKey: () => "global",
initial: getDefaultGlobalData,
storage: globalDbAdapter,
},
chat: {
2023-09-10 18:56:17 +00:00
initial: () => ({}),
2023-09-06 00:53:34 +00:00
},
user: {
getSessionKey: (ctx) => ctx.from?.id.toFixed(),
2023-09-10 18:56:17 +00:00
initial: () => ({}),
2023-09-06 00:53:34 +00:00
},
});
export async function getGlobalSession(): Promise<GlobalData> {
const data = await globalDbAdapter.read("global");
return data ?? getDefaultGlobalData();
}