-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
127 lines (116 loc) · 3.88 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import "dotenv/config";
import { getMostRecentMiningSats } from "./braiins";
import { createComment, donateToRewards, search } from "./sn";
const getMonthAbbr = (num: number) =>
[
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
][num];
const main = async () => {
// optionally load sats via process.env
let todaySats: string | number | undefined = process.env.SATS;
if (!todaySats) {
todaySats = await getMostRecentMiningSats();
// Add some slight variation to the number to avoid doxxing
const randOffset = Math.floor(Math.random() * 5);
// randomly add or subtract the offset for some variance
if (Math.random() > 0.5) {
todaySats += randOffset;
} else {
todaySats -= randOffset;
}
}
const myComments = [];
let cursor;
let done = false;
let counter = 0;
const max = 20;
while (!done) {
const { cursor: responseCursor, items } = await search({
cursor,
what: "comments",
nym: "weareallsatoshi",
});
await new Promise((resolve) => setTimeout(resolve, 500));
counter++;
console.log("request counter: " + counter);
if (responseCursor === null || counter === max) {
done = true;
}
myComments.push(...items);
cursor = responseCursor;
}
const dailyMiningComments = myComments
.filter((comment) => /^Day \d+ of posting/.test(comment.text))
.sort(
(comment1, comment2) =>
new Date(comment2.createdAt).getTime() -
new Date(comment1.createdAt).getTime()
);
const mostRecentDailyComment = dailyMiningComments[0];
console.log(mostRecentDailyComment);
const { id: mostRecentDailyCommentId } = mostRecentDailyComment;
const rxResult =
/^Day (\d+) of posting mining earnings from the day before: \d+ sats on (\d{1,2}[a-zA-Z]{1,4}\d{4})!\nRunning total: (\d+,?\d+) sats!/.exec(
mostRecentDailyComment.text
);
if (!rxResult) {
console.log(
"Failed to match regex on most recent daily comment. Exiting..."
);
return;
}
const [, mostRecentDayCount, mostRecentDate, mostRecentTotal] = rxResult;
console.log({
mostRecentDayCount,
mostRecentDate,
mostRecentTotal,
mostRecentDailyCommentId,
});
const yesterdayDate = new Date(Date.now() - 864e5);
const nextComment = `Day ${
Number(mostRecentDayCount) + 1
} of posting mining earnings from the day before: ${todaySats} sats on ${yesterdayDate.getDate()}${getMonthAbbr(
yesterdayDate.getMonth()
)}${yesterdayDate.getFullYear()}!\nRunning total: ${Intl.NumberFormat().format(
Number(mostRecentTotal.toString().replaceAll(",", "")) + Number(todaySats)
)} sats!\n\n[Yesterday's comment](https://stacker.news/items/${mostRecentDailyCommentId}/r/WeAreAllSatoshi)`;
console.log("Next comment:");
console.log(nextComment);
const { items: saloonPosts } = await search({ what: "posts", nym: "saloon" });
const mostRecentSaloonPost = saloonPosts.sort(
(post1, post2) =>
new Date(post2.createdAt).getTime() - new Date(post1.createdAt).getTime()
)[0];
const saloonPostId = mostRecentSaloonPost.id;
console.log("posting today's top level comment...");
const { newCommentId } = await createComment({
parentId: saloonPostId,
text: nextComment,
});
const yesterdaysCommentReply = `[Day ${
Number(mostRecentDayCount) + 1
}'s comment](https://stacker.news/items/${newCommentId}/r/WeAreAllSatoshi)`;
console.log("\nYesterdays comment reply:");
console.log(yesterdaysCommentReply);
console.log("posting the reply to yesterday's comment...");
await createComment({
parentId: mostRecentDailyCommentId,
text: yesterdaysCommentReply,
});
console.log("posted!");
console.log("making daily donation to rewards...");
const { donatedAmount } = await donateToRewards({ sats: 100 });
console.log(`donated ${donatedAmount} sats!`);
};
main().catch(console.error);