forked from PluralFlux/PluralFlux
edit webhook helper to replace message including attachment
This commit is contained in:
@@ -24,10 +24,8 @@ wh.sendMessageAsMember = async function(client, message) {
|
|||||||
if (!message.guildId) {
|
if (!message.guildId) {
|
||||||
throw new Error(enums.err.NOT_IN_SERVER);
|
throw new Error(enums.err.NOT_IN_SERVER);
|
||||||
}
|
}
|
||||||
if (proxyMatch.hasAttachment) {
|
const attachments = await messageHelper.createFileObjectFromAttachments(message.attachments);
|
||||||
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname ?? proxyMatch.member.name}`)
|
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member, attachments).catch(e =>{throw e});
|
||||||
}
|
|
||||||
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,28 +35,35 @@ wh.sendMessageAsMember = async function(client, message) {
|
|||||||
* @param {Message} message - The message to be deleted.
|
* @param {Message} message - The message to be deleted.
|
||||||
* @param {string} text - The text to send via the webhook.
|
* @param {string} text - The text to send via the webhook.
|
||||||
* @param {model} member - A member object from the database.
|
* @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.
|
* @throws {Error} When there's no message to send.
|
||||||
*/
|
*/
|
||||||
wh.replaceMessage = async function(client, message, text, member) {
|
wh.replaceMessage = async function (client, message, text, member, attachments) {
|
||||||
// attachment logic is not relevant yet, text length will always be over 0 right now
|
if (text.length === 0 && attachments.length === 0) {
|
||||||
if (text.length > 0 || message.attachments.size > 0) {
|
return;
|
||||||
|
}
|
||||||
const channel = client.channels.get(message.channelId);
|
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;
|
const username = member.displayname ?? member.name;
|
||||||
if (text.length > 0) {
|
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);
|
const returnedBuffer = messageHelper.returnBufferFromText(text);
|
||||||
await webhook.send({content: returnedBuffer.text, username: username, avatar_url: member.propic, files: [{ name: 'text.txt', data: returnedBuffer.file }]
|
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);
|
console.error(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (message.attachments.size > 0) {
|
else {
|
||||||
// Not implemented yet
|
await webhook.send({username: username, avatar_url: member.propic, files: attachments}).catch(async (e) => {
|
||||||
|
console.error(e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await message.delete();
|
await message.delete();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ describe('webhookHelper', () => {
|
|||||||
const content = "hi"
|
const content = "hi"
|
||||||
const attachments = {
|
const attachments = {
|
||||||
size: 0,
|
size: 0,
|
||||||
first: () => {}
|
first: () => {},
|
||||||
|
foreach: jest.fn()
|
||||||
}
|
}
|
||||||
const message = {
|
const message = {
|
||||||
client,
|
client,
|
||||||
|
|||||||
Reference in New Issue
Block a user