Skip to content

Commit bbaef24

Browse files
committed
feat: bug fixes and improvements
- Add src/runner.ts to run and restart target scripts and run checks this to prevent the bot from restarting over again if it didnt meet the requirements and also removes commands the only works on linux - Update package.json to use runner for start and dev - Remove redundant checkRequirements call from src/index.ts - Improve error messages in requirements checks (include err) - Add prisma generate step and tidy README/license formatting - Fix tsconfig typeRoots path from src/typeses to src/types
1 parent e14ac59 commit bbaef24

6 files changed

Lines changed: 92 additions & 30 deletions

File tree

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Canis supports multiple AI providers out of the box:
2121
- [Ollama](https://ollama.com/)
2222

2323
## Features
24+
2425
- Auto react on messages
2526
- Repeat react on messages
2627
- Resent unsend andor edit messages
@@ -70,13 +71,19 @@ Canis supports multiple AI providers out of the box:
7071
cp .env.example .env
7172
```
7273

73-
4. **Run Migration**
74+
4. **Generate Prisma Client**
75+
76+
```sh
77+
npx prisma generate
78+
```
79+
80+
5. **Run Migration**
7481

7582
```sh
7683
npx prisma migrate dev
7784
```
7885

79-
5. **Start bot**
86+
6. **Start bot**
8087

8188
```sh
8289
npm run dev
@@ -116,16 +123,16 @@ A Telegram version of Project Canis is available at [project-canis-tg](https://g
116123

117124
## License
118125

119-
Copyright 2025 Melvin Jones Repol
126+
Copyright 2025 Melvin Jones Repol
120127

121-
Licensed under the Apache License, Version 2.0 (the "License");
122-
you may not use this file except in compliance with the License.
123-
You may obtain a copy of the License at
128+
Licensed under the Apache License, Version 2.0 (the "License");
129+
you may not use this file except in compliance with the License.
130+
You may obtain a copy of the License at
124131

125132
http://www.apache.org/licenses/LICENSE-2.0
126133

127-
Unless required by applicable law or agreed to in writing, software
128-
distributed under the License is distributed on an "AS IS" BASIS,
129-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130-
See the License for the specific language governing permissions and
131-
limitations under the License.
134+
Unless required by applicable law or agreed to in writing, software
135+
distributed under the License is distributed on an "AS IS" BASIS,
136+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137+
See the License for the specific language governing permissions and
138+
limitations under the License.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"scripts": {
88
"build": "prisma generate && ts-node scripts/build.ts",
99
"build:clean": "ts-node scripts/build.ts --clean",
10-
"start": "clear && while [ true ]; do node dist/index.js ; done",
11-
"dev": "clear && while [ true ]; do ts-node src/index.ts ; done",
10+
"start": "node dist/runner.js dist/index.js",
11+
"dev": "ts-node src/runner.ts src/index.ts",
1212
"test:sentry": "ts-node tests/sentry.ts"
1313
},
1414
"author": {

src/components/utils/requirements.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ async function checkDatabase(): Promise<void> {
2121
await prisma.$queryRaw`SELECT 1`;
2222
const end = Date.now() - start;
2323
log.info("Database", `${end}ms Ping`);
24-
} catch {
24+
} catch (err) {
2525
log.error(
26-
"Database",
27-
"Database is required but not found (install mariadb or ensure server is running",
26+
"Failed to connect to Database make sure your database is running",
27+
err,
2828
);
2929
process.exit(1);
3030
}
@@ -36,10 +36,10 @@ async function checkRedis(): Promise<void> {
3636
await redis.ping();
3737
const end = Date.now() - start;
3838
log.info("Redis", `${end}ms Ping`);
39-
} catch {
39+
} catch (err) {
4040
log.error(
41-
"Redis",
42-
"Redis is required but not found (install redis-cli or ensure server is running).",
41+
"Failed to connect to Redis make sure redis-server is running",
42+
err,
4343
);
4444
process.exit(1);
4545
}
@@ -51,11 +51,8 @@ function checkChrome(): void {
5151
.toString()
5252
.trim();
5353
log.info("GoogleChrome", version);
54-
} catch {
55-
log.error(
56-
"GoogleChrome",
57-
"Google Chrome not found. Some features may not work.",
58-
);
54+
} catch (err) {
55+
log.error("Failed to find Google Chrome executable", err);
5956
}
6057
}
6158

@@ -66,8 +63,8 @@ function checkFFMPEG(): void {
6663
.split("\n")[0]
6764
.trim();
6865
log.info("FFMPEG", version);
69-
} catch {
70-
log.warn("FFMPEG", "FFmpeg not found. Some features may not work.");
66+
} catch (err) {
67+
log.warn("Failed to find FFMPEG, some commands might fail to work", err);
7168
}
7269
}
7370

src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ dotenv.config({ quiet: true, debug: process.env.NODE_ENV === "production" });
44
import "./instrument";
55
import { registerCronJobs } from "./cron";
66
import { client } from "./components/client";
7-
import { checkRequirements } from "./components/utils/requirements";
8-
import log from "./components/utils/log";
97
import { mapCommands } from "./components/utils/cmd/loader";
108
import watcher from "./components/utils/cmd/watcher";
119
import "./components/process";
@@ -28,7 +26,6 @@ const phishtank = new PhishTankClient();
2826
let phishingSet: Set<string>;
2927

3028
async function main() {
31-
await checkRequirements();
3229
await Promise.all([
3330
monitor.start(),
3431
phishtank.startAutoUpdateLoop(),

src/runner.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import dotenv from "dotenv";
2+
dotenv.config({ quiet: true, debug: process.env.NODE_ENV === "production" });
3+
4+
import { spawn } from "child_process";
5+
import path from "path";
6+
import fs from "fs";
7+
import log from "./components/utils/log";
8+
import { checkRequirements } from "./components/utils/requirements";
9+
10+
const [, , scriptArg, ...scriptArgs] = process.argv;
11+
12+
if (!scriptArg) {
13+
log.error("Runner", "No script provided");
14+
process.exit(1);
15+
}
16+
17+
const SCRIPT_TO_RUN = path.resolve(scriptArg);
18+
const EXT = path.extname(SCRIPT_TO_RUN);
19+
20+
let RUNTIME: "node" | "ts-node";
21+
22+
switch (EXT) {
23+
case ".ts":
24+
RUNTIME = "ts-node";
25+
break;
26+
case ".js":
27+
RUNTIME = "node";
28+
break;
29+
default:
30+
log.error("Runner", `Unsupported file type: ${EXT} (expected .ts or .js)`);
31+
process.exit(1);
32+
}
33+
34+
async function start() {
35+
await checkRequirements();
36+
37+
if (!fs.existsSync(SCRIPT_TO_RUN)) {
38+
log.error("Runner", `File not found: ${SCRIPT_TO_RUN}`);
39+
process.exit(1);
40+
}
41+
42+
log.info("Runner", `Starting ${SCRIPT_TO_RUN} with ${RUNTIME}`);
43+
44+
const child = spawn(RUNTIME, [SCRIPT_TO_RUN, ...scriptArgs], {
45+
stdio: "inherit",
46+
});
47+
48+
child.on("exit", (code, signal) => {
49+
log.info(
50+
"Exit",
51+
`Child exited (code=${code}, signal=${signal}). Restarting in 1s...`,
52+
);
53+
setTimeout(start, 1000);
54+
});
55+
56+
child.on("error", (err) => {
57+
log.error("Failed to start child process", err);
58+
});
59+
}
60+
61+
start();

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"skipLibCheck": true,
1313
"outDir": "./dist",
1414
"rootDir": "./src",
15-
"typeRoots": ["./node_modules/@types", "./src/typeses"],
15+
"typeRoots": ["./node_modules/@types", "./src/types"],
1616
"incremental": true,
1717
"tsBuildInfoFile": "./.tsbuildinfo"
1818
},

0 commit comments

Comments
 (0)