forked from PluralFlux/PluralFlux
* refactored async/await for import helper to not also use then/catch * added enum * refactor webhookHelper and tests to not use then/catch * changed docstring * refactoring bot and tests to not use then/catch * refactoring commands.js and tests to not use then/catch * refactoring memberHelper.js and tests to not use then/catch * removing then/catch from messageHelper.test.js * fixed set up for commands tests * edited bot to have top level main function * one more test in commands.js, and removed console.error * fixed typo in webhookHelper * forgot to switch over some tests in bot.test and commands.test * removed console.log from import helper * put console.error in commands * converted utils.js to not use then/catch * tested utils checkImageFormatValidity * removed jest-fetch-mock since it turns out I was just manually mocking it anyway * refactored database to not use then/catch * added dash to commands.js and test to pass * added the remaining webhook tests * changed utils to check for 10MB size not 1MB * removed unnecessary try/catch from utils * Simplify getWebhook to use .find() instead of foreach logic * make memberCommand exit when error occurs with parseMemberCommand * changed commands.js to not have user interaction within the catch * updated console.error message in database.js * made importHelper mock throw error instead of "resolve" error * replaced "pk;" with "pf;" in test * Got rid of unnecessary check for empty message from user (Fluxer doesn't allow this to happen) Removed export of token * getAllMembersInfo checks for fields.length * added default case to memberCommandHandler to throw error if command is not recognized * reversed check for valid proxy (was returning valid if the proxy existed and invalid if it didn't) * pushes e.message instead of full error object to errors array in importHelper * adjusted tests to properly use mockRejectedValue for async rejections * changed getAllMembersInfo map to say `index` not `name` as it actually gets the index of a member and then the member object * adjusted importHelper to properly test throwing of aggregate error * revamped setting of expiration warning (moved to utils and changed logic, wrote tests) --------- Co-authored-by: Aster Fialla <asterfialla@gmail.com>
96 lines
3.7 KiB
JavaScript
96 lines
3.7 KiB
JavaScript
import {messageHelper} from "./messageHelper.js";
|
|
import {Webhook, Channel, Message, Client} from '@fluxerjs/core';
|
|
import {enums} from "../enums.js";
|
|
|
|
const wh = {};
|
|
|
|
const name = 'PluralFlux Proxy Webhook';
|
|
|
|
/**
|
|
* Replaces a proxied message with a webhook using the member information.
|
|
* @async
|
|
* @param {Client} client - The fluxer.js client.
|
|
* @param {Message} message - The full message object.
|
|
* @throws {Error} When the proxy message is not in a server.
|
|
*/
|
|
wh.sendMessageAsMember = async function(client, message) {
|
|
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
|
const proxyMatch = await messageHelper.parseProxyTags(message.author.id, message.content, attachmentUrl);
|
|
// If the message doesn't match a proxy, just return.
|
|
if (!proxyMatch || !proxyMatch.member || (proxyMatch.message.length === 0 && !proxyMatch.hasAttachment) ) {
|
|
return;
|
|
}
|
|
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs)
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Replaces a proxied message with a webhook using the member information.
|
|
* @async
|
|
* @param {Client} client - The fluxer.js client.
|
|
* @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.
|
|
* @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);
|
|
const username = member.displayname ?? member.name;
|
|
if (text.length <= 2000) {
|
|
await webhook.send({content: text, username: username, avatar_url: member.propic})
|
|
}
|
|
else 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 }]
|
|
})
|
|
}
|
|
if (message.attachments.size > 0) {
|
|
// Not implemented yet
|
|
}
|
|
await message.delete();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets or creates a webhook.
|
|
* @async
|
|
* @param {Client} client - The fluxer.js client.
|
|
* @param {Channel} channel - The channel the message was sent in.
|
|
* @returns {Webhook} A webhook object.
|
|
* @throws {Error} When no webhooks are allowed in the channel.
|
|
*/
|
|
wh.getOrCreateWebhook = async function(client, channel) {
|
|
// If channel doesn't allow webhooks
|
|
if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
|
|
let webhook = await wh.getWebhook(client, channel)
|
|
if (!webhook) {
|
|
webhook = await channel.createWebhook({name: name});
|
|
}
|
|
return webhook;
|
|
}
|
|
|
|
/**
|
|
* Gets an existing webhook.
|
|
* @async
|
|
* @param {Client} client - The fluxer.js client.
|
|
* @param {Channel} channel - The channel the message was sent in.
|
|
* @returns {Webhook} A webhook object.
|
|
*/
|
|
wh.getWebhook = async function(client, channel) {
|
|
const channelWebhooks = await channel?.fetchWebhooks() ?? [];
|
|
if (channelWebhooks.length === 0) {
|
|
return;
|
|
}
|
|
return channelWebhooks.find((webhook) => webhook.name === name);
|
|
}
|
|
|
|
export const webhookHelper = wh; |