Skip to content

Commit

Permalink
feat: slipt messages for msg size > 2000
Browse files Browse the repository at this point in the history
  • Loading branch information
tsyl-dcrespo committed Jul 13, 2022
1 parent 9ff7e94 commit 3bae55a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
"module": "build/module/index.js",
"repository": "https://github.com/YOUR_GITHUB_USER_NAME/git-up-to-date",
"repository": "https://github.com/dicren/git-up-to-date",
"license": "MIT",
"keywords": [],
"scripts": {
Expand Down
33 changes: 29 additions & 4 deletions src/lib/sendChangelogMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import { RequestOptions } from "https";

import { changelogData } from "../types";

const WEBHOOK = process.env.WEBHOOK || "";
const PROJECT = process.env.PROJECT || "";
const WEBHOOK =
process.env.WEBHOOK ||
"https://discord.com/api/webhooks/974608679846428712/bnAYrbgCkrQ4Gh-YzYoXPIh0T_HJJruBngz-BQbTa-4dR4L3Uzoo8D2-3WUHAbHhvfkz";
const PROJECT = process.env.PROJECT || "Karonte";

export function sendChangelogToChannel(changelog: changelogData) {
export async function sendChangelogToChannel(changelog: changelogData) {
if (!WEBHOOK) throw new Error("WEBHOOK is not defined");
const msg = generateMsg(changelog);
sendMessage(msg);
const msgs = splitMessages(msg);
const index = 0;
console.log(msgs);
for (const msgSplitted of msgs) {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(`Send msg nº ${index} charcount: ${msgSplitted.length}`);
sendMessage(msgSplitted);
}
}

function generateMsg(changelog: changelogData) {
Expand Down Expand Up @@ -53,6 +62,22 @@ function getEmojiSection(section: string) {
}
}

function splitMessages(msg: string) {
const linebreaks = msg.split("\n");
const allmsgs = [];
let buildedmsg = "";
for (const line of linebreaks) {
if (buildedmsg.length + line.length > 2000) {
allmsgs.push(buildedmsg);
buildedmsg = line;
} else {
buildedmsg += "\n" + line;
}
}
allmsgs.push(buildedmsg);
return allmsgs;
}

function sendMessage(message: string) {
const post_data = JSON.stringify({
content: message,
Expand Down

0 comments on commit 3bae55a

Please sign in to comment.