From 6f5f4251663152990d966c58755bbf97f6525dca Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Fri, 26 Apr 2024 07:02:21 +0200 Subject: [PATCH] configDefault: add status options --- src/configDefault.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 1 + 2 files changed, 69 insertions(+) diff --git a/src/configDefault.ts b/src/configDefault.ts index dcd4dd9..130b1a2 100644 --- a/src/configDefault.ts +++ b/src/configDefault.ts @@ -1,3 +1,8 @@ +import { + ActivityType +, type PresenceStatusData +, type PresenceData +} from "discord.js"; import { ChatCompletionMessageParam as OpenAIMessage, ChatCompletionCreateParamsNonStreaming as ChatCompletionRequestData, @@ -13,6 +18,8 @@ export interface IConfigRequired { readonly Discord: string; readonly OpenAI: string; }; + /** Discord bot status */ + readonly status: PresenceData /** Messages to append at the start of every chat history when sending to API */ systemPrompt(context: apiRequest): OpenAIMessage[]; /** OpenAI model config */ @@ -39,6 +46,10 @@ export default function newConfig(config?: IConfig): IConfigRequired { return { ...defaultConfig, ...config }; } +function isEnvDefined(key: string): boolean { + return process.env[key] !== undefined; +} + function envAsString(key: string): string | undefined { key = key.toLocaleUpperCase(); return process.env[key]; @@ -50,6 +61,53 @@ function envAsNumber(key: string): number | undefined { return !Number.isNaN(value) ? value : undefined; } +function envAsBoolean(key: string): boolean | undefined { + key = key.toUpperCase(); + const value = process.env[key]; + return !(value === "false" || value === "0"); +} + +function envAsActivityType(key: string): ActivityType | undefined { + key = key.toUpperCase(); + const value = process.env[key]?.toUpperCase(); + switch (value) { + case "0": + case "PLAYING": + return ActivityType.Playing; + case "1": + case "STREAMING": + return ActivityType.Streaming; + case "2": + case "LISTENING": + return ActivityType.Listening; + case "3": + case "WATCHING": + return ActivityType.Watching; + case "4": + case "CUSTOM": + return ActivityType.Custom; + case "5": + case "COMPETING": + return ActivityType.Competing; + default: + return undefined; + } +} + +function envAsPresenceStatusData(key: string): PresenceStatusData | undefined { + key = key.toUpperCase(); + const value = process.env[key]?.toLowerCase(); + switch (value) { + case "online": + case "idle": + case "dnd": + case "invisible": + return value; + default: + return undefined; + } +} + const defaultConfig: IConfigRequired = { tokens: { Discord: envAsString("TOKENS__DISCORD") ?? "", @@ -65,6 +123,16 @@ const defaultConfig: IConfigRequired = { }, ]; }, + status: { + activities: isEnvDefined("STATUS__NAME") ? [{ + name: envAsString("STATUS__NAME") as string, + type: envAsActivityType("STATUS__TYPE") ?? ActivityType.Custom, + state: envAsString("STATUS__STATE"), + url: envAsString("STATUS__URL"), + }] : undefined, + status: envAsPresenceStatusData("STATUS__STATUS"), + afk: envAsBoolean("STATUS__AFK"), + }, chatCompletionParams: { model: envAsString("CHAT_COMPLETION_PARAMS__MODEL") ?? "gpt-3.5-turbo", max_tokens: envAsNumber("CHAT_COMPLETION_PARAMS__MAX_TOKENS") ?? 384, diff --git a/src/index.ts b/src/index.ts index eb7a9f7..38468b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -55,6 +55,7 @@ interactionManager.bindClient(discord); discord.on("ready", event => { console.log(`Connected to Discord as ${event.user.tag} (${event.user.id})`); + event.user.setPresence(config.status); }); discord.on("messageCreate", message => {