Skip to content

Commit 127bb8b

Browse files
2 parents e146a2d + 250c63d commit 127bb8b

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/bot.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class LemmyBot {
4343
#timeouts: NodeJS.Timeout[] = [];
4444
#auth?: string;
4545
#markAsBot: boolean;
46+
#enableLogs: boolean;
4647
#defaultMinutesUntilReprocess?: number;
4748
#federationOptions: BotFederationOptions;
4849
#tasks: ScheduledTask[] = [];
@@ -391,7 +392,8 @@ class LemmyBot {
391392
dbFile,
392393
federation,
393394
schedule,
394-
markAsBot = true
395+
markAsBot = true,
396+
enableLogs = true
395397
}: BotOptions) {
396398
switch (federation) {
397399
case undefined:
@@ -468,6 +470,7 @@ class LemmyBot {
468470
this.#defaultSecondsBetweenPolls = defaultSecondsBetweenPolls;
469471
this.#isRunning = false;
470472
this.#markAsBot = markAsBot;
473+
this.#enableLogs = enableLogs;
471474
this.#instance = instance;
472475
this.#defaultMinutesUntilReprocess = defaultMinutesUntilReprocess;
473476
this.#httpClient = new LemmyHttp(
@@ -540,7 +543,7 @@ class LemmyBot {
540543
modBanFromSite: modBanFromSiteOptions
541544
} = this.#handlers;
542545

543-
await setupDB(this.#dbFile);
546+
await setupDB(this.#log, this.#dbFile);
544547

545548
if (this.#credentials) {
546549
await this.#login();
@@ -678,7 +681,7 @@ class LemmyBot {
678681
read: true
679682
});
680683

681-
console.log(
684+
this.#log(
682685
`Marked private message ID ${messageView.private_message.id} from ${messageView.creator.id} as read`
683686
);
684687

@@ -1151,29 +1154,29 @@ class LemmyBot {
11511154
}
11521155

11531156
start() {
1154-
console.log('Starting bot');
1157+
this.#log('Starting bot');
11551158
this.#isRunning = true;
11561159
this.#runBot();
11571160
}
11581161

11591162
stop() {
1160-
console.log('stopping bot');
1163+
this.#log('Stopping bot');
11611164
this.#isRunning = false;
11621165
}
11631166

11641167
async #login() {
11651168
if (this.#credentials) {
1166-
console.log('logging in');
1169+
this.#log('Logging in');
11671170
const loginRes = await this.#httpClient.login({
11681171
password: this.#credentials.password,
11691172
username_or_email: this.#credentials.username
11701173
});
11711174
this.#auth = loginRes.jwt;
11721175
if (this.#auth) {
1173-
console.log('logged in');
1176+
this.#log('Logged in');
11741177

11751178
if (this.#markAsBot) {
1176-
console.log('Marking account as bot account');
1179+
this.#log('Marking account as bot account');
11771180

11781181
await this.#httpClient
11791182
.saveUserSettings({
@@ -1383,6 +1386,12 @@ class LemmyBot {
13831386
throw new Error('Not logged in');
13841387
}
13851388
}
1389+
1390+
#log = (output: string) => {
1391+
if (this.#enableLogs) {
1392+
console.log(output);
1393+
}
1394+
};
13861395
}
13871396

13881397
export default LemmyBot;

src/db.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ const createTable = (db: Database, table: string) => {
154154
db.run(`CREATE UNIQUE INDEX IF NOT EXISTS idx_${table}_id ON ${table} (id);`);
155155
};
156156

157-
export const setupDB = async (dbPath?: string) => {
157+
export const setupDB = async (log: (output: string) => void, dbPath?: string) => {
158158
if (dbPath && !existsSync(dbPath)) {
159-
console.log('Creating database file');
159+
log('Creating database file');
160160

161161
try {
162162
await mkdir(path.dirname(dbPath), { recursive: true });
@@ -170,7 +170,7 @@ export const setupDB = async (dbPath?: string) => {
170170

171171
await useDatabase(async (db) => {
172172
db.serialize(() => {
173-
console.log('Initializing DB');
173+
log('Initializing DB');
174174
for (const table of tableTypes) {
175175
createTable(db, table);
176176
}

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ export type BotOptions = {
9292
* @default true
9393
*/
9494
markAsBot?: boolean;
95+
/**
96+
* If true, the bot will output verbose logs for any operation it conducts.
97+
* If set to false, no internal logs will be produced.
98+
*
99+
* @default true
100+
*/
101+
enableLogs?: boolean;
95102
};
96103

97104
export type ParentType = 'post' | 'comment';

0 commit comments

Comments
 (0)