-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
75 lines (66 loc) · 1.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require('dotenv').config();
const Snoowrap = require('snoowrap');
import { Comment as SnooWrapComment } from 'snoowrap';
import { Comment, GetComments } from 'types';
interface RawComment extends SnooWrapComment {
link_author: string;
}
const r = new Snoowrap({
userAgent: 'Whatever',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
username: process.env.REDDIT_USER,
password: process.env.REDDIT_PASS,
});
const SUBREDDIT_NAME = 'tgtestbot';
const getMappedComments = (comments: RawComment[]) =>
comments.map(
({
body,
link_author: linkAuthor,
author_flair_text: authorFlair,
author: commentAuthor,
}) => ({
body,
linkAuthor,
authorFlair,
commentAuthor,
})
);
const getComments: GetComments = () =>
r
.getSubreddit(SUBREDDIT_NAME)
.getNewComments()
.then((comments: RawComment[]): Comment[] => {
return getMappedComments(comments);
});
const Increase = (comments: Comment[]) => {
comments.forEach((comment: Comment): void => {
if (comment.body.indexOf('Δ') !== -1) {
increase(comment.linkAuthor);
}
});
};
const increase = async (linkAuthor: string) => {
let flair = await r.getSubreddit(SUBREDDIT_NAME).getUserFlair(linkAuthor)
.flair_text;
if (!flair) {
r.getUser(linkAuthor).assignFlair({
subredditName: SUBREDDIT_NAME,
text: 'Delta 1',
});
} else {
const splitFlair = flair.split(' ');
const intFlair = parseInt(splitFlair[1]);
const pointValue = intFlair + 1;
r.getUser(linkAuthor).assignFlair({
subredditName: SUBREDDIT_NAME,
text: 'Delta ' + pointValue,
});
}
};
const main = async (): Promise<void> => {
const comments = await getComments();
Increase(comments);
};
main();