Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (87 loc) · 3.27 KB
/
Copy pathindex.js
File metadata and controls
107 lines (87 loc) · 3.27 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import express from "express";
import session from "express-session";
import fetch from "node-fetch";
import fs from "fs";
import { v4 as uuidv4 } from "uuid";
import { Client, GatewayIntentBits, REST, Routes } from "discord.js";
import dotenv from "dotenv";
dotenv.config();
const PORT = process.env.PORT || 3000;
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
const CLIENT_ID = process.env.CLIENT_ID;
const BACKEND_URL = process.env.BACKEND_URL || `http://localhost:${PORT}`;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
const commands = [
{ name: "link", description: "Get your Diswatch UUID link code" }
];
const rest = new REST({ version: "10" }).setToken(DISCORD_TOKEN);
(async () => {
try {
console.log("Registering slash commands...");
await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands });
console.log("Slash commands registered.");
} catch (err) {
console.error(err);
}
})();
// ===== Backend Setup =====
const app = express();
app.use(express.json());
app.use(session({ secret: "supersecretkey", resave: false, saveUninitialized: false }));
const DB_FILE = "./db.json";
function loadDB() {
if (!fs.existsSync(DB_FILE)) fs.writeFileSync(DB_FILE, JSON.stringify({ links: {} }, null, 2));
return JSON.parse(fs.readFileSync(DB_FILE));
}
function saveDB(db) {
fs.writeFileSync(DB_FILE, JSON.stringify(db, null, 2));
}
// Watch registers UUID
app.post("/register", (req, res) => {
const { uuid, discordId } = req.body;
if (!uuid) return res.status(400).json({ error: "UUID required" });
const db = loadDB();
if (discordId) db.links[discordId] = uuid;
else if (!db.uuids) db.uuids = db.uuids || [];
if (!db.uuids.includes(uuid)) db.uuids.push(uuid);
saveDB(db);
res.json({ success: true });
});
// Bot requests UUID
app.get("/get-uuid", (req, res) => {
const { discordId } = req.query;
if (!discordId) return res.status(400).json({ error: "discordId required" });
const db = loadDB();
let uuid = db.links[discordId];
if (!uuid) {
uuid = uuidv4();
db.links[discordId] = uuid;
saveDB(db);
}
res.json({ uuid });
});
app.listen(PORT, () => console.log(`Backend running on http://localhost:${PORT}`));
// ===== Bot Commands =====
client.on("ready", () => console.log(`Bot logged in as ${client.user.tag}`));
client.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === "link") {
try {
const discordId = interaction.user.id;
const response = await fetch(`${BACKEND_URL}/get-uuid?discordId=${discordId}`);
const data = await response.json();
if (data.uuid) await interaction.reply({ content: `🔗 Your Diswatch link code: \`${data.uuid}\``, ephemeral: true });
else await interaction.reply({ content: "❌ Could not find or create UUID.", ephemeral: true });
} catch (err) {
console.error("Error in /link command:", err);
await interaction.reply({ content: "⚠️ Error fetching UUID.", ephemeral: true });
}
}
});
client.login(DISCORD_TOKEN);