From de77c61c6f7296b2c57c04af4eacb3c0d2eb91f1 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Tue, 25 Jun 2024 10:13:29 +0530 Subject: [PATCH] idle connection thing --- src/scripts/network/sendMessage.ts | 36 +++++++++++------------ src/scripts/tasks/pauseIdleConnections.ts | 4 +-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/scripts/network/sendMessage.ts b/src/scripts/network/sendMessage.ts index 2b0dfd4b..a43a06b2 100644 --- a/src/scripts/network/sendMessage.ts +++ b/src/scripts/network/sendMessage.ts @@ -1,30 +1,33 @@ -import { APIEmbed, APIMessage, WebhookClient, WebhookMessageCreateOptions, isJSONEncodable } from 'discord.js'; -// import { isDevBuild } from '../../utils/Constants.js'; -// import { encryptMessage } from '../../utils/Utils.js'; +import { + APIEmbed, + APIMessage, + WebhookClient, + WebhookMessageCreateOptions, + isJSONEncodable, +} from 'discord.js'; import { NetworkAPIError, isNetworkApiError } from './helpers.js'; import { encryptMessage, wait } from '../../utils/Utils.js'; -import { isDevBuild } from '../../utils/Constants.js'; + +export default async (webhookUrl: string, data: WebhookMessageCreateOptions) => { + const webhook = new WebhookClient({ url: webhookUrl }); + return await webhook.send(data); +}; const { INTERCHAT_API_URL1, INTERCHAT_API_URL2 } = process.env; -let primaryUrl = INTERCHAT_API_URL1 ?? INTERCHAT_API_URL2; +const urls = [INTERCHAT_API_URL1, INTERCHAT_API_URL2]; +let primaryUrl = urls[0]; const switchUrl = (currentUrl: string) => { - return currentUrl === INTERCHAT_API_URL1 ? INTERCHAT_API_URL2 : INTERCHAT_API_URL1; + if (currentUrl === urls[urls.length - 1]) return urls[0] ?? currentUrl; + else return urls[urls.indexOf(currentUrl) + 1] ?? currentUrl; }; - -const sendMessage = async ( +export const specialSendMessage = async ( webhookUrl: string, data: WebhookMessageCreateOptions, tries = 0, // eslint-disable-next-line @typescript-eslint/no-unused-vars encrypt = true, ): Promise => { - // No need for external apis in development mode - if (isDevBuild) { - const webhook = new WebhookClient({ url: webhookUrl }); - return await webhook.send(data); - } - const networkKey = process.env.NETWORK_API_KEY; if (!networkKey || !primaryUrl) { throw new Error('NETWORK_API_KEY or INTERCHAT_API_URL(s) env variables missing.'); @@ -65,13 +68,10 @@ const sendMessage = async ( const resJson = (await res.json()) as NetworkAPIError | APIMessage | undefined; if (isNetworkApiError(resJson) && tries <= 5) { - console.log('here', tries); await wait(3000); primaryUrl = switchUrl(primaryUrl); - return await sendMessage(webhookUrl, data, tries + 1, false); + return await specialSendMessage(webhookUrl, data, tries + 1, false); } return resJson; }; - -export default sendMessage; diff --git a/src/scripts/tasks/pauseIdleConnections.ts b/src/scripts/tasks/pauseIdleConnections.ts index 8e8c4b49..f2a5332b 100644 --- a/src/scripts/tasks/pauseIdleConnections.ts +++ b/src/scripts/tasks/pauseIdleConnections.ts @@ -17,7 +17,7 @@ export default async (manager: ClusterManager) => { }, }); - if (!connections) return; + if (connections?.length === 0) return; const reconnectButtonArr: { channelId: Snowflake; @@ -45,7 +45,7 @@ export default async (manager: ClusterManager) => { const embed = simpleEmbed( stripIndents` ### ${emojis.timeout} Paused Due to Inactivity - Connection to this hub has been stopped. **Click the button** below to resume chatting (or alternatively, \`/connection\`). + Connection to this hub has been stopped because no messages were sent for past day. **Click the button** below to resume chatting (or alternatively, \`/connection\`). `, ).toJSON();