From 41323c49ae2ea2de43e0018bd36bbcf1a9a12144 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 6 Sep 2024 23:24:22 -0500 Subject: [PATCH] Fix memory leak (#47) * a * vulnerability? * removeundici --- package.json | 1 - src/commands/emoji.ts | 1 - src/commands/eval.ts | 2 +- src/commands/giveaway.ts | 20 +++++++++++--------- src/commands/tags.ts | 4 +--- src/commands/time.ts | 1 - src/events/tagDetect.ts | 3 +-- src/index.ts | 1 - src/utils/codeUpload.ts | 21 --------------------- src/utils/cutText.ts | 6 ------ src/utils/index.ts | 37 ++++++++++++++++++++++++++++++++----- src/utils/randomStatus.ts | 27 --------------------------- src/utils/require.ts | 2 -- yarn.lock | 26 -------------------------- 14 files changed, 46 insertions(+), 106 deletions(-) delete mode 100644 src/utils/codeUpload.ts delete mode 100644 src/utils/cutText.ts delete mode 100644 src/utils/randomStatus.ts delete mode 100644 src/utils/require.ts diff --git a/package.json b/package.json index ce175ce..1c11fb9 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "string-similarity": "4.0.4", "tesseract.js": "^5.0.4", "trie-search": "1.4.1", - "undici": "5.22.0", "winston": "3.8.2" }, "devDependencies": { diff --git a/src/commands/emoji.ts b/src/commands/emoji.ts index da64c5b..82526bb 100644 --- a/src/commands/emoji.ts +++ b/src/commands/emoji.ts @@ -10,7 +10,6 @@ import { Snowflake, TextChannel, } from "discord.js"; -import { fetch } from "undici"; import { cooldown, publish } from "#plugins"; import { Resolver, slashCommand } from "#utils"; diff --git a/src/commands/eval.ts b/src/commands/eval.ts index a108f70..54a2cd0 100644 --- a/src/commands/eval.ts +++ b/src/commands/eval.ts @@ -19,7 +19,7 @@ export default commandModule({ } const { channel, guild, client, user, member, message: msg } = ctx; if ( - ["TOKEN", "process..env", "token"].some((e) => code.includes(e)) && + ["TOKEN", "process.env", "token"].some((e) => code.includes(e)) && ctx.user.id !== "697795666373640213" ) return ctx.message.react("❌"); diff --git a/src/commands/giveaway.ts b/src/commands/giveaway.ts index 7008be5..4de5cb5 100644 --- a/src/commands/giveaway.ts +++ b/src/commands/giveaway.ts @@ -1,5 +1,5 @@ import { commandModule, CommandType, scheduledTask } from "@sern/handler"; -import { publish } from "#plugins"; +import { ownerOnly, publish } from "#plugins"; import { ApplicationCommandOptionType, EmbedBuilder } from "discord.js"; import { db } from "../utils/db.js"; import { add, addDays, addHours, addMinutes, addSeconds } from "date-fns" @@ -8,13 +8,13 @@ import { Timestamp } from "#utils"; export default commandModule({ type: CommandType.Slash, description: "Start a giveaway involving users who react to the embed", - plugins: [publish()], + plugins: [publish(), ownerOnly()], options: [ { - name: "item", - description: "The item that will be given away", - type: ApplicationCommandOptionType.String, - required: true + name: "item", + description: "The item that will be given away", + type: ApplicationCommandOptionType.String, + required: true }, { name: "time", @@ -25,7 +25,7 @@ export default commandModule({ ], execute: async (ctx, { deps }) => { const item = ctx.options.getString("item") - const timeLeftString = ctx.options.getString("time") + const timeLeftString = ctx.options.getString("time", true) let timeUnit1 let timeLeft1 @@ -117,7 +117,8 @@ export default commandModule({ }).then(embedMessage => { embedMessage.react("🎉") - setInterval(() => { + //checks if author reacted to itself + const selfReactionInterval = setInterval(() => { const userReactions = embedMessage.reactions.cache.filter(reaction => reaction.users.cache.has(ctx.userId)); for (const reaction of userReactions.values()) { @@ -157,9 +158,10 @@ export default commandModule({ embedMessage.edit({embeds: [embed]}) } + clearInterval(selfReactionInterval) }, intervalTime) }) db.prepare(`DELETE FROM entrees`).run() } -}) \ No newline at end of file +}) diff --git a/src/commands/tags.ts b/src/commands/tags.ts index 6676ac2..4b0bff6 100644 --- a/src/commands/tags.ts +++ b/src/commands/tags.ts @@ -6,12 +6,10 @@ import { TextInputStyle, } from "discord.js"; import { existsSync, writeFileSync } from "fs"; -import { createRequire } from "module"; import { Evo, Seren, TagList } from "#constants"; import { ownerOnly, publish } from "#plugins"; import type { TagData } from "typings"; -import { slashCommand } from "#utils"; -const require = createRequire(import.meta.url); +import { slashCommand, require } from "#utils"; export default slashCommand({ description: "Edit tags", diff --git a/src/commands/time.ts b/src/commands/time.ts index a409a42..128156e 100644 --- a/src/commands/time.ts +++ b/src/commands/time.ts @@ -1,6 +1,5 @@ import { ApplicationCommandOptionType, GuildMember } from "discord.js"; import { publish } from "#plugins"; -import { fetch } from "undici"; import { readFileSync } from "fs"; import { slashCommand } from "#utils"; diff --git a/src/events/tagDetect.ts b/src/events/tagDetect.ts index ef70338..9e55c47 100644 --- a/src/events/tagDetect.ts +++ b/src/events/tagDetect.ts @@ -1,10 +1,9 @@ import { eventModule, EventType } from "@sern/handler"; import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, Message } from "discord.js"; -import { createRequire } from "module"; import { FuzzyMatcher } from "../utils/FuzzyMatcher.js"; import type { TagData, TagMessage } from "typings"; import { TagList } from "#constants"; -const require = createRequire(import.meta.url); +import { require } from '#utils' const file: TagData[] = require(TagList); export default eventModule({ diff --git a/src/index.ts b/src/index.ts index 981134c..4838328 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ import "dotenv/config"; -import * as config from './config.js' import { Client, GatewayIntentBits, Partials } from "discord.js"; import { Sern, makeDependencies, Service } from "@sern/handler"; import { SernLogger } from "#utils"; diff --git a/src/utils/codeUpload.ts b/src/utils/codeUpload.ts deleted file mode 100644 index da144ad..0000000 --- a/src/utils/codeUpload.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { fetch } from "undici"; - -export async function upload(code: string, name?: string) { - const response = await fetch("https://sourceb.in/api/bins", { - body: JSON.stringify({ - title: "Code", - description: "Because I am lazy", - files: [{ name, content: code, languageId: 378 }], - }), - method: "POST", - headers: { - "Content-Type": "application/json", - }, - }); - const data = (await response.json()) as PostData; - return ``; -} - -interface PostData { - key: string; -} diff --git a/src/utils/cutText.ts b/src/utils/cutText.ts deleted file mode 100644 index 1c20d7c..0000000 --- a/src/utils/cutText.ts +++ /dev/null @@ -1,6 +0,0 @@ -export function cutText(text: string) { - if (text.length > 100) { - return text.slice(0, 97) + "..."; - } - return text; -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 6a62dcc..afb79af 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,13 +1,40 @@ +import { createRequire } from "node:module"; + export * from "./FuzzyMatcher.js"; export * from "./Paginator.js"; export * from "./Resolver.js"; export * from "./TicTacToe.js"; export * from "./Timestamp.js"; export * from "./pagination.js"; -export * from "./randomStatus.js"; -export * from "./codeUpload.js"; export * from "./Logger.js"; export * from "./composable/slashCommand.js"; -export * from "./require.js"; -export * from "./cutText.js"; -//export * from './SyncCommands.js'; + +export const require = createRequire(import.meta.url); + +export function cutText(text: string) { + if (text.length > 100) { + return text.slice(0, 97) + "..."; + } + return text; +} + + +export async function upload(code: string, name?: string) { + const response = await fetch("https://sourceb.in/api/bins", { + body: JSON.stringify({ + title: "Code", + description: "Because I am lazy", + files: [{ name, content: code, languageId: 378 }], + }), + method: "POST", + headers: { + "Content-Type": "application/json", + }, + }); + const data = (await response.json()) as PostData; + return ``; +} + +interface PostData { + key: string; +} diff --git a/src/utils/randomStatus.ts b/src/utils/randomStatus.ts deleted file mode 100644 index 983bd74..0000000 --- a/src/utils/randomStatus.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { ActivityType, Client, ClientPresenceStatus } from "discord.js"; - -const statues: [Exclude, string, ClientPresenceStatus][] = [ - [ActivityType.Watching, "the sern community", "online"], - [ActivityType.Listening, "Evo", "dnd"], - [ActivityType.Playing, "with @sern/cli", "idle"], - [ActivityType.Watching, "sern bots", "dnd"], - [ActivityType.Watching, "github stars go brrr", "online"], - [ActivityType.Listening, "Spotify", "dnd"], - [ActivityType.Listening, "what's bofa", "idle"], -]; - -export function randomStatus(client: Client) { - setInterval(() => { - const shuffledStatuses = shuffleArray(statues); - const [type, name, status] = [...shuffledStatuses].shift()!; - client.user!.setPresence({ activities: [{ name, type }], status }); - }, 60_000); -} - -function shuffleArray(array: T[]) { - for (let i = array.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [array[i], array[j]] = [array[j], array[i]]; - } - return [...array]; -} diff --git a/src/utils/require.ts b/src/utils/require.ts deleted file mode 100644 index a5ff0f3..0000000 --- a/src/utils/require.ts +++ /dev/null @@ -1,2 +0,0 @@ -import { createRequire } from "module"; -export const require = createRequire(import.meta.url); diff --git a/yarn.lock b/yarn.lock index 37323e2..678fa9d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -767,15 +767,6 @@ __metadata: languageName: node linkType: hard -"busboy@npm:^1.6.0": - version: 1.6.0 - resolution: "busboy@npm:1.6.0" - dependencies: - streamsearch: ^1.1.0 - checksum: 32801e2c0164e12106bf236291a00795c3c4e4b709ae02132883fe8478ba2ae23743b11c5735a0aae8afe65ac4b6ca4568b91f0d9fed1fdbc32ede824a73746e - languageName: node - linkType: hard - "cac@npm:^6.7.12": version: 6.7.12 resolution: "cac@npm:6.7.12" @@ -2409,7 +2400,6 @@ __metadata: trie-search: 1.4.1 tsup: 6.7.0 typescript: 5.0.4 - undici: 5.22.0 winston: 3.8.2 languageName: unknown linkType: soft @@ -2531,13 +2521,6 @@ __metadata: languageName: node linkType: hard -"streamsearch@npm:^1.1.0": - version: 1.1.0 - resolution: "streamsearch@npm:1.1.0" - checksum: 1cce16cea8405d7a233d32ca5e00a00169cc0e19fbc02aa839959985f267335d435c07f96e5e0edd0eadc6d39c98d5435fb5bbbdefc62c41834eadc5622ad942 - languageName: node - linkType: hard - "string-similarity@npm:4.0.4": version: 4.0.4 resolution: "string-similarity@npm:4.0.4" @@ -2844,15 +2827,6 @@ __metadata: languageName: node linkType: hard -"undici@npm:5.22.0": - version: 5.22.0 - resolution: "undici@npm:5.22.0" - dependencies: - busboy: ^1.6.0 - checksum: 8dc55240a60ae7680798df344e8f46ad0f872ed0fa434fb94cc4fd2b5b2f8053bdf11994d15902999d3880f9bf7cd875a2e90883d2702bf0f366dacd9cbf3fc6 - languageName: node - linkType: hard - "undici@npm:5.27.2": version: 5.27.2 resolution: "undici@npm:5.27.2"