-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (71 loc) · 2.97 KB
/
index.js
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
import fs from 'fs';
import cron from 'cron';
import { Client, Events, GatewayIntentBits } from 'discord.js';
import configFile from './config.json' assert { type: 'json' };
const db = fs.existsSync('./db.json') ? JSON.parse(fs.readFileSync('./db.json', { encoding: 'utf8' })) : {};
const unixDay = 86400000; // amount of miliseconds in a day
for (let i = 0; i < configFile.worms.length; i++) {
configFile.worms[i] = configFile.worms[i].toUpperCase();
}
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
function updateDB() {
fs.writeFile('./db.json', JSON.stringify(db, null, 4), function writeJSON(err) {
if (err) return console.log(err);
})
}
client.once(Events.ClientReady, readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.MessageCreate, (message) => {
// see if the message needs to be checked
for (const person of configFile.wormedPeople) {
if (message.author.id === person.id) {
// check if it contains any worms
if (message.content.toUpperCase().replace(/[^A-Z ]/g, '').split(' ').some((v) => configFile.worms.includes(v))) {
// check if a db entry for this person already exists
// if not, create one
if (!db[person.id]) {
db[person.id] = {
latestWorm: 0,
wormsToday: 0
}
}
db[person.id].latestWorm = message.createdTimestamp;
db[person.id].wormsToday++;
message.reply(person.badResponse + " This is worm number " + db[person.id].wormsToday + " of the day.");
updateDB();
break;
}
break;
}
}
})
// send message giving an update on abstinence every day at 6am
// also resets daily worm count for everyone
const scheduledSuccessMessages = new cron.CronJob("0 6 * * *", () => {
console.log("Called scheduledSuccessMessages.");
// unix timestamp for right now
const currentTime = Date.now();
for (const id of configFile.successMessageChannels) {
const channel = client.channels.cache.get(id);
for (const person of configFile.wormedPeople) {
if (db[person.id]) {
const daysWithoutWorms = Math.floor((currentTime - db[person.id].latestWorm) / unixDay);
if (daysWithoutWorms >= 1) {
channel.send(
configFile.successMessage
.replace("{ping}", "<@" + person.id + ">")
.replace("{time}", daysWithoutWorms)
);
}
}
}
}
for (const person of configFile.wormedPeople) {
if (db[person.id])
db[person.id].wormsToday = 0;
}
updateDB();
});
scheduledSuccessMessages.start();
client.login(configFile.token);