mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-10 17:26:17 +00:00
fix: Convert serverless function to JavaScript for Vercel
This commit is contained in:
@@ -1,62 +1,62 @@
|
|||||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
const axios = require('axios');
|
||||||
|
|
||||||
// In-memory rate limiting for error reporting
|
// In-memory rate limiting for error reporting
|
||||||
const rateLimitMap = new Map<string, { count: number; resetTime: number }>()
|
const rateLimitMap = new Map();
|
||||||
const RATE_LIMIT_WINDOW_MS = 60 * 1000 // 1 minute
|
const RATE_LIMIT_WINDOW_MS = 60 * 1000; // 1 minute
|
||||||
const RATE_LIMIT_MAX_REQUESTS = 10
|
const RATE_LIMIT_MAX_REQUESTS = 10;
|
||||||
|
|
||||||
function isRateLimited(ip: string): boolean {
|
function isRateLimited(ip) {
|
||||||
const now = Date.now()
|
const now = Date.now();
|
||||||
const record = rateLimitMap.get(ip)
|
const record = rateLimitMap.get(ip);
|
||||||
|
|
||||||
if (!record || now > record.resetTime) {
|
if (!record || now > record.resetTime) {
|
||||||
rateLimitMap.set(ip, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS })
|
rateLimitMap.set(ip, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS });
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (record.count >= RATE_LIMIT_MAX_REQUESTS) {
|
if (record.count >= RATE_LIMIT_MAX_REQUESTS) {
|
||||||
return true
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
record.count++
|
record.count++;
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vercel serverless handler
|
// Vercel serverless handler
|
||||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
module.exports = async function handler(req, res) {
|
||||||
// CORS headers
|
// CORS headers
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*')
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS')
|
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
|
||||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||||
|
|
||||||
// Handle preflight
|
// Handle preflight
|
||||||
if (req.method === 'OPTIONS') {
|
if (req.method === 'OPTIONS') {
|
||||||
return res.status(200).end()
|
return res.status(200).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only POST allowed
|
// Only POST allowed
|
||||||
if (req.method !== 'POST') {
|
if (req.method !== 'POST') {
|
||||||
return res.status(405).json({ error: 'Method not allowed' })
|
return res.status(405).json({ error: 'Method not allowed' });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Rate limiting
|
// Rate limiting
|
||||||
const ip = (req.headers['x-forwarded-for'] as string)?.split(',')[0]?.trim() || 'unknown'
|
const ip = req.headers['x-forwarded-for']?.split(',')[0]?.trim() || 'unknown';
|
||||||
if (isRateLimited(ip)) {
|
if (isRateLimited(ip)) {
|
||||||
return res.status(429).json({ error: 'Rate limit exceeded' })
|
return res.status(429).json({ error: 'Rate limit exceeded' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Discord webhook URL
|
// Check Discord webhook URL
|
||||||
const webhookUrl = process.env.DISCORD_ERROR_WEBHOOK_URL
|
const webhookUrl = process.env.DISCORD_ERROR_WEBHOOK_URL;
|
||||||
if (!webhookUrl) {
|
if (!webhookUrl) {
|
||||||
console.error('[ErrorReporting] DISCORD_ERROR_WEBHOOK_URL not configured')
|
console.error('[ErrorReporting] DISCORD_ERROR_WEBHOOK_URL not configured');
|
||||||
return res.status(503).json({ error: 'Error reporting service unavailable' })
|
return res.status(503).json({ error: 'Error reporting service unavailable' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate payload
|
// Validate payload
|
||||||
const payload = req.body
|
const payload = req.body;
|
||||||
if (!payload?.error) {
|
if (!payload?.error) {
|
||||||
return res.status(400).json({ error: 'Invalid payload: missing error field' })
|
return res.status(400).json({ error: 'Invalid payload: missing error field' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build Discord embed
|
// Build Discord embed
|
||||||
@@ -71,33 +71,32 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|||||||
],
|
],
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
footer: { text: 'Community Error Reporting' }
|
footer: { text: 'Community Error Reporting' }
|
||||||
}
|
};
|
||||||
|
|
||||||
if (payload.stack) {
|
if (payload.stack) {
|
||||||
const stackLines = String(payload.stack).split('\n').slice(0, 15).join('\n')
|
const stackLines = String(payload.stack).split('\n').slice(0, 15).join('\n');
|
||||||
embed.fields.push({
|
embed.fields.push({
|
||||||
name: 'Stack Trace',
|
name: 'Stack Trace',
|
||||||
value: `\`\`\`\n${stackLines.slice(0, 1000)}\n\`\`\``,
|
value: `\`\`\`\n${stackLines.slice(0, 1000)}\n\`\`\``,
|
||||||
inline: false
|
inline: false
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send to Discord
|
// Send to Discord
|
||||||
const axios = (await import('axios')).default
|
|
||||||
await axios.post(webhookUrl, {
|
await axios.post(webhookUrl, {
|
||||||
username: 'Microsoft Rewards Bot',
|
username: 'Microsoft Rewards Bot',
|
||||||
avatar_url: 'https://raw.githubusercontent.com/LightZirconite/Microsoft-Rewards-Bot/refs/heads/main/assets/logo.png',
|
avatar_url: 'https://raw.githubusercontent.com/LightZirconite/Microsoft-Rewards-Bot/refs/heads/main/assets/logo.png',
|
||||||
embeds: [embed]
|
embeds: [embed]
|
||||||
}, { timeout: 10000 })
|
}, { timeout: 10000 });
|
||||||
|
|
||||||
console.log('[ErrorReporting] Report sent successfully')
|
console.log('[ErrorReporting] Report sent successfully');
|
||||||
return res.json({ success: true, message: 'Error report received' })
|
return res.json({ success: true, message: 'Error report received' });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[ErrorReporting] Failed:', error)
|
console.error('[ErrorReporting] Failed:', error);
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: 'Failed to send error report',
|
error: 'Failed to send error report',
|
||||||
message: error instanceof Error ? error.message : 'Unknown error'
|
message: error.message || 'Unknown error'
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
Reference in New Issue
Block a user