configDefault: add status options

This commit is contained in:
Wroclaw 2024-04-26 07:02:21 +02:00
parent b1e464fe50
commit 6f5f425166
2 changed files with 69 additions and 0 deletions

View file

@ -1,3 +1,8 @@
import {
ActivityType
, type PresenceStatusData
, type PresenceData
} from "discord.js";
import { import {
ChatCompletionMessageParam as OpenAIMessage, ChatCompletionMessageParam as OpenAIMessage,
ChatCompletionCreateParamsNonStreaming as ChatCompletionRequestData, ChatCompletionCreateParamsNonStreaming as ChatCompletionRequestData,
@ -13,6 +18,8 @@ export interface IConfigRequired {
readonly Discord: string; readonly Discord: string;
readonly OpenAI: string; readonly OpenAI: string;
}; };
/** Discord bot status */
readonly status: PresenceData
/** Messages to append at the start of every chat history when sending to API */ /** Messages to append at the start of every chat history when sending to API */
systemPrompt(context: apiRequest): OpenAIMessage[]; systemPrompt(context: apiRequest): OpenAIMessage[];
/** OpenAI model config */ /** OpenAI model config */
@ -39,6 +46,10 @@ export default function newConfig(config?: IConfig): IConfigRequired {
return { ...defaultConfig, ...config }; return { ...defaultConfig, ...config };
} }
function isEnvDefined(key: string): boolean {
return process.env[key] !== undefined;
}
function envAsString(key: string): string | undefined { function envAsString(key: string): string | undefined {
key = key.toLocaleUpperCase(); key = key.toLocaleUpperCase();
return process.env[key]; return process.env[key];
@ -50,6 +61,53 @@ function envAsNumber(key: string): number | undefined {
return !Number.isNaN(value) ? value : 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 = { const defaultConfig: IConfigRequired = {
tokens: { tokens: {
Discord: envAsString("TOKENS__DISCORD") ?? "", 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: { chatCompletionParams: {
model: envAsString("CHAT_COMPLETION_PARAMS__MODEL") ?? "gpt-3.5-turbo", model: envAsString("CHAT_COMPLETION_PARAMS__MODEL") ?? "gpt-3.5-turbo",
max_tokens: envAsNumber("CHAT_COMPLETION_PARAMS__MAX_TOKENS") ?? 384, max_tokens: envAsNumber("CHAT_COMPLETION_PARAMS__MAX_TOKENS") ?? 384,

View file

@ -55,6 +55,7 @@ interactionManager.bindClient(discord);
discord.on("ready", event => { discord.on("ready", event => {
console.log(`Connected to Discord as ${event.user.tag} (${event.user.id})`); console.log(`Connected to Discord as ${event.user.tag} (${event.user.id})`);
event.user.setPresence(config.status);
}); });
discord.on("messageCreate", message => { discord.on("messageCreate", message => {