Skip to content

Commit 6d06861

Browse files
committed
feat: better env checking
1 parent 2355213 commit 6d06861

File tree

6 files changed

+48
-26
lines changed

6 files changed

+48
-26
lines changed

.env.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ DATABASE_URL="file:./bot.db"
4343
# if you're having issues with puppeteer's bundled chromium.
4444
PUPPETEER_EXECUTABLE_PATH=""
4545

46+
# Accepted values are "true" or "false"
47+
# Defaults to "true" if left blank
48+
ENABLE_SOURCES="true"
49+
4650
# Accepted values are "true", "dms_only", "groups_only" or "false"
4751
# Defaults to "true" if left blank
4852
ENABLE_REACTIONS="true"
4953

5054
# Change to your liking
51-
QUEUED_REACTION="🔁" # Defalts to "🔁" if left blank
55+
QUEUED_REACTION="🔁" # Defaults to "🔁" if left blank
5256
WORKING_REACTION="⚙️" # Defaults to "⚙️" if left blank
5357
DONE_REACTION="" # Defaults to "✅" if left blank
5458
ERROR_REACTION="⚠️" # Defaults to "⚠️" if left blank
@@ -95,3 +99,4 @@ BLOCKED_USERS=""
9599
# - If you set BLOCKED_USERS and ALLOWED_USERS, the bot will ignore users in the BLOCKED_USERS list,
96100
# even if they are in the ALLOWED_USERS list.
97101
#
102+
#############################################################################################################

src/clients/bing.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
import { BingAIClient } from "@waylaidwanderer/chatgpt-api";
44
import KeyvSqlite from "@keyv/sqlite";
55

6-
console.log(process.env.DATABASE_URL);
6+
function convertFileToSQLite(string: string) {
7+
// Check if the inputString starts with "file:./"
8+
if (string.startsWith("file:./")) {
9+
// Replace "file:./" with "sqlite://./prisma/"
10+
return string.replace("file:./", "sqlite://./prisma/");
11+
} else {
12+
// If it doesn't start with "file:./", return the original string
13+
return string;
14+
}
15+
}
16+
717
const store = new KeyvSqlite({
8-
uri: "sqlite://./prisma/bot.db",
18+
uri: convertFileToSQLite(process.env.DATABASE_URL as string),
919
table: "cache",
1020
});
1121

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import dotenv from "dotenv";
22
import dotenvExpand from "dotenv-expand";
33
dotenvExpand.expand(dotenv.config());
44

5+
export const ENABLE_REACTIONS = process.env.ENABLE_REACTIONS || "true";
6+
export const ENABLE_SOURCES = process.env.ENABLE_SOURCES || "true";
57
export const BOT_PREFIX = process.env.BOT_PREFIX?.trim() + " " || "[BOT]: ";
68
export const ALLOWED_USERS = process.env.ALLOWED_USERS?.split(",") || [];
79
export const BLOCKED_USERS = process.env.BLOCKED_USERS?.split(",") || [];

src/handlers/completion.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// see src/types/bing-ai-client.d.ts
2-
// @ts-ignore
3-
import type { BingAIClientResponse, SuggestedResponse } from "@waylaidwanderer/chatgpt-api";
4-
5-
// see src/types/bing-ai-client.d.ts
6-
// @ts-ignore
7-
import type { SourceAttribution } from "@waylaidwanderer/chatgpt-api";
2+
import type {
3+
BingAIClientResponse,
4+
SuggestedResponse,
5+
SourceAttribution,
6+
// @ts-ignore
7+
} from "@waylaidwanderer/chatgpt-api";
88

99
import { Message } from "whatsapp-web.js";
1010
import { prisma } from "../clients/prisma";
1111
import { bing } from "../clients/bing";
12-
import { SYSTEM_MESSAGE } from "../constants";
12+
import { ENABLE_SOURCES, SYSTEM_MESSAGE } from "../constants";
1313
import { createConversation, getConversationFor } from "../crud/conversation";
1414
import { createChat, getChatFor } from "../crud/chat";
1515

@@ -45,10 +45,13 @@ export async function getCompletionFor(message: Message, context: string, reply:
4545

4646
const completion = await generateCompletionFor(message, context, onProgress);
4747
completion.response = removeFootnotes(completion.response);
48-
completion.response = completion.response + "\n\n" + getSources(completion);
48+
49+
if (ENABLE_SOURCES === "true")
50+
completion.response = completion.response + "\n\n" + getSources(completion);
4951

5052
// TODO: suggestions will be added later; must have a way to select them when replying
51-
// completion.response = completion.response + "\n\n" + getSuggestions(completion);
53+
// if (ENABLE_SUGGESTIONS === "true")
54+
// completion.response = completion.response + "\n\n" + getSuggestions(completion);
5255

5356
// @ts-ignore
5457
return Promise.all([completion, replyEditing]).then(([completion]) => completion);

src/handlers/reactions.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Message } from "whatsapp-web.js";
2+
import { ENABLE_REACTIONS } from "../constants";
23

34
export const REACTIONS = {
45
queued: process.env.QUEUED_REACTION || "🔁",
@@ -9,14 +10,10 @@ export const REACTIONS = {
910

1011
export type Reaction = keyof typeof REACTIONS;
1112

12-
export async function react(
13-
message: Message,
14-
reaction: keyof typeof REACTIONS
15-
) {
13+
export async function react(message: Message, reaction: keyof typeof REACTIONS) {
1614
const chat = await message.getChat();
17-
const enableReactions = process.env.ENABLE_REACTIONS || "true";
1815

19-
switch (enableReactions) {
16+
switch (ENABLE_REACTIONS) {
2017
case "false":
2118
break;
2219
case "true":

src/helpers/utils.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export function isEmoji(str: string) {
2626
}
2727

2828
export function checkEnv() {
29+
if (!process.env.DATABASE_URL)
30+
throw new Error(
31+
`Invalid database url "${process.env.DATABASE_URL}" provided. Please check the DATABASE_URL variable your .env file.`
32+
);
2933
if (!process.env.BOT_PREFIX)
3034
throw new Error(
3135
`Invalid bot prefix "${process.env.BOT_PREFIX}" provided. Please check the BOT_PREFIX variable your .env file.`
@@ -41,12 +45,13 @@ export function checkEnv() {
4145
`Invalid system message "${process.env.SYSTEM_MESSAGE}" provided. Please check the SYSTEM_MESSAGE variable your .env file.`
4246
);
4347

44-
// Checks if all reactions are valid emojis
45-
Object.values(REACTIONS).forEach((reaction) => {
46-
if (!isEmoji(reaction)) {
47-
throw new Error(
48-
`Invalid reaction "${reaction}" provided. Please check the reactions variables your .env file. Make sure to only use emojis.`
49-
);
50-
}
51-
});
48+
if (process.env.ENABLE_REACTIONS !== "false")
49+
// Checks if all reactions are valid emojis
50+
Object.values(REACTIONS).forEach((reaction) => {
51+
if (!isEmoji(reaction)) {
52+
throw new Error(
53+
`Invalid reaction "${reaction}" provided. Please check the reactions variables your .env file. Make sure to only use emojis.`
54+
);
55+
}
56+
});
5257
}

0 commit comments

Comments
 (0)