Skip to content

Commit 0c87e93

Browse files
committed
Add numbers to handling
1 parent 2e6fe75 commit 0c87e93

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.env
22
/db.sqlite3
3+
/remindme.sqlite3
34
/node_modules

config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ instances:
142142
keywords:
143143
- webdev
144144
- web
145-
- react
146-
- vue
147-
- angular
148145
- html
149146
- css
150147
linux:
@@ -261,7 +258,6 @@ instances:
261258
- angular
262259
c_lang:
263260
keywords:
264-
- c
265261
- clang
266262
shell:
267263
keywords:
@@ -482,7 +478,6 @@ instances:
482478
vlang:
483479
keywords:
484480
- vlang
485-
- v
486481
fsharp:
487482
keywords:
488483
- fsharp

src/common/database.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @file Creates and provides a database to store persistent data for the bot
3+
*/
4+
5+
import { LogCategory, log } from './log.js'
6+
import sqlite3 from 'sqlite3'
7+
8+
const db = new sqlite3.Database('remindme.sqlite3', (err) => {
9+
if (err) {
10+
return console.error(err.message)
11+
}
12+
log('DB', 'Connected to the database.', LogCategory.SUCCESS)
13+
14+
db.run(
15+
`CREATE TABLE IF NOT EXISTS triggeredon (
16+
id INTEGER PRIMARY KEY AUTOINCREMENT,
17+
community TEXT NOT NULL,
18+
name TEXT NOT NULL
19+
)`,
20+
(err) => {
21+
if (err) {
22+
return console.error(err.message)
23+
}
24+
log('TABLE', 'Loaded triggeredon table.', LogCategory.SUCCESS)
25+
}
26+
)
27+
})
28+
29+
export default db

src/handlers/post.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import chalk from 'chalk'
12
import { instances, responseMessage } from '../common/config.js'
3+
import db from '../common/database.js'
4+
import { LogCategory, log } from '../common/log.js'
25

36
export default async ({
47
postView: { post, community },
@@ -14,6 +17,10 @@ export default async ({
1417
return
1518
}
1619

20+
if (!post.name) {
21+
return
22+
}
23+
1724
let communities = []
1825

1926
for (const [matchingCommunity, communityValue] of Object.entries(
@@ -27,9 +34,33 @@ export default async ({
2734
continue
2835
}
2936

37+
const matches = await new Promise(function (resolve, reject) {
38+
db.all(
39+
`SELECT COUNT(*) as count FROM triggeredon WHERE community = ? AND name = ?`,
40+
[matchingCommunity, post.name],
41+
(err, rows) => {
42+
if (err) {
43+
reject(err)
44+
}
45+
46+
if (!rows) {
47+
resolve(0)
48+
}
49+
50+
resolve(rows[0].count)
51+
}
52+
)
53+
})
54+
55+
if (matches > 0) {
56+
continue
57+
}
58+
3059
const words = post.name.match(
3160
new RegExp(
32-
`(?:^|[^A-z])(?:${communityValue.keywords.join('|')})(?:$|[^A-z])`,
61+
`(?:^|[^A-Za-z0-9])(?:${communityValue.keywords.join(
62+
'|'
63+
)})(?:$|[^A-Za-z0-9])`,
3364
'gi'
3465
)
3566
)
@@ -52,6 +83,17 @@ export default async ({
5283
),
5384
post_id: post.id,
5485
})
86+
87+
db.run(
88+
`INSERT INTO triggeredon (community, name) VALUES (?, ?)`,
89+
[community.name, post.name],
90+
(err) => {
91+
if (err) {
92+
return console.error(err.message)
93+
}
94+
log('DB', 'Inserted new post into database.', LogCategory.SUCCESS)
95+
}
96+
)
5597
}
5698

5799
function getCommunityInstance(community) {

0 commit comments

Comments
 (0)