From 6fdfd68848d8fda84c07aaacf4c013ef6617131e Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Fri, 21 Jun 2024 11:31:30 +0530 Subject: [PATCH] fix: disable async file load and use cf again --- src/commands/slash/Main/blacklist/user.ts | 1 - src/scripts/network/helpers.ts | 7 ++- src/scripts/network/runChecks.ts | 5 +- src/scripts/network/sendMessage.ts | 62 +++++++++++++---------- src/scripts/network/storeMessageData.ts | 9 ++-- src/utils/LoadCommands.ts | 12 ++--- src/utils/Locale.ts | 14 ++--- 7 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/commands/slash/Main/blacklist/user.ts b/src/commands/slash/Main/blacklist/user.ts index e0759715..a9c7ffe9 100644 --- a/src/commands/slash/Main/blacklist/user.ts +++ b/src/commands/slash/Main/blacklist/user.ts @@ -20,7 +20,6 @@ export default class Server extends BlacklistCommand { hubInDb?.ownerId === interaction.user.id || hubInDb?.moderators.find((mod) => mod.userId === interaction.user.id) || checkIfStaff(interaction.user.id); - console.log(isStaffOrHubMod); if (!hubInDb || !isStaffOrHubMod) { await interaction.editReply({ diff --git a/src/scripts/network/helpers.ts b/src/scripts/network/helpers.ts index 26a0eb50..93361abe 100644 --- a/src/scripts/network/helpers.ts +++ b/src/scripts/network/helpers.ts @@ -13,6 +13,9 @@ import { censor } from '../../utils/Profanity.js'; import { broadcastedMessages } from '@prisma/client'; import { t } from '../../utils/Locale.js'; +export type NetworkAPIError = { error: string }; + + /** * Retrieves the content of a referred message, which can be either the message's text content or the description of its first embed. * If the referred message has no content, returns a default message indicating that the original message contains an attachment. @@ -175,6 +178,6 @@ export const sendWelcomeMsg = async (message: Message, totalServers: string, hub }; -export function isNetworkApiError(res: string | APIMessage | undefined): res is string { - return (res && typeof res === 'string') === true; +export function isNetworkApiError(res: NetworkAPIError | APIMessage | undefined): res is NetworkAPIError { + return (res && Object.hasOwn(res, 'error')) === true; } \ No newline at end of file diff --git a/src/scripts/network/runChecks.ts b/src/scripts/network/runChecks.ts index 8e406713..dde37d8d 100644 --- a/src/scripts/network/runChecks.ts +++ b/src/scripts/network/runChecks.ts @@ -117,11 +117,10 @@ export const runChecks = async ( const { settings, userData, attachmentURL } = opts; const isUserBlacklisted = userData.blacklistedFrom.some((b) => b.hubId === hubId); - if (await isCaughtSpam(message, settings, hubId)) return false; - if (containsLinks(message, settings)) message.content = replaceLinks(message.content); - // banned / blacklisted if (userData.banMeta?.reason || isUserBlacklisted) return false; + if (containsLinks(message, settings)) message.content = replaceLinks(message.content); + if (await isCaughtSpam(message, settings, hubId)) return false; // send a log to the log channel set by the hub if (hasProfanity || hasSlurs) { diff --git a/src/scripts/network/sendMessage.ts b/src/scripts/network/sendMessage.ts index a5063d9b..d04048fe 100644 --- a/src/scripts/network/sendMessage.ts +++ b/src/scripts/network/sendMessage.ts @@ -1,7 +1,8 @@ import { APIEmbed, APIMessage, WebhookMessageCreateOptions, isJSONEncodable } from 'discord.js'; // import { isDevBuild } from '../../utils/Constants.js'; -import { encryptMessage } from '../../utils/Utils.js'; -import { isNetworkApiError } from './helpers.js'; +// import { encryptMessage } from '../../utils/Utils.js'; +import { NetworkAPIError, isNetworkApiError } from './helpers.js'; +import { encryptMessage, wait } from '../../utils/Utils.js'; const { INTERCHAT_API_URL1, INTERCHAT_API_URL2 } = process.env; let primaryUrl = INTERCHAT_API_URL1 ?? INTERCHAT_API_URL2; @@ -12,57 +13,66 @@ const switchUrl = (currentUrl: string) => { const sendMessage = async ( webhookUrl: string, - message: WebhookMessageCreateOptions, + data: WebhookMessageCreateOptions, tries = 0, - encrypt = true, -): Promise => { - // No need for external apis in development mode FIXME + // eslint-disable-next-line @typescript-eslint/no-unused-vars + encrypt?: boolean, +): Promise => { + encrypt = false; // FIXME since im using cf workers its disabeld + // No need for external apis in development mode + // FIXME: uncomment dis // if (isDevBuild) { // const webhook = new WebhookClient({ url: webhookUrl }); // return await webhook.send(message); // } - if (!process.env.NETWORK_ENCRYPT_KEY) throw new Error('Missing encryption key for network.'); - - const encryptKey = Buffer.from(process.env.NETWORK_ENCRYPT_KEY, 'base64'); const networkKey = process.env.NETWORK_API_KEY; if (!networkKey || !primaryUrl) { throw new Error('NETWORK_API_KEY or INTERCHAT_API_URL(s) env variables missing.'); } - const firstEmbed = message.embeds?.at(0); - const embed = isJSONEncodable(firstEmbed) ? firstEmbed.toJSON() : firstEmbed; - - const content = message.content; + // TODO: Encryption stuff, doesn't work in cf workers :( + let embed: APIEmbed = {}; if (encrypt) { - if (content) message.content = encryptMessage(content, encryptKey); - if (embed?.description) { - // FIXME: message.embeds[0] might not work if its json encodable, check! - (message.embeds as APIEmbed[])![0].description = encryptMessage( - embed.description, - encryptKey, - ); + if (!process.env.NETWORK_ENCRYPT_KEY) throw new Error('Missing encryption key for network.'); + + const firstEmbed = data.embeds?.at(0); + + const encryptKey = Buffer.from(process.env.NETWORK_ENCRYPT_KEY, 'base64'); + const content = data.content; + if (encrypt) { + if (content) { + data.content = encryptMessage(content, encryptKey); + } + else if (firstEmbed) { + embed = isJSONEncodable(firstEmbed) ? firstEmbed.toJSON() : firstEmbed; + if (embed.description) { + embed.description = encryptMessage(embed.description, encryptKey); + } + } } } + // console.log(data); const res = await fetch(primaryUrl, { method: 'POST', - body: JSON.stringify({ webhookUrl, data: message }), + body: JSON.stringify({ webhookUrl, data: { ...data, ...embed } }), headers: { authorization: networkKey, - 'x-webhook-url': webhookUrl, 'Content-Type': 'application/json', }, }); - const data = (await res.json()) as string | APIMessage | undefined; + const resJson = (await res.json()) as NetworkAPIError | APIMessage | undefined; - if (isNetworkApiError(data) && tries <= 2) { + if (isNetworkApiError(resJson) && tries <= 2) { + console.log('here', tries); + await wait(3000); primaryUrl = switchUrl(primaryUrl); - return await sendMessage(webhookUrl, message, tries++, !encrypt); + return await sendMessage(webhookUrl, data, tries + 1, false); } - return data; + return resJson; }; export default sendMessage; diff --git a/src/scripts/network/storeMessageData.ts b/src/scripts/network/storeMessageData.ts index 510cea3e..924047b1 100644 --- a/src/scripts/network/storeMessageData.ts +++ b/src/scripts/network/storeMessageData.ts @@ -2,10 +2,11 @@ import db from '../../utils/Db.js'; import { originalMessages } from '@prisma/client'; import { APIMessage, Message } from 'discord.js'; import { messageTimestamps, modifyConnections } from '../../utils/ConnectedList.js'; -import { isNetworkApiError } from './helpers.js'; +import { NetworkAPIError, isNetworkApiError } from './helpers.js'; +import Logger from '../../utils/Logger.js'; export interface NetworkWebhookSendResult { - messageOrError: APIMessage | string; + messageOrError: APIMessage | NetworkAPIError; webhookURL: string; } @@ -33,8 +34,8 @@ export default async ( createdAt: new Date(messageOrError.timestamp), }); } - else if (validErrors.some((e) => messageOrError?.includes(e))) { - console.log(messageOrError); + else if (validErrors.some((e) => messageOrError.error?.includes(e))) { + Logger.info('%O', messageOrError); // TODO Remove dis invalidWebhookURLs.push(webhookURL); } }); diff --git a/src/utils/LoadCommands.ts b/src/utils/LoadCommands.ts index be8e6270..d13db0c1 100644 --- a/src/utils/LoadCommands.ts +++ b/src/utils/LoadCommands.ts @@ -1,5 +1,5 @@ import { join, dirname } from 'path'; -import { access, constants, readdir, stat } from 'fs/promises'; +import { access, constants, readdirSync, statSync } from 'fs'; import { fileURLToPath } from 'url'; import BaseCommand, { commandsMap } from '../core/BaseCommand.js'; @@ -11,17 +11,17 @@ const __dirname = dirname(fileURLToPath(import.meta.url)); */ const loadCommandFiles = async (commandDir = join(__dirname, '..', 'commands')) => { const importPrefix = process.platform === 'win32' ? 'file://' : ''; - const files = await readdir(commandDir); + const files = readdirSync(commandDir); for (const file of files) { const filePath = join(commandDir, file); - const stats = await stat(filePath); + const stats = statSync(filePath); // If the item is a directory, recursively read its files if (stats.isDirectory()) { await loadCommandFiles(filePath); } - else if (file.endsWith('.js')) { + else if (file.endsWith('.js') && file !== 'BaseCommand.js') { const imported = await import(importPrefix + filePath); const command: BaseCommand = new imported.default(); @@ -33,9 +33,9 @@ const loadCommandFiles = async (commandDir = join(__dirname, '..', 'commands')) // if the command has subcommands, add them to the parent command's subcommands map else { const subcommandFile = join(commandDir, '.', 'index.js'); - if (!(await stat(subcommandFile)).isFile()) return; + if (!statSync(subcommandFile).isFile()) return; - await access(subcommandFile, constants.F_OK).catch((err) => { + access(subcommandFile, constants.F_OK, (err) => { if (err || file === 'index.js') return; // get the parent command class from the subcommand diff --git a/src/utils/Locale.ts b/src/utils/Locale.ts index ba671a1f..272ed9eb 100644 --- a/src/utils/Locale.ts +++ b/src/utils/Locale.ts @@ -1,5 +1,5 @@ import Logger from './Logger.js'; -import fs from 'fs/promises'; +import fs from 'fs'; import path from 'path'; import yaml from 'js-yaml'; @@ -41,20 +41,20 @@ export interface tParams { locale?: supportedLocaleCodes; } -export const loadLocales = async (localesDirectory: string) => { - const files = await fs.readdir(localesDirectory); +export const loadLocales = (localesDirectory: string) => { + const files = fs.readdirSync(localesDirectory); - files.forEach(async (file: string, index) => { + files.forEach((file: string) => { const filePath = path.join(localesDirectory, file); const localeKey = path.basename(file, '.yml'); - const content = await fs.readFile(filePath, 'utf8'); + const content = fs.readFileSync(filePath, 'utf8'); const parsedContent = yaml.load(content); localesMap.set(localeKey, parsedContent); - - if (index + 1 === files.length) Logger.info(`${localesMap.size} Locales loaded successfully.`); }); + + Logger.info(`${localesMap.size} Locales loaded successfully.`); }; /** Get the translated text with variable replacement */