Skip to content

Commit 250c63d

Browse files
Merge pull request #66 from ornato-t/main
Optional flag to disable logs
2 parents 2c57438 + 72a5674 commit 250c63d

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/bot.ts

Lines changed: 18 additions & 11 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,9 +681,7 @@ class LemmyBot {
678681
read: true
679682
});
680683

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

685686
return promise;
686687
}
@@ -1151,29 +1152,29 @@ class LemmyBot {
11511152
}
11521153

11531154
start() {
1154-
console.log('Starting bot');
1155+
this.#log('Starting bot');
11551156
this.#isRunning = true;
11561157
this.#runBot();
11571158
}
11581159

11591160
stop() {
1160-
console.log('stopping bot');
1161+
this.#log('Stopping bot');
11611162
this.#isRunning = false;
11621163
}
11631164

11641165
async #login() {
11651166
if (this.#credentials) {
1166-
console.log('logging in');
1167+
this.#log('Logging in');
11671168
const loginRes = await this.#httpClient.login({
11681169
password: this.#credentials.password,
11691170
username_or_email: this.#credentials.username
11701171
});
11711172
this.#auth = loginRes.jwt;
11721173
if (this.#auth) {
1173-
console.log('logged in');
1174+
this.#log('Logged in');
11741175

11751176
if (this.#markAsBot) {
1176-
console.log('Marking account as bot account');
1177+
this.#log('Marking account as bot account');
11771178

11781179
await this.#httpClient
11791180
.saveUserSettings({
@@ -1377,10 +1378,16 @@ class LemmyBot {
13771378
action: () => Promise<T>;
13781379
}) {
13791380
if (this.#auth) {
1380-
console.log(logMessage);
1381+
this.#log(logMessage);
13811382
await action();
13821383
}
13831384
}
1385+
1386+
#log = (output: string) => {
1387+
if (this.#enableLogs) {
1388+
console.log(output);
1389+
}
1390+
}
13841391
}
13851392

13861393
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
@@ -82,6 +82,13 @@ export type BotOptions = {
8282
* @default true
8383
*/
8484
markAsBot?: boolean;
85+
/**
86+
* If true, the bot will output verbose logs for any operation it conducts.
87+
* If set to false, no internal logs will be produced.
88+
*
89+
* @default true
90+
*/
91+
enableLogs?: boolean;
8592
};
8693

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

0 commit comments

Comments
 (0)