diff --git a/src/bot.ts b/src/bot.ts index dbd9f58..93d3568 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -10,6 +10,8 @@ import { messageOf, formatImageMessage, formatUserMessage, + Logger, + emp, } from "./utils"; import { allowedChat, @@ -25,6 +27,7 @@ import { ImagesResponse } from "openai/resources"; import chalk from "chalk"; import dotenv from "dotenv"; dotenv.config(); +const logger = new Logger(); // Specific to Tailchat. The endpoint of my Tailchat server, the bot ID and Secret. export const HOST = process.env.HOST; @@ -33,7 +36,7 @@ export const APPSECRET = process.env.SECRET; const allVarsFilled = HOST && APPID && APPSECRET; if (!allVarsFilled) { - console.log("Not all required variables are filled in."); + logger.warn("Not all the required environment variables are filled in."); process.exit(1); } @@ -41,8 +44,8 @@ if (!allVarsFilled) { const session = new GuildData(checkFile("messages.json", "utf-8")); session.data.toString() - ? console.log("Our conversation is:", session.data) - : console.log( + ? logger.log(`Our conversation is: ${session.data}`) + : logger.log( "Looks like we're starting fresh, no previous chat history was found.", ); @@ -58,8 +61,8 @@ client.connect().then(async () => { const SELF = await client.whoami().then((response) => { return response.userId; }); - console.log("Our userId is:", chalk.green(SELF)); - console.log("Messages from this userId are automatically ignored."); + logger.log(`Our userId is: ${emp(SELF)}`); + logger.log("Messages from this userId are automatically ignored."); client.onMessage(async (message) => { const canRespondInGuild = @@ -84,7 +87,7 @@ client.connect().then(async () => { .replace("Please generate: ", "") .replace("please generate: ", "") .replace("without", "|"); - console.log("Generating image:", chalk.green(prompt)); + logger.log(`Generating image: ${emp(prompt)}`); await client.sendMessage({ converseId: message.converseId, @@ -113,7 +116,7 @@ client.connect().then(async () => { .replace(/(\[(.*?)\])/g, "") .replace("{BACKEND}", HOST); const username = await getUsername(HOST, message.author!); - console.log("Analyzing image at:", chalk.green(imageData)); + logger.log(`Analyzing the image at: ${emp(imageData)}`); await client.sendMessage({ converseId: message.converseId, @@ -150,7 +153,7 @@ client.connect().then(async () => { JSON.stringify(session.data), "utf8", ); - console.log("Now our conversation is", session); + logger.log(`Now our conversation is: ${session.data}`); } else { const username = await getUsername(HOST, message.author!); @@ -187,7 +190,7 @@ client.connect().then(async () => { ); } } catch (err) { - console.log("Failed", err); + logger.error(`fs write failed: ${err}`); await client.sendMessage({ converseId: message.converseId, @@ -225,7 +228,7 @@ client.connect().then(async () => { fs.writeFileSync("messages.json", JSON.stringify(session.data), "utf8"); } catch (err) { - console.log("Failed", err); + logger.error(`fs write failed: ${err}`); await client.sendMessage({ converseId: message.converseId, diff --git a/src/utils.ts b/src/utils/functions.ts similarity index 97% rename from src/utils.ts rename to src/utils/functions.ts index fbbb8b6..be668bf 100644 --- a/src/utils.ts +++ b/src/utils/functions.ts @@ -4,8 +4,8 @@ import { ChatHistory, ChatHistoryData, ImageMessage, -} from "./types"; -import { system } from "./assistant"; +} from "../types"; +import { system } from "../assistant"; import { ChatCompletion, ChatCompletionMessage } from "openai/resources"; import { stripMentionTag } from "tailchat-client-sdk"; diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..d106912 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,2 @@ +export * from "./functions"; +export * from "./logger"; diff --git a/src/utils/logger.ts b/src/utils/logger.ts new file mode 100644 index 0000000..a062b38 --- /dev/null +++ b/src/utils/logger.ts @@ -0,0 +1,30 @@ +import chalk, { ChalkFunction } from "chalk"; + +export class Logger { + private _wrn: ChalkFunction; + private _err: ChalkFunction; + private _main: string; + + constructor() { + this._wrn = chalk.yellow; + this._err = chalk.red; + + this._main = chalk.bold.gray("Tailchat-Assistant:"); + } + + log(text: any): void { + console.log(this._main, text); + } + + warn(text: any): void { + console.warn(this._main, this._wrn(text)) + } + + error(text: any): void { + console.error(this._main, this._err(text)); + } +} + +export const emp = chalk.green; +export const wrn = chalk.yellow; +export const err = chalk.red;