Skip to content

Commit

Permalink
split messages to avoid 2k limit (wow xD)
Browse files Browse the repository at this point in the history
  • Loading branch information
Niceygy committed Feb 4, 2024
1 parent cf7cc84 commit 7ffa185
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,44 @@ async function cron(env, sendMsg) {
let currentDate = new Date();

//MESSAGE ########################################################################
var message = "# Downloads as of " + currentDate.toUTCString() + "\n\n" + repos.map(repo => `Repo: ${repo.name}, Count: ${repo.count}\n`).join("");
var message = message + "\n\nTotal downloads: " + Dtotal;
var message = message + "\n\n# Page visits as of " + currentDate.toUTCString() + "\n\n" + pages.map(page => `Page: ${page.name}, Count: ${page.count}\n`).join("");
var message = message + "\n\nTotal page visits: " + Ptotal;
console.log("Message generated. Sending to Discord...");
console.log(message);

if (sendMsg == false) {
await fetch(webhookURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content: message,
}),
})
.then((response) => console.log(response))
var message1 = "# Downloads as of " + currentDate.toUTCString() + "\n\n" + repos.map((repo) => `Repo: ${repo.name}, Count: ${repo.count}
`).join("");
var message1 = message1 + "\n\nTotal downloads: " + Dtotal;
var message2 = "# Page visits as of " + currentDate.toUTCString() + "\n\n" + pages.map((page) => `Page: ${page.name}, Count: ${page.count}
`).join("");
var message = message2 + "\n\nTotal page visits: " + Ptotal;
console.log("Message generated. Sending to Discord...");
console.log(message);
if (sendMsg == false) {
console.log("Sending data to " + env.url)
//split messages to avoid limit
await fetch(env.url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
content: message1
})
}).then((response) => {
console.log('Status:', response.status);
// return response.text();
})
.then((body) => console.log('Body:', body))
.catch((error) => console.error(error));
await fetch(env.url, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
content: message2
})
}).then((response) => {
console.log('Status:', response.status);
// return response.text();
})
.then((body) => console.log('Body:', body))
.catch((error) => console.error(error));
console.log("Message sent to Discord");
return "Complete! Message sent.";
Expand Down

0 comments on commit 7ffa185

Please sign in to comment.