-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (78 loc) · 3.36 KB
/
index.js
File metadata and controls
91 lines (78 loc) · 3.36 KB
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
const axios = require('axios');
const fs = require('fs');
require('dotenv').config();
// Configuration
const WEBHOOK_URL = process.env.WEBHOOK_URL;
const API_URL = process.env.API_URL || 'https://api.modifold.com/projects?type=mod&sort=recent&limit=20';
const DATA_FILE = process.env.DATA_FILE || './sent_projects.json';
const INTERVAL_MINUTES = Number(process.env.CHECK_INTERVAL_MINUTES || 2);
const CHECK_INTERVAL_MS = Number.isFinite(INTERVAL_MINUTES) && INTERVAL_MINUTES > 0 ? INTERVAL_MINUTES * 60 * 1000 : 2 * 60 * 1000;
if(!WEBHOOK_URL) {
console.error('WEBHOOK_URL is not set. Add it to .env');
process.exit(1);
}
if(!(Number.isFinite(INTERVAL_MINUTES) && INTERVAL_MINUTES > 0)) {
console.warn('CHECK_INTERVAL_MINUTES is invalid. Using default: 2 minutes.');
}
if(!fs.existsSync(DATA_FILE)) {
fs.writeFileSync(DATA_FILE, JSON.stringify([]));
console.log("Data file was created.");
}
// Load already posted project IDs
const getSentIds = () => JSON.parse(fs.readFileSync(DATA_FILE, 'utf-8'));
// Save a newly posted project ID
const saveSentId = (id) => {
const ids = getSentIds();
ids.push(id);
fs.writeFileSync(DATA_FILE, JSON.stringify(ids));
};
async function checkAndPost() {
try {
const response = await axios.get(API_URL);
const projects = response.data.projects;
const sentIds = getSentIds();
const newProjects = projects.filter(p => !sentIds.includes(p.id)).reverse();
for(const project of newProjects) {
// Build embed payload according to Discord limits
const embed = {
title: project.title.substring(0, 256), // Title limit
url: `https://modifold.com/mod/${project.slug}`,
description: (project.summary || "No description").substring(0, 2048),
color: 3177968, // HEX #F15400
thumbnail: project.icon_url ? { url: project.icon_url } : null,
fields: [
{
name: "Type",
value: project.project_type === "mod" ? "New Release" : "Project",
inline: true
},
{
name: "Developer",
value: project.owner?.username || "Unknown",
inline: true
}
],
footer: {
text: "Modifold • New Release",
icon_url: "https://modifold.com/images/apple-touch-icon-180x180.png"
},
// Use strict ISO timestamp format
timestamp: new Date(project.created_at).toISOString()
};
// Discord expects embeds as an array
await axios.post(WEBHOOK_URL, {
embeds: [embed]
}).catch(err => {
console.error("Discord error details:", err.response?.data);
});
saveSentId(project.id);
console.log(`[${new Date().toLocaleTimeString()}] Posted: ${project.title}`);
await new Promise(resolve => setTimeout(resolve, 1000));
}
} catch (error) {
console.error("Request error:", error.message);
}
}
console.log(`Monitoring started. Interval: ${CHECK_INTERVAL_MS / 60000} minute(s).`);
setInterval(checkAndPost, CHECK_INTERVAL_MS);
checkAndPost();