52 Commits

Author SHA1 Message Date
Aster Fialla
899d04a125 Merge branch 'main' of https://github.com/pieartsy/PluralFlux into add-attachments
# Conflicts:
#	src/commands.js
#	src/enums.js
#	src/helpers/memberHelper.js
#	src/helpers/messageHelper.js
#	src/helpers/webhookHelper.js
#	tests/helpers/memberHelper.test.js
#	tests/helpers/messageHelper.test.js
#	tests/helpers/webhookHelper.test.js
2026-02-20 09:55:18 -05:00
Aster Fialla
eb80fd2ec0 edit webhook helper to replace message including attachment 2026-02-19 13:50:15 -05:00
Aster Fialla
f65aeb0019 add methods to messageHelper to parse through attachment URLs 2026-02-19 13:49:37 -05:00
Aster Fialla
7a3b8c1994 edited help message trigger for updatePropic 2026-02-19 01:45:43 -05:00
Aster Fialla
2e0a8adec5 added tests for updateDisplayName 2026-02-19 01:31:38 -05:00
Aster Fialla
7aeae1837f added todo note 2026-02-19 00:59:57 -05:00
Aster Fialla
6eb9fef376 added debounce to count guilds properly 2026-02-19 00:52:06 -05:00
Aster Fialla
9dab429d0d , updated tests for webhookHelper and removed error response when proxy is sent without content 2026-02-19 00:51:33 -05:00
Aster Fialla
f9199f8477 removed error response when proxy is sent without content 2026-02-19 00:48:57 -05:00
Aster Fialla
a7cd4e96f0 updating enums 2026-02-19 00:20:53 -05:00
Aster Fialla
21efbccfd7 adding more cases to messageHelper tests 2026-02-19 00:20:46 -05:00
Aster Fialla
873959a5f4 finally mocking correctly 2026-02-18 21:24:41 -05:00
Aster Fialla
d33c3213f3 removed confusing brackets from enum docs 2026-02-18 21:20:56 -05:00
Aster Fialla
75c4c548d8 deleted extra file added during merge 2026-02-18 21:13:25 -05:00
Aster Fialla
9d5493e8ab Merge remote-tracking branch 'origin/add-tests' into add-tests
# Conflicts:
#	src/bot.js
#	src/commands.js
#	src/database.js
#	src/helpers/importHelper.js
#	src/helpers/memberHelper.js
#	src/helpers/messageHelper.js
#	src/helpers/webhookHelper.js
2026-02-18 21:03:44 -05:00
Aster Fialla
fc1c463696 more tests for webhookhelper 2026-02-18 17:14:04 -05:00
Aster Fialla
1bba8099e9 updating docstring for messageHelper parseProxyTags 2026-02-18 16:29:08 -05:00
Aster Fialla
acd9ce7c3e more cases for messageHelper just in case 2026-02-18 16:28:35 -05:00
Aster Fialla
da9a3d2c8a finished tests for messageHelper! 2026-02-18 12:37:39 -05:00
Aster Fialla
274f1ead15 added "return" so tests dont terminate on failure and deleted env.jest 2026-02-18 12:11:55 -05:00
Aster Fialla
223292c2d3 added tests for parseProxyTags and updated logic 2026-02-18 10:17:08 -05:00
Aster Fialla
400e40a405 updated sendMessageAsAttachment to returnBufferFromText and updated commands/webhookHelper accordingly 2026-02-18 09:19:44 -05:00
Aster Fialla
152bc8873d added test for memberHelper 2026-02-18 09:16:26 -05:00
Aster Fialla
e16694ac2d fixed test and logic 2026-02-18 08:47:37 -05:00
Aster Fialla
f0ac02e86d readded line i shouldn't have removed in sendMessageAsMember 2026-02-18 00:33:05 -05:00
Aster Fialla
5c01f2e284 test setup for messagehelper and webhookhelper 2026-02-18 00:28:18 -05:00
Aster Fialla
da5a250445 added return to addFullMember so that addNewMember can reference it properly in strings 2026-02-18 00:27:51 -05:00
Aster Fialla
23a57b3e99 simplified sendMessageAsAttachment 2026-02-18 00:26:51 -05:00
Aster Fialla
1bf6c8c1f2 think i fixed weird error with webhook sending error when a user has no members 2026-02-18 00:25:41 -05:00
Aster Fialla
fe00f66104 more tests for member helper 2026-02-17 23:03:45 -05:00
Aster Fialla
15703c24cd moved import to helpers folder 2026-02-17 23:03:19 -05:00
Aster Fialla
3dbbe7df50 moved import to helpers folder 2026-02-17 23:03:08 -05:00
Aster Fialla
31eb4262dd upgraded fluxer.js 2026-02-17 22:23:55 -05:00
Aster Fialla
c645bb0aea renamed db to database
more tests and fixing logic for memberhelper
2026-02-17 22:23:47 -05:00
Aster Fialla
0b7f549bdf added error handling parseMemberCommand test 2026-02-17 21:21:07 -05:00
Aster Fialla
bfc633a755 setup fixed more 2026-02-17 20:52:22 -05:00
Aster Fialla
01e620a935 finally figured out issue with tests (referencing the method directly in the test.each calls the real method not the mock in beforeEach()) 2026-02-17 20:49:19 -05:00
Aster Fialla
a4804c2ea7 added babel to convert es modules to cjs 2026-02-17 20:48:44 -05:00
Aster Fialla
164ff7d8b6 nevermind it wasn't actually working, gonna move on for now 2026-02-17 19:58:56 -05:00
Aster Fialla
5e3b3f33d3 mostly working except for the weirdest error 2026-02-17 19:38:50 -05:00
Aster Fialla
4fcb53482c separating out enum return from method return 2026-02-17 19:36:14 -05:00
Aster Fialla
ba9552b4aa updated jest to sort of work with es6 2026-02-17 17:38:06 -05:00
Aster Fialla
35b454bc80 Revert "converted import syntax to commonJS"
This reverts commit 5ab0d62b
2026-02-17 17:25:18 -05:00
Aster Fialla
321fe7f0a9 more correct dockerfile and compose.yaml 2026-02-17 17:16:48 -05:00
Aster Fialla
0a4bfa59ad more correct test setup 2026-02-17 17:16:10 -05:00
Aster Fialla
79d98c3618 adjusted beforeeach/beforeall so more pass 2026-02-17 17:16:10 -05:00
Aster Fialla
a44e2745c5 got test sort of working (jest set up is not crashing but also not mocking correctly) 2026-02-17 17:16:10 -05:00
Aster Fialla
5ab0d62bdb converted import syntax to commonJS
removed unused methods
2026-02-17 17:16:09 -05:00
Aster Fialla
876f9486ad more correct test setup 2026-02-17 07:14:47 -05:00
Aster Fialla
5e28cdfd01 adjusted beforeeach/beforeall so more pass 2026-02-17 07:11:22 -05:00
Aster Fialla
5a39610547 got test sort of working (jest set up is not crashing but also not mocking correctly) 2026-02-16 18:46:33 -05:00
Aster Fialla
a3caa2dc42 converted import syntax to ES modules
removed unused methods
2026-02-16 18:46:32 -05:00
6 changed files with 88 additions and 30 deletions

View File

@@ -39,7 +39,7 @@ All commands are prefixed by `pf;`. Currently only a few are implemented.
- `proxy` Updates the proxy tag for a specific member based on their name. The proxy must be formatted with the tags surrounding the word 'text', for example: `pf;member jane proxy Jane:text` or `pf;member amal proxy [text]` This is so the bot can detect what the proxy tags are. **Only one proxy can be set per member currently.**
## Notes
- Attaching files to messages with the proxy does not work, due to either the limitations of Fluxer itself :(
- Only one proxy tag can be set per member currently.
## Upcoming
- [ ] React with x to delete message

View File

@@ -11,6 +11,7 @@
"dependencies": {
"@fluxerjs/core": "^1.1.5",
"dotenv": "^17.3.1",
"node-fetch": "^3.3.2",
"pg": "^8.18.0",
"pg-hstore": "^2.3.4",
"pm2": "^6.0.14",

View File

@@ -1,8 +1,6 @@
import {memberHelper} from "./memberHelper.js";
import {enums} from "../enums.js";
import tmp, {setGracefulCleanup} from "tmp";
import fs from 'fs';
import {Message} from "@fluxerjs/core";
import fetch from 'node-fetch';
const msgh = {};
@@ -86,4 +84,56 @@ msgh.returnBufferFromText = function (text) {
return {text: text, file: undefined}
}
/**
* Returns an ArrayBuffer from an attachment URL.
*
* @param {string} attachmentUrl
* @returns {ArrayBuffer} The buffer from the image.
*
*/
msgh.returnBufferFromUrl = async function (attachmentUrl) {
retryPromise(() => fetch(attachmentUrl),{
retryIf: (response) => !response.ok,
retries: 5
}).then(async(res) => {
return await res.arrayBuffer().catch((err) => {
throw new Error(`Error loading attachment into buffer: ${err.message}`);
})
})
}
// Source - https://stackoverflow.com/a/70687149 - Arturo Hernandez
function retryPromise(promise, options) {
const { retryIf, retryCatchIf, retries } = { retryIf: () => false, retryCatchIf: () => true, retries: 5, ...options};
let _promise = promise();
for (let i = 1; i < retries; i++)
_promise = _promise.catch((value) => retryCatchIf(value) ? promise() : Promise.reject(value))
.then((value) => retryIf(value) ? promise() : Promise.reject(value));
return _promise;
}
/**
* Returns an ArrayBuffer from an attachment URL.
*
* @param {Collection<string, APIMessageAttachment>} attachments - A collection of attachments from the message object
* @returns {[{string, ArrayBuffer}]} An array of file objects
*
*/
msgh.createFileObjectFromAttachments = async function (attachments) {
if (attachments.size === 0) {
return [];
}
const attachmentsObj = [];
attachments.forEach(async (attachment) => {
await msgh.returnBufferFromUrl(attachment.url).then((res) => {
attachmentsObj.push({name: attachment.filename, data: res});
});
});
return attachmentsObj;
}
export const messageHelper = msgh;

View File

@@ -24,10 +24,9 @@ wh.sendMessageAsMember = async function(client, message) {
if (!message.guildId) {
throw new Error(enums.err.NOT_IN_SERVER);
}
if (proxyMatch.hasAttachment) {
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname ?? proxyMatch.member.name}`)
}
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
const attachments = messageHelper.createFileObjectFromAttachments(message.attachments);
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member, attachments).catch(e =>{throw e});
}
/**
@@ -37,28 +36,35 @@ wh.sendMessageAsMember = async function(client, message) {
* @param {Message} message - The message to be deleted.
* @param {string} text - The text to send via the webhook.
* @param {model} member - A member object from the database.
* @param {[{string, ArrayBuffer}]} attachments - Attachments file objects, if any.
* @throws {Error} When there's no message to send.
*/
wh.replaceMessage = async function(client, message, text, member) {
// attachment logic is not relevant yet, text length will always be over 0 right now
if (text.length > 0 || message.attachments.size > 0) {
wh.replaceMessage = async function (client, message, text, member, attachments) {
if (text.length === 0 && attachments.length === 0) {
return;
}
const channel = client.channels.get(message.channelId);
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) =>{throw e});
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) => {
throw e
});
const username = member.displayname ?? member.name;
if (text.length > 0) {
await webhook.send({content: text, username: username, avatar_url: member.propic}).catch(async(e) => {
if (text.length > 2000) {
const returnedBuffer = messageHelper.returnBufferFromText(text);
await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.txt', data: returnedBuffer.file }]
})
attachments.push(returnedBuffer);
}
await webhook.send({content: text, username: username, avatar_url: member.propic, files: attachments}).catch(async (e) => {
console.error(e);
});
}
if (message.attachments.size > 0) {
// Not implemented yet
else {
await webhook.send({username: username, avatar_url: member.propic, files: attachments}).catch(async (e) => {
console.error(e);
});
}
await message.delete();
}
}
/**

View File

@@ -5,7 +5,7 @@ const {memberHelper} = require("../../src/helpers/memberHelper.js");
const {Message} = require("@fluxerjs/core");
const {fs} = require('fs');
const {enums} = require('../../src/enums');
const {tmp, setGracefulCleanup} = require('tmp');
const fetch = require('node-fetch');
jest.mock('../../src/helpers/memberHelper.js', () => {
return {memberHelper: {
@@ -13,7 +13,7 @@ jest.mock('../../src/helpers/memberHelper.js', () => {
}}
})
jest.mock('tmp');
jest.mock('node-fetch');
jest.mock('fs');
jest.mock('@fluxerjs/core');

View File

@@ -25,7 +25,8 @@ describe('webhookHelper', () => {
const content = "hi"
const attachments = {
size: 0,
first: () => {}
first: () => {},
foreach: jest.fn()
}
const message = {
client,