Skip to content

Commit d24e708

Browse files
committed
Allow hosting without keywords
1 parent 764540a commit d24e708

File tree

1 file changed

+79
-74
lines changed

1 file changed

+79
-74
lines changed

main.js

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import chalk from 'chalk';
77
import 'dotenv/config';
88
import { start } from 'repl';
99

10-
let { markAsBot, timeCheckInterval, responseMessage, invalidMessage, showLogs, overflowMessage, finalMessage } = load(readFileSync('config.yaml', 'utf8'));
10+
let { markAsBot, timeCheckInterval, responseMessage, invalidMessage, showLogs, overflowMessage, finalMessage, allowKeywords } = load(readFileSync('config.yaml', 'utf8'));
1111

1212
markAsBot = markAsBot ?? true;
1313
showLogs = showLogs ?? false;
14+
allowKeywords = allowKeywords ?? false; // DO NOT ENABLE
1415

1516
log(`${chalk.magenta('STARTED:')} Started Bot`)
1617

@@ -46,24 +47,77 @@ const db = new sqlite3.Database('remindme.sqlite3', (err) => {
4647
// -----------------------------------------------------------------------------
4748
// Main Bot Code
4849

50+
let handlers = {}
4951

50-
const bot = new LemmyBot.LemmyBot({
51-
instance: process.env.LEMMY_INSTANCE,
52-
credentials: {
53-
username: process.env.LEMMY_USERNAME,
54-
password: process.env.LEMMY_PASSWORD,
55-
},
56-
dbFile: 'db.sqlite3',
57-
federation: 'all',
58-
markAsBot: markAsBot,
59-
handlers: {
60-
mention: async ({
61-
mentionView: { comment },
52+
handlers["mention"] = async ({
53+
mentionView: { comment },
54+
botActions: { createComment },
55+
}) => {
56+
const words = comment.content.split(' ');
57+
let amount = 0;
58+
59+
for (let i = 1; i < words.length; i++) {
60+
const word = words[i];
61+
62+
if (word === 'seconds' || word === 'second') {
63+
const seconds = parseInt(words[i - 1]);
64+
if (seconds) amount += seconds;
65+
}
66+
else if (word === 'minutes' || word === 'minute') {
67+
const seconds = parseInt(words[i - 1])*60;
68+
if (seconds) amount += seconds;
69+
}
70+
else if (word === 'hours' || word === 'hour') {
71+
const seconds = parseInt(words[i - 1])*60*60;
72+
if (seconds) amount += seconds;
73+
}
74+
else if (word === 'days' || word === 'day') {
75+
const seconds = parseInt(words[i - 1])*60*60*24;
76+
if (seconds) amount += seconds;
77+
}
78+
else if (word === 'weeks' || word === 'week') {
79+
const seconds = parseInt(words[i - 1])*60*60*24*7;
80+
if (seconds) amount += seconds;
81+
}
82+
else if (word === 'months' || word === 'month') {
83+
const seconds = parseInt(words[i - 1])*60*60*24*7*4;
84+
if (seconds) amount += seconds;
85+
}
86+
else if (word === 'years' || word === 'year') {
87+
const seconds = parseInt(words[i - 1])*60*60*24*7*4*12;
88+
if (seconds) amount += seconds;
89+
}
90+
}
91+
92+
if (amount > 0) {
93+
// Stop larger than 10 years
94+
if (amount > 315360000) {
95+
createComment({ content: overflowMessage, parent_id: comment.id, post_id: comment.post_id });
96+
return;
97+
}
98+
99+
addReminder(amount, comment.ap_id, createComment, comment);
100+
}
101+
else {
102+
createComment({ content: invalidMessage, parent_id: comment.id, post_id: comment.post_id });
103+
}
104+
}
105+
106+
if (allowKeywords) {
107+
handlers["comment"] = {
108+
sort: 'New',
109+
handle: async ({
110+
commentView: {
111+
comment,
112+
},
62113
botActions: { createComment },
63114
}) => {
64115
const words = comment.content.split(' ');
65116
let amount = 0;
66117

118+
if (words.length < 1) return;
119+
if (words[0].toLowerCase() !== '!remindme' && words[0].toLowerCase() !== '@remindme') return;
120+
67121
for (let i = 1; i < words.length; i++) {
68122
const word = words[i];
69123

@@ -109,70 +163,21 @@ const bot = new LemmyBot.LemmyBot({
109163
else {
110164
createComment({ content: invalidMessage, parent_id: comment.id, post_id: comment.post_id });
111165
}
112-
},
113-
comment: {
114-
sort: 'New',
115-
handle: async ({
116-
commentView: {
117-
comment,
118-
},
119-
botActions: { createComment },
120-
}) => {
121-
const words = comment.content.split(' ');
122-
let amount = 0;
123-
124-
if (words.length < 1) return;
125-
if (words[0].toLowerCase() !== '!remindme' && words[0].toLowerCase() !== '@remindme') return;
126-
127-
for (let i = 1; i < words.length; i++) {
128-
const word = words[i];
129-
130-
if (word === 'seconds' || word === 'second') {
131-
const seconds = parseInt(words[i - 1]);
132-
if (seconds) amount += seconds;
133-
}
134-
else if (word === 'minutes' || word === 'minute') {
135-
const seconds = parseInt(words[i - 1])*60;
136-
if (seconds) amount += seconds;
137-
}
138-
else if (word === 'hours' || word === 'hour') {
139-
const seconds = parseInt(words[i - 1])*60*60;
140-
if (seconds) amount += seconds;
141-
}
142-
else if (word === 'days' || word === 'day') {
143-
const seconds = parseInt(words[i - 1])*60*60*24;
144-
if (seconds) amount += seconds;
145-
}
146-
else if (word === 'weeks' || word === 'week') {
147-
const seconds = parseInt(words[i - 1])*60*60*24*7;
148-
if (seconds) amount += seconds;
149-
}
150-
else if (word === 'months' || word === 'month') {
151-
const seconds = parseInt(words[i - 1])*60*60*24*7*4;
152-
if (seconds) amount += seconds;
153-
}
154-
else if (word === 'years' || word === 'year') {
155-
const seconds = parseInt(words[i - 1])*60*60*24*7*4*12;
156-
if (seconds) amount += seconds;
157-
}
158-
}
159-
160-
if (amount > 0) {
161-
// Stop larger than 10 years
162-
if (amount > 315360000) {
163-
createComment({ content: overflowMessage, parent_id: comment.id, post_id: comment.post_id });
164-
return;
165-
}
166+
}
167+
}
168+
}
166169

167-
addReminder(amount, comment.ap_id, createComment, comment);
168-
}
169-
else {
170-
createComment({ content: invalidMessage, parent_id: comment.id, post_id: comment.post_id });
171-
}
172-
}
173-
},
174170

171+
const bot = new LemmyBot.LemmyBot({
172+
instance: process.env.LEMMY_INSTANCE,
173+
credentials: {
174+
username: process.env.LEMMY_USERNAME,
175+
password: process.env.LEMMY_PASSWORD,
175176
},
177+
dbFile: 'db.sqlite3',
178+
federation: 'all',
179+
markAsBot: markAsBot,
180+
handlers: handlers,
176181
schedule: [
177182
{
178183
cronExpression: `0 */${timeCheckInterval} * * * *`,

0 commit comments

Comments
 (0)