forked from PluralFlux/PluralFlux
Compare commits
52 Commits
HEAD
...
add-attach
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
899d04a125 | ||
|
|
eb80fd2ec0 | ||
|
|
f65aeb0019 | ||
|
|
7a3b8c1994 | ||
|
|
2e0a8adec5 | ||
|
|
7aeae1837f | ||
|
|
6eb9fef376 | ||
|
|
9dab429d0d | ||
|
|
f9199f8477 | ||
|
|
a7cd4e96f0 | ||
|
|
21efbccfd7 | ||
|
|
873959a5f4 | ||
|
|
d33c3213f3 | ||
|
|
75c4c548d8 | ||
|
|
9d5493e8ab | ||
|
|
fc1c463696 | ||
|
|
1bba8099e9 | ||
|
|
acd9ce7c3e | ||
|
|
da9a3d2c8a | ||
|
|
274f1ead15 | ||
|
|
223292c2d3 | ||
|
|
400e40a405 | ||
|
|
152bc8873d | ||
|
|
e16694ac2d | ||
|
|
f0ac02e86d | ||
|
|
5c01f2e284 | ||
|
|
da5a250445 | ||
|
|
23a57b3e99 | ||
|
|
1bf6c8c1f2 | ||
|
|
fe00f66104 | ||
|
|
15703c24cd | ||
|
|
3dbbe7df50 | ||
|
|
31eb4262dd | ||
|
|
c645bb0aea | ||
|
|
0b7f549bdf | ||
|
|
bfc633a755 | ||
|
|
01e620a935 | ||
|
|
a4804c2ea7 | ||
|
|
164ff7d8b6 | ||
|
|
5e3b3f33d3 | ||
|
|
4fcb53482c | ||
|
|
ba9552b4aa | ||
|
|
35b454bc80 | ||
|
|
321fe7f0a9 | ||
|
|
0a4bfa59ad | ||
|
|
79d98c3618 | ||
|
|
a44e2745c5 | ||
|
|
5ab0d62bdb | ||
|
|
876f9486ad | ||
|
|
5e28cdfd01 | ||
|
|
5a39610547 | ||
|
|
a3caa2dc42 |
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
const channel = client.channels.get(message.channelId);
|
||||
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) => {
|
||||
const returnedBuffer = messageHelper.returnBufferFromText(text);
|
||||
await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.txt', data: returnedBuffer.file }]
|
||||
})
|
||||
console.error(e);
|
||||
});
|
||||
}
|
||||
if (message.attachments.size > 0) {
|
||||
// Not implemented yet
|
||||
}
|
||||
|
||||
await message.delete();
|
||||
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 username = member.displayname ?? member.name;
|
||||
if (text.length > 0) {
|
||||
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);
|
||||
});
|
||||
}
|
||||
else {
|
||||
await webhook.send({username: username, avatar_url: member.propic, files: attachments}).catch(async (e) => {
|
||||
console.error(e);
|
||||
});
|
||||
}
|
||||
await message.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
|
||||
|
||||
@@ -25,7 +25,8 @@ describe('webhookHelper', () => {
|
||||
const content = "hi"
|
||||
const attachments = {
|
||||
size: 0,
|
||||
first: () => {}
|
||||
first: () => {},
|
||||
foreach: jest.fn()
|
||||
}
|
||||
const message = {
|
||||
client,
|
||||
|
||||
Reference in New Issue
Block a user