Add a custom logger, move utils into a folder
This commit is contained in:
parent
f4592fff96
commit
bb49873961
23
src/bot.ts
23
src/bot.ts
|
@ -10,6 +10,8 @@ import {
|
||||||
messageOf,
|
messageOf,
|
||||||
formatImageMessage,
|
formatImageMessage,
|
||||||
formatUserMessage,
|
formatUserMessage,
|
||||||
|
Logger,
|
||||||
|
emp,
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
import {
|
import {
|
||||||
allowedChat,
|
allowedChat,
|
||||||
|
@ -25,6 +27,7 @@ import { ImagesResponse } from "openai/resources";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
const logger = new Logger();
|
||||||
|
|
||||||
// Specific to Tailchat. The endpoint of my Tailchat server, the bot ID and Secret.
|
// Specific to Tailchat. The endpoint of my Tailchat server, the bot ID and Secret.
|
||||||
export const HOST = process.env.HOST;
|
export const HOST = process.env.HOST;
|
||||||
|
@ -33,7 +36,7 @@ export const APPSECRET = process.env.SECRET;
|
||||||
const allVarsFilled = HOST && APPID && APPSECRET;
|
const allVarsFilled = HOST && APPID && APPSECRET;
|
||||||
|
|
||||||
if (!allVarsFilled) {
|
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);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +44,8 @@ if (!allVarsFilled) {
|
||||||
const session = new GuildData(checkFile("messages.json", "utf-8"));
|
const session = new GuildData(checkFile("messages.json", "utf-8"));
|
||||||
|
|
||||||
session.data.toString()
|
session.data.toString()
|
||||||
? console.log("Our conversation is:", session.data)
|
? logger.log(`Our conversation is: ${session.data}`)
|
||||||
: console.log(
|
: logger.log(
|
||||||
"Looks like we're starting fresh, no previous chat history was found.",
|
"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) => {
|
const SELF = await client.whoami().then((response) => {
|
||||||
return response.userId;
|
return response.userId;
|
||||||
});
|
});
|
||||||
console.log("Our userId is:", chalk.green(SELF));
|
logger.log(`Our userId is: ${emp(SELF)}`);
|
||||||
console.log("Messages from this userId are automatically ignored.");
|
logger.log("Messages from this userId are automatically ignored.");
|
||||||
|
|
||||||
client.onMessage(async (message) => {
|
client.onMessage(async (message) => {
|
||||||
const canRespondInGuild =
|
const canRespondInGuild =
|
||||||
|
@ -84,7 +87,7 @@ client.connect().then(async () => {
|
||||||
.replace("Please generate: ", "")
|
.replace("Please generate: ", "")
|
||||||
.replace("please generate: ", "")
|
.replace("please generate: ", "")
|
||||||
.replace("without", "|");
|
.replace("without", "|");
|
||||||
console.log("Generating image:", chalk.green(prompt));
|
logger.log(`Generating image: ${emp(prompt)}`);
|
||||||
|
|
||||||
await client.sendMessage({
|
await client.sendMessage({
|
||||||
converseId: message.converseId,
|
converseId: message.converseId,
|
||||||
|
@ -113,7 +116,7 @@ client.connect().then(async () => {
|
||||||
.replace(/(\[(.*?)\])/g, "")
|
.replace(/(\[(.*?)\])/g, "")
|
||||||
.replace("{BACKEND}", HOST);
|
.replace("{BACKEND}", HOST);
|
||||||
const username = await getUsername(HOST, message.author!);
|
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({
|
await client.sendMessage({
|
||||||
converseId: message.converseId,
|
converseId: message.converseId,
|
||||||
|
@ -150,7 +153,7 @@ client.connect().then(async () => {
|
||||||
JSON.stringify(session.data),
|
JSON.stringify(session.data),
|
||||||
"utf8",
|
"utf8",
|
||||||
);
|
);
|
||||||
console.log("Now our conversation is", session);
|
logger.log(`Now our conversation is: ${session.data}`);
|
||||||
} else {
|
} else {
|
||||||
const username = await getUsername(HOST, message.author!);
|
const username = await getUsername(HOST, message.author!);
|
||||||
|
|
||||||
|
@ -187,7 +190,7 @@ client.connect().then(async () => {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Failed", err);
|
logger.error(`fs write failed: ${err}`);
|
||||||
|
|
||||||
await client.sendMessage({
|
await client.sendMessage({
|
||||||
converseId: message.converseId,
|
converseId: message.converseId,
|
||||||
|
@ -225,7 +228,7 @@ client.connect().then(async () => {
|
||||||
|
|
||||||
fs.writeFileSync("messages.json", JSON.stringify(session.data), "utf8");
|
fs.writeFileSync("messages.json", JSON.stringify(session.data), "utf8");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Failed", err);
|
logger.error(`fs write failed: ${err}`);
|
||||||
|
|
||||||
await client.sendMessage({
|
await client.sendMessage({
|
||||||
converseId: message.converseId,
|
converseId: message.converseId,
|
||||||
|
|
|
@ -4,8 +4,8 @@ import {
|
||||||
ChatHistory,
|
ChatHistory,
|
||||||
ChatHistoryData,
|
ChatHistoryData,
|
||||||
ImageMessage,
|
ImageMessage,
|
||||||
} from "./types";
|
} from "../types";
|
||||||
import { system } from "./assistant";
|
import { system } from "../assistant";
|
||||||
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
|
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
|
||||||
import { stripMentionTag } from "tailchat-client-sdk";
|
import { stripMentionTag } from "tailchat-client-sdk";
|
||||||
|
|
2
src/utils/index.ts
Normal file
2
src/utils/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export * from "./functions";
|
||||||
|
export * from "./logger";
|
30
src/utils/logger.ts
Normal file
30
src/utils/logger.ts
Normal file
|
@ -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;
|
Loading…
Reference in a new issue