-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
116 lines (95 loc) · 3.6 KB
/
server.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
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
108
109
110
111
112
113
114
115
116
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const { WebhookClient } = require('discord.js');
const path = require('path');
require('dotenv').config();
const app = express();
// Rate limiting için değişkenler
const webhookCooldowns = new Map();
const COOLDOWN_SECONDS = 5;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Static dosya yolları - Basitleştirilmiş hali
app.use(express.static('public'));
app.use('/assets', express.static(path.join(__dirname, 'public/assets')));
// View engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Error handler
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Bir şeyler ters gitti!' });
});
// Ana sayfa
app.get('/', (req, res) => {
try {
res.render('index');
} catch (error) {
console.error('Render hatası:', error);
res.status(500).send('Sayfa yüklenirken bir hata oluştu');
}
});
// Webhook bilgilerini getir
app.get('/api/webhook-info', async (req, res) => {
try {
const { url } = req.query;
if (!url) {
return res.status(400).json({ error: 'Webhook URL required' });
}
// Webhook bilgilerini al
const response = await axios.get(url);
if (!response.data) {
return res.status(400).json({ error: 'Geçersiz webhook URL' });
}
// Sadece gerekli bilgileri gönder
res.json({
name: response.data.name,
avatar_url: response.data.avatar ? `https://cdn.discordapp.com/avatars/${response.data.id}/${response.data.avatar}.png` : 'https://cdn.discordapp.com/embed/avatars/0.png',
channel_id: response.data.channel_id,
guild_id: response.data.guild_id
});
} catch (error) {
console.error('Webhook bilgi hatası:', error);
res.status(500).json({ error: 'Webhook bilgileri alınamadı' });
}
});
// Webhook'a embed gönder
app.post('/api/send-webhook', async (req, res) => {
try {
const { url, embedData } = req.body;
if (!url) {
return res.status(400).json({ error: 'Webhook URL gerekli' });
}
// Cooldown kontrolü
const now = Date.now();
const lastUsed = webhookCooldowns.get(url);
if (lastUsed) {
const timeElapsed = now - lastUsed;
const timeRemaining = (COOLDOWN_SECONDS * 1000) - timeElapsed;
if (timeRemaining > 0) {
return res.status(429).json({
error: 'Please wait 5 seconds - Rate limit exceeded',
message: `Lütfen ${Math.ceil(timeRemaining / 1000)} saniye bekleyin`,
retryAfter: timeRemaining
});
}
}
// Webhook gönderme işlemi
const webhookClient = new WebhookClient({ url });
await webhookClient.send(embedData);
// Cooldown güncelleme
webhookCooldowns.set(url, now);
setTimeout(() => webhookCooldowns.delete(url), COOLDOWN_SECONDS * 1000);
res.json({ success: true });
} catch (error) {
console.error('Webhook gönderme hatası:', error);
res.status(500).json({ error: error.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server http://localhost:${PORT} adresinde çalışıyor`);
});