Skip to content

Commit 246ed2a

Browse files
committed
Allow disabling logging
1 parent 9f5112d commit 246ed2a

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ dayCutOff: 7
2020
# dont want any old posts posted when the bot is first ran. Set to false to post normally
2121
stopPosts: false
2222

23+
# Set to true if you want to see log messages. False if not
24+
# (Note I cant control log messages sent by the bot library so those will still show. Just ones thrown by the bot wont)
25+
showLogs: false
26+
2327

2428
# ------------------------------------------------------------------------------
2529

main.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@ let parser = new Parser({
1313
});
1414
console.log(`${chalk.magenta('STARTED:')} Started Bot`)
1515

16-
let { instances, feeds, markAsBot, postCheckInterval, dayCheckInterval, timezone, dayCutOff, stopPosts } = load(readFileSync('config.yaml', 'utf8'));
16+
let { instances, feeds, markAsBot, postCheckInterval, dayCheckInterval, timezone, dayCutOff, stopPosts, showLogs } = load(readFileSync('config.yaml', 'utf8'));
1717

1818
markAsBot = markAsBot ?? true;
1919
postCheckInterval = postCheckInterval ?? 10;
2020
dayCheckInterval = dayCheckInterval ?? 10;
2121
timezone = timezone ?? 'America/Toronto';
2222
dayCutOff = dayCutOff ?? 7;
2323
stopPosts = stopPosts ?? false;
24+
showLogs = showLogs ?? false;
2425

25-
console.log(`${chalk.grey('INSTANCES:')} ${Object.keys(instances).length} instances loaded.`)
26-
console.log(`${chalk.grey('FEEDS:')} ${Object.keys(feeds).length} feeds loaded.`)
26+
log(`${chalk.grey('INSTANCES:')} ${Object.keys(instances).length} instances loaded.`)
27+
log(`${chalk.grey('FEEDS:')} ${Object.keys(feeds).length} feeds loaded.`)
28+
29+
function log(message) {
30+
if (showLogs) {
31+
console.log(message);
32+
}
33+
}
2734

2835
// -----------------------------------------------------------------------------
2936
// Databases
@@ -32,7 +39,7 @@ const db = new sqlite3.Database('mega.sqlite3', (err) => {
3239
if (err) {
3340
return console.error(err.message);
3441
}
35-
console.log(`${chalk.green('DB:')} Connected to the database.`);
42+
log(`${chalk.green('DB:')} Connected to the database.`);
3643

3744
db.run(`CREATE TABLE IF NOT EXISTS posts (
3845
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -44,7 +51,7 @@ const db = new sqlite3.Database('mega.sqlite3', (err) => {
4451
if (err) {
4552
return console.error(err.message);
4653
}
47-
console.log(`${chalk.grey('TABLE:')} Loaded posts table.`);
54+
log(`${chalk.grey('TABLE:')} Loaded posts table.`);
4855
});
4956

5057
db.run(`CREATE TABLE IF NOT EXISTS time (
@@ -54,7 +61,7 @@ const db = new sqlite3.Database('mega.sqlite3', (err) => {
5461
if (err) {
5562
return console.error(err.message);
5663
}
57-
console.log(`${chalk.grey('TABLE:')} Loaded time table`);
64+
log(`${chalk.grey('TABLE:')} Loaded time table`);
5865

5966
db.run(`INSERT OR IGNORE INTO time (key, value) VALUES ('day', 0)`, (err) => {
6067
if (err) {
@@ -69,7 +76,7 @@ const db = new sqlite3.Database('mega.sqlite3', (err) => {
6976
return console.error(err.message);
7077
}
7178

72-
console.log(`${chalk.grey('POSTS:')} ${rows[0].count} posts in database.`)
79+
log(`${chalk.grey('POSTS:')} ${rows[0].count} posts in database.`)
7380
});
7481
});
7582

@@ -125,7 +132,7 @@ const bot = new LemmyBot.LemmyBot({
125132
if (rows[0].featured) {
126133
// Pin post
127134
await featurePost({postId: post.id, featureType: "Community", featured: true})
128-
console.log(`${chalk.green('PINNED:')} Pinned ${post.name} in ${post.community_id} by ${creator.name}`)
135+
log(`${chalk.green('PINNED:')} Pinned ${post.name} in ${post.community_id} by ${creator.name}`)
129136

130137
// Update post in db
131138
db.run(`UPDATE posts SET post_id = ? WHERE link = ?`, [post.id, post.url], (err) => {
@@ -146,7 +153,7 @@ const bot = new LemmyBot.LemmyBot({
146153
cronExpression: `0 */${postCheckInterval} * * * *`,
147154
timezone: timezone,
148155
doTask: async ({getCommunityId, createPost}) => {
149-
console.log(`${chalk.cyan('STARTED:')} RSS Feed Fetcher.`);
156+
log(`${chalk.cyan('STARTED:')} RSS Feed Fetcher.`);
150157
for (const [name, feed] of Object.entries(feeds)) {
151158
const rss = await parser.parseURL(feed.url);
152159

@@ -156,7 +163,7 @@ const bot = new LemmyBot.LemmyBot({
156163
let joinedItems = [];
157164
// gather all items from feeds to be joined
158165
if (feed.joinfeeds) {
159-
console.log(`${chalk.grey('FETCHING:')} joining feeds for ${name}`);
166+
log(`${chalk.grey('FETCHING:')} joining feeds for ${name}`);
160167
for (const joinFeedName of feed.joinfeeds) {
161168
const joinFeed = Object.entries(feeds).find(f => f[0] === joinFeedName);
162169

@@ -170,7 +177,7 @@ const bot = new LemmyBot.LemmyBot({
170177
let excludeItems = [];
171178
// exclude feeds
172179
if (feed.exclude) {
173-
console.log(`${chalk.grey('FETCHING:')} exclusion feeds for ${name}`);
180+
log(`${chalk.grey('FETCHING:')} exclusion feeds for ${name}`);
174181
for (const excludeFeedName of feed.exclude) {
175182
const excludeFeed = Object.entries(feeds).find(f => f[0] === excludeFeedName);
176183

@@ -219,14 +226,14 @@ const bot = new LemmyBot.LemmyBot({
219226
return console.error(err.message);
220227
}
221228
}
222-
console.log(`${chalk.yellow('INSERTED:')} ${item.link} into database.`);
229+
log(`${chalk.yellow('INSERTED:')} ${item.link} into database.`);
223230

224231
if (stopPosts) return;
225232

226233
for (const [instance, communities] of Object.entries(instances)) {
227234
for (const [community, value] of Object.entries(communities)) {
228235
if (Object.values(value).includes(name)) {
229-
console.log(`${chalk.grey('CREATING:')} post for link ${item.link} in ${community }`);
236+
log(`${chalk.grey('CREATING:')} post for link ${item.link} in ${community }`);
230237
const communityId = await getCommunityId({ name: community, instance: instance });
231238

232239
let title = item.title;
@@ -253,7 +260,7 @@ const bot = new LemmyBot.LemmyBot({
253260
}
254261

255262
}
256-
console.log(`${chalk.green('COMPLETE:')} Feed ${name} processed.`);
263+
log(`${chalk.green('COMPLETE:')} Feed ${name} processed.`);
257264
}
258265
}
259266
},
@@ -276,14 +283,14 @@ const bot = new LemmyBot.LemmyBot({
276283
}
277284
});
278285

279-
console.log(`${chalk.magenta('TIME:')} Updated day to ${day}`);
286+
log(`${chalk.magenta('TIME:')} Updated day to ${day}`);
280287
// decrement all post times by 1
281288
db.run(`UPDATE posts SET pin_days = pin_days - 1 WHERE featured = 1`, (err) => {
282289
if (err) {
283290
return console.error(err.message);
284291
}
285292

286-
console.log(`${chalk.magenta('TIME:')} Decremented all post times`);
293+
log(`${chalk.magenta('TIME:')} Decremented all post times`);
287294

288295
// get all posts with 0 days left and unpin them
289296
db.all(`SELECT * FROM posts WHERE pin_days = 0 && featured = 1`, async (err, rows) => {
@@ -293,7 +300,7 @@ const bot = new LemmyBot.LemmyBot({
293300

294301
for (const row of rows) {
295302
await featurePost({postId: row.post_id, featureType: "Community", featured: false})
296-
console.log(`${chalk.green('UNFEATURED:')} Unfeatured ${row.post_id}`);
303+
log(`${chalk.green('UNFEATURED:')} Unfeatured ${row.post_id}`);
297304
}
298305

299306
// set all posts with 0 days left to unfeatured
@@ -302,7 +309,7 @@ const bot = new LemmyBot.LemmyBot({
302309
return console.error(err.message);
303310
}
304311

305-
console.log(`${chalk.magenta('TIME:')} Unfeatured all posts with 0 days left`);
312+
log(`${chalk.magenta('TIME:')} Unfeatured all posts with 0 days left`);
306313
});
307314
});
308315
});

0 commit comments

Comments
 (0)