-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (87 loc) · 2.8 KB
/
server.js
File metadata and controls
101 lines (87 loc) · 2.8 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
import express from 'express';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
import { Client, GatewayIntentBits, EmbedBuilder, ChannelType } from 'discord.js';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname));
// Routes
app.get('/suggestions', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/report', (req, res) => {
res.sendFile(path.join(__dirname, 'report.html'));
});
// Discord bot
const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]
});
bot.once('ready', () => {
console.log(`✅ Logged in as ${bot.user.tag}`);
});
async function sendToDiscord(channelId, title, fields, color) {
const channel = await bot.channels.fetch(channelId);
if (!channel || channel.type !== ChannelType.GuildText) {
throw new Error(`Channel ${channelId} is not a text channel or doesn't exist`);
}
const embed = new EmbedBuilder()
.setTitle(title)
.addFields(...fields)
.setColor(color)
.setTimestamp();
const sentMessage = await channel.send({ embeds: [embed] });
await sentMessage.react('✅');
await sentMessage.react('❌');
}
// Suggestion submission
app.post('/submit', async (req, res) => {
const { username, message } = req.body;
if (!username || !message) {
return res.status(400).send('Missing username or message');
}
try {
await sendToDiscord(
process.env.CHANNEL_ID,
'📩 New Suggestion',
[
{ name: 'Username', value: username, inline: true },
{ name: 'Suggestion', value: message }
],
0x00ff00
);
res.status(200).json({ success: true, message: 'Suggestion submitted to Discord!' });
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: 'Error submitting suggestion' });
}
});
// Report submission
app.post('/submits', async (req, res) => {
const { username, message } = req.body;
if (!username || !message) {
return res.status(400).send('Missing username or message');
}
try {
await sendToDiscord(
process.env.CHANNEL_ID2,
'📩 New Report',
[
{ name: 'Username', value: username, inline: true },
{ name: 'Report', value: message }
],
0xff0000
);
res.status(200).json({ success: true, message: 'Report submitted to Discord!' });
} catch (error) {
console.error(error);
res.status(500).json({ success: false, message: 'Error submitting report' });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`🌐 Website running on port ${PORT}`));
bot.login(process.env.TOKEN);