configDefault: add status options
This commit is contained in:
parent
b1e464fe50
commit
6f5f425166
2 changed files with 69 additions and 0 deletions
|
@ -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,
|
||||
|
|
|
@ -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 => {
|
||||
|
|
Loading…
Reference in a new issue