diff --git a/src/bot.js b/src/bot.js index 80a8370..7bd43f7 100644 --- a/src/bot.js +++ b/src/bot.js @@ -70,9 +70,10 @@ function printGuilds() { } const debouncePrintGuilds = utils.debounce(printGuilds, 2000); +const debounceLogin = utils.debounce(client.login, 60000); try { - await client.login(token); + await debounceLogin(token); // await db.check_connection(); } catch (err) { console.error('Login failed:', err); diff --git a/src/commands.js b/src/commands.js index 5a7edc1..73497eb 100644 --- a/src/commands.js +++ b/src/commands.js @@ -54,7 +54,7 @@ cmds.set('import', { if ((message.content.includes('--help') || (args[0] === '' && args.length === 1)) && !attachmentUrl ) { return await message.reply(enums.help.IMPORT); } - return await importHelper.pluralKitImport(message.author.id, attachmentUrl).then(async (successfullyAdded) => { + return await importHelper.debounceImport(message.author.id, attachmentUrl).then(async (successfullyAdded) => { await message.reply(successfullyAdded); }).catch(async (error) => { if (error instanceof AggregateError) { diff --git a/src/helpers/importHelper.js b/src/helpers/importHelper.js index 6cb2b15..7e86754 100644 --- a/src/helpers/importHelper.js +++ b/src/helpers/importHelper.js @@ -1,5 +1,6 @@ import {enums} from "../enums.js"; import {memberHelper} from "./memberHelper.js"; +import {utils} from "./utils.js"; const ih = {}; @@ -40,4 +41,6 @@ ih.pluralKitImport = async function (authorId, attachmentUrl) { }); } +ih.debounceImport = utils.debounce(ih.pluralKitImport, 5000); + export const importHelper = ih; \ No newline at end of file diff --git a/src/helpers/memberHelper.js b/src/helpers/memberHelper.js index 21396f5..02899b6 100644 --- a/src/helpers/memberHelper.js +++ b/src/helpers/memberHelper.js @@ -296,7 +296,7 @@ mh.updateProxy = async function (authorId, memberName, proxy) { mh.updatePropic = async function (authorId, memberName, values, attachmentUrl = null, attachmentExpiration = null) { const imgUrl = values ?? attachmentUrl; // Throws error if invalid - await utils.checkImageFormatValidity(imgUrl).catch((e) => { throw e }); + await utils.debounceCheckImageFormat(imgUrl).catch((e) => { throw e }); return await mh.updateMemberField(authorId, memberName, "propic", imgUrl, attachmentExpiration).catch((e) => { throw e }); } @@ -381,7 +381,7 @@ mh.addFullMember = async function (authorId, memberName, displayName = null, pro let isValidPropic; if (propic && propic.length > 0) { - await utils.checkImageFormatValidity(propic).then(() => { + await utils.debounceCheckImageFormat(propic).then(() => { isValidPropic = true; }).catch((e) => { errors.push(`Tried to set profile picture to \"${propic}\". ${e.message}. ${enums.err.SET_TO_NULL}`); diff --git a/src/helpers/utils.js b/src/helpers/utils.js index a980cbf..6b79064 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -26,4 +26,6 @@ u.checkImageFormatValidity = async function (imageUrl) { }); } +u.debounceCheckImageFormat = u.debounce(u.checkImageFormatValidity, 60000); + export const utils = u; diff --git a/tests/helpers/memberHelper.test.js b/tests/helpers/memberHelper.test.js index a652032..583b851 100644 --- a/tests/helpers/memberHelper.test.js +++ b/tests/helpers/memberHelper.test.js @@ -20,7 +20,7 @@ jest.mock("../../src/helpers/utils.js", () => { return { utils: { - checkImageFormatValidity: jest.fn().mockResolvedValue(), + debounceCheckImageFormat: jest.fn().mockResolvedValue(), } } }); @@ -476,15 +476,15 @@ describe('MemberHelper', () => { [mockMember.propic, null, null, mockMember.propic], [mockMember.propic, attachmentUrl, null, attachmentUrl], [null, attachmentUrl, attachmentExpiration, attachmentUrl] - ])('calls checkImageFormatValidity and updateMemberField and returns string', async(imgUrl, attachmentUrl, attachmentExpiration, expected) => { + ])('calls debounceCheckImageFormat and updateMemberField and returns string', async(imgUrl, attachmentUrl, attachmentExpiration, expected) => { // Arrange jest.spyOn(memberHelper, 'updateMemberField').mockResolvedValue("Updated"); // Act return memberHelper.updatePropic(authorId, mockMember.name, imgUrl, attachmentUrl, attachmentExpiration).then((result) => { expect(result).toEqual("Updated"); - expect(utils.checkImageFormatValidity).toHaveBeenCalledTimes(1); - expect(utils.checkImageFormatValidity).toHaveBeenCalledWith(expected); + expect(utils.debounceCheckImageFormat).toHaveBeenCalledTimes(1); + expect(utils.debounceCheckImageFormat).toHaveBeenCalledWith(expected); expect(memberHelper.updateMemberField).toHaveBeenCalledTimes(1); expect(memberHelper.updateMemberField).toHaveBeenCalledWith(authorId, mockMember.name, "propic", expected, attachmentExpiration); }); @@ -603,7 +603,7 @@ describe('MemberHelper', () => { }) }) - test('if propic, call checkImageFormatValidity', async () => { + test('if propic, call debounceCheckImageFormat', async () => { // Arrange const expectedMemberArgs = { name: mockMember.name, @@ -618,8 +618,8 @@ describe('MemberHelper', () => { return await memberHelper.addFullMember(authorId, mockMember.name, null, null, mockMember.propic).then((res) => { // Assert expect(res).toEqual(expectedReturn); - expect(utils.checkImageFormatValidity).toHaveBeenCalledWith(mockMember.propic); - expect(utils.checkImageFormatValidity).toHaveBeenCalledTimes(1); + expect(utils.debounceCheckImageFormat).toHaveBeenCalledWith(mockMember.propic); + expect(utils.debounceCheckImageFormat).toHaveBeenCalledTimes(1); expect(database.members.create).toHaveBeenCalledWith(expectedMemberArgs); expect(database.members.create).toHaveBeenCalledTimes(1); })