Discord Benachrichigung, PWA zum teil implementiert

This commit is contained in:
dev committed 2025-12-19 13:07:59 +01:00
1 parent b82adc93c7
commit dae99a0b76
716 files changed
+128101 -21

No files matched your search

+113 -8
View File
@@ -3,38 +3,143 @@ import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
const stateFile = path.join(__dirname, "state.json");
// Initialzustand laden oder erzeugen
// --------------------
// Konfiguration
// --------------------
const CONFIG = {
A: {
name: "Marc",
color: 0x2ecc71, // grün
icon: "a.png"
},
B: {
name: "Laila",
color: 0xe74c3c, // rot
icon: "b.png"
}
};
// Öffentliche URL zu deinem Server (WICHTIG für Discord Icons!)
const PUBLIC_BASE_URL = process.env.PUBLIC_BASE_URL
|| "http://localhost:3000";
// 🔔 Discord Webhook URL
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL
|| "https://discord.com/api/webhooks/1450872860607381515/zRbiXOykKPUfLJ_H87wD2OxQJPLdro6yQzU2T48Pg4fxSNaXEqBFgxdHGTiIDmmKGHIF";
// --------------------
// State Handling
// --------------------
function getState() {
if (!fs.existsSync(stateFile)) {
fs.writeFileSync(stateFile, JSON.stringify({ current: "A" }, null, 2));
fs.writeFileSync(
stateFile,
JSON.stringify({ current: "A" }, null, 2)
);
}
return JSON.parse(fs.readFileSync(stateFile));
return JSON.parse(fs.readFileSync(stateFile, "utf-8"));
}
function setState(newState) {
fs.writeFileSync(stateFile, JSON.stringify(newState, null, 2));
}
// GET: aktuellen Zustand
// --------------------
// Discord Webhook
// --------------------
async function sendDiscordEmbed(previousKey, currentKey) {
const previous = CONFIG[previousKey];
const current = CONFIG[currentKey];
if (!DISCORD_WEBHOOK_URL) return;
const embed = {
title: "💸 Zahl-Wechsel",
description: `**${current.name}** ist jetzt dran zu zahlen`,
color: current.color,
thumbnail: {
url: `${PUBLIC_BASE_URL}/img/${current.icon}`
},
fields: [
{
name: "Von",
value: previous.name,
inline: true
},
{
name: "Zu",
value: current.name,
inline: true
}
],
footer: {
text: new Date().toLocaleString("de-DE")
}
};
try {
await fetch(DISCORD_WEBHOOK_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
embeds: [embed]
})
});
} catch (err) {
console.error("Discord Webhook Fehler:", err.message);
}
}
// --------------------
// Routes
// --------------------
// GET: aktueller Zustand
app.get("/status", (req, res) => {
res.json(getState());
});
// POST: wechseln
app.post("/toggle", (req, res) => {
app.post("/toggle", async (req, res) => {
const state = getState();
const previousKey = state.current;
state.current = state.current === "A" ? "B" : "A";
setState(state);
res.json(state);
console.log(
`Zahl-Wechsel: ${CONFIG[previousKey].name} → ${CONFIG[state.current].name}`
);
await sendDiscordEmbed(previousKey, state.current);
res.json({
current: state.current,
name: CONFIG[state.current].name
});
});
// Fallback
app.get(/.*/, (req, res) => {
res.sendFile(path.join(__dirname, "public/index.html"));
});
// --------------------
// Start
// --------------------
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Zahl-Schalter läuft auf Port ${PORT}`));
app.listen(PORT, () =>
console.log(`WhoPays läuft auf http://localhost:${PORT}`)
);