Skip to content

Commit

Permalink
idle connection thing
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Jun 25, 2024
1 parent 7995771 commit de77c61
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
36 changes: 18 additions & 18 deletions src/scripts/network/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -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<NetworkAPIError | APIMessage | undefined> => {
// 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.');
Expand Down Expand Up @@ -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;
4 changes: 2 additions & 2 deletions src/scripts/tasks/pauseIdleConnections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default async (manager: ClusterManager) => {
},
});

if (!connections) return;
if (connections?.length === 0) return;

const reconnectButtonArr: {
channelId: Snowflake;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit de77c61

Please sign in to comment.