From 3bae55a30a75187798afcd09ddf7ea3b86ded630 Mon Sep 17 00:00:00 2001 From: dcrespo Date: Wed, 13 Jul 2022 18:01:05 +0200 Subject: [PATCH] feat: slipt messages for msg size > 2000 --- package.json | 2 +- src/lib/sendChangelogMsg.ts | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f9963d1..8c18b06 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/lib/sendChangelogMsg.ts b/src/lib/sendChangelogMsg.ts index 67d403b..d3e35a6 100644 --- a/src/lib/sendChangelogMsg.ts +++ b/src/lib/sendChangelogMsg.ts @@ -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) { @@ -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,