Workers Sending User to 405 Error
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method === 'POST') {
const ip = request.headers.get('cf-connecting-ip');
const lastSubmitted = await GROUP_APP.get(ip);
const currentTime = new Date().getTime();
// Rate limiting: check if the IP has submitted within the last 3 hours
if (lastSubmitted && (currentTime - lastSubmitted) < 10800000) { // 3 hours in milliseconds
return new Response('Rate Limited: You can only submit one application every 3 hours.', { status: 429 });
}
// Parse the form data
const formData = await request.formData();
const discordUsername = formData.get('discord-username');
const discordId = formData.get('discord-id');
const groupCategory = formData.get('group-category');
const groupName = formData.get('group-name');
const groupDescription = formData.get('group-description');
const interestReason = formData.get('interest-reason');
const experienceDetail = formData.get('previous-experience');
const planForGroup = formData.get('foster-plan');
const conflictResolution = formData.get('conflict-resolution');
const teamHeads = formData.get('initial-heads');
const finalAgreement = formData.get('final-agreement'); // This will be 'on' if checked
// Construct the Discord Embed
const discordData = {
embeds: [{
title: "New Group Application",
color: 3447003, // Blue color
author: {
name: "#############",
icon_url: "https://i.imgur.com/#####"
},
fields: [
{ name: "Discord Username", value: discordUsername, inline: true },
{ name: "Discord ID", value: discordId, inline: true },
{ name: "Desired Group Category", value: groupCategory, inline: false },
{ name: "Proposed Group Name", value: groupName, inline: false },
{ name: "Description of Group", value: groupDescription, inline: false },
{ name: "Interest Reason", value: interestReason, inline: false },
{ name: "Experience Detail", value: experienceDetail, inline: false },
{ name: "Plan for Group", value: planForGroup, inline: false },
{ name: "Conflict Resolution", value: conflictResolution, inline: false },
{ name: "Team Heads", value: teamHeads, inline: false },
{ name: "Final Agreement", value: finalAgreement ? 'Yes' : 'No', inline: false }
],
footer: {
text: "####################"
}
}]
};
// Send to Discord webhook
const discordResponse = await fetch('https://discord.com/api/webhooks/###############################', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(discordData)
});
if (discordResponse.ok) {
// Update the submission time for this IP
await GROUP_APP.put(ip, currentTime.toString(), { expirationTtl: 10800 }); // 3 hours
// Redirect to the success page
return Response.redirect('https://#############.com/application-success', 303);
} else {
return new Response('Failed to send message to Discord', { status: 500 });
}
} else {
return new Response('Method not allowed', { status: 405 });
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method === 'POST') {
const ip = request.headers.get('cf-connecting-ip');
const lastSubmitted = await GROUP_APP.get(ip);
const currentTime = new Date().getTime();
// Rate limiting: check if the IP has submitted within the last 3 hours
if (lastSubmitted && (currentTime - lastSubmitted) < 10800000) { // 3 hours in milliseconds
return new Response('Rate Limited: You can only submit one application every 3 hours.', { status: 429 });
}
// Parse the form data
const formData = await request.formData();
const discordUsername = formData.get('discord-username');
const discordId = formData.get('discord-id');
const groupCategory = formData.get('group-category');
const groupName = formData.get('group-name');
const groupDescription = formData.get('group-description');
const interestReason = formData.get('interest-reason');
const experienceDetail = formData.get('previous-experience');
const planForGroup = formData.get('foster-plan');
const conflictResolution = formData.get('conflict-resolution');
const teamHeads = formData.get('initial-heads');
const finalAgreement = formData.get('final-agreement'); // This will be 'on' if checked
// Construct the Discord Embed
const discordData = {
embeds: [{
title: "New Group Application",
color: 3447003, // Blue color
author: {
name: "#############",
icon_url: "https://i.imgur.com/#####"
},
fields: [
{ name: "Discord Username", value: discordUsername, inline: true },
{ name: "Discord ID", value: discordId, inline: true },
{ name: "Desired Group Category", value: groupCategory, inline: false },
{ name: "Proposed Group Name", value: groupName, inline: false },
{ name: "Description of Group", value: groupDescription, inline: false },
{ name: "Interest Reason", value: interestReason, inline: false },
{ name: "Experience Detail", value: experienceDetail, inline: false },
{ name: "Plan for Group", value: planForGroup, inline: false },
{ name: "Conflict Resolution", value: conflictResolution, inline: false },
{ name: "Team Heads", value: teamHeads, inline: false },
{ name: "Final Agreement", value: finalAgreement ? 'Yes' : 'No', inline: false }
],
footer: {
text: "####################"
}
}]
};
// Send to Discord webhook
const discordResponse = await fetch('https://discord.com/api/webhooks/###############################', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(discordData)
});
if (discordResponse.ok) {
// Update the submission time for this IP
await GROUP_APP.put(ip, currentTime.toString(), { expirationTtl: 10800 }); // 3 hours
// Redirect to the success page
return Response.redirect('https://#############.com/application-success', 303);
} else {
return new Response('Failed to send message to Discord', { status: 500 });
}
} else {
return new Response('Method not allowed', { status: 405 });
}
}
0 Replies