2026-02-14 12:56:00 -05:00
|
|
|
import {messageHelper} from "./messageHelper.js";
|
2026-02-19 21:45:10 -05:00
|
|
|
import {Webhook, Channel, Message, Client} from '@fluxerjs/core';
|
2026-02-14 12:56:00 -05:00
|
|
|
import {enums} from "../enums.js";
|
|
|
|
|
|
2026-02-13 18:20:29 -05:00
|
|
|
const wh = {};
|
|
|
|
|
|
2026-02-14 12:56:00 -05:00
|
|
|
const name = 'PluralFlux Proxy Webhook';
|
|
|
|
|
|
2026-02-13 21:23:33 -05:00
|
|
|
/**
|
2026-02-14 21:02:08 -05:00
|
|
|
* Replaces a proxied message with a webhook using the member information.
|
2026-02-19 21:45:10 -05:00
|
|
|
* @async
|
2026-02-14 12:56:00 -05:00
|
|
|
* @param {Client} client - The fluxer.js client.
|
2026-02-14 21:02:08 -05:00
|
|
|
* @param {Message} message - The full message object.
|
|
|
|
|
* @throws {Error} When the proxy message is not in a server.
|
2026-02-13 21:23:33 -05:00
|
|
|
*/
|
2026-02-15 21:24:19 -05:00
|
|
|
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).catch(e =>{throw e});
|
2026-02-14 21:02:08 -05:00
|
|
|
// If the message doesn't match a proxy, just return.
|
2026-02-19 21:45:10 -05:00
|
|
|
if (!proxyMatch || !proxyMatch.member || (proxyMatch.message.length === 0 && !proxyMatch.hasAttachment) ) {
|
2026-02-13 18:20:29 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-19 21:45:10 -05:00
|
|
|
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs)
|
2026-02-14 21:02:08 -05:00
|
|
|
if (!message.guildId) {
|
|
|
|
|
throw new Error(enums.err.NOT_IN_SERVER);
|
|
|
|
|
}
|
2026-02-19 21:45:10 -05:00
|
|
|
if (proxyMatch.hasAttachment) {
|
|
|
|
|
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname ?? proxyMatch.member.name}`)
|
2026-02-15 15:57:08 -05:00
|
|
|
}
|
2026-02-19 21:45:10 -05:00
|
|
|
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-13 21:23:33 -05:00
|
|
|
/**
|
|
|
|
|
* Replaces a proxied message with a webhook using the member information.
|
2026-02-19 21:45:10 -05:00
|
|
|
* @async
|
2026-02-14 12:56:00 -05:00
|
|
|
* @param {Client} client - The fluxer.js client.
|
|
|
|
|
* @param {Message} message - The message to be deleted.
|
2026-02-13 21:23:33 -05:00
|
|
|
* @param {string} text - The text to send via the webhook.
|
2026-02-14 14:31:28 -05:00
|
|
|
* @param {model} member - A member object from the database.
|
2026-02-14 12:56:00 -05:00
|
|
|
* @throws {Error} When there's no message to send.
|
2026-02-13 21:23:33 -05:00
|
|
|
*/
|
2026-02-19 21:45:10 -05:00
|
|
|
wh.replaceMessage = async function(client, message, text, member) {
|
|
|
|
|
// attachment logic is not relevant yet, text length will always be over 0 right now
|
2026-02-15 21:24:19 -05:00
|
|
|
if (text.length > 0 || message.attachments.size > 0) {
|
|
|
|
|
const channel = client.channels.get(message.channelId);
|
2026-02-19 21:45:10 -05:00
|
|
|
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) =>{throw e});
|
2026-02-14 14:31:28 -05:00
|
|
|
const username = member.displayname ?? member.name;
|
2026-02-19 21:45:10 -05:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 12:56:00 -05:00
|
|
|
await message.delete();
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
2026-02-14 12:56:00 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 14:31:28 -05:00
|
|
|
/**
|
2026-02-14 21:02:08 -05:00
|
|
|
* Gets or creates a webhook.
|
2026-02-19 21:45:10 -05:00
|
|
|
* @async
|
2026-02-14 21:02:08 -05:00
|
|
|
* @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.
|
2026-02-14 14:31:28 -05:00
|
|
|
*/
|
2026-02-19 21:45:10 -05:00
|
|
|
wh.getOrCreateWebhook = async function(client, channel) {
|
2026-02-14 21:02:08 -05:00
|
|
|
// If channel doesn't allow webhooks
|
|
|
|
|
if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
|
2026-02-19 21:45:10 -05:00
|
|
|
let webhook = await wh.getWebhook(client, channel).catch((e) =>{throw e});
|
2026-02-14 21:02:08 -05:00
|
|
|
if (!webhook) {
|
|
|
|
|
webhook = await channel.createWebhook({name: name});
|
2026-02-14 14:31:28 -05:00
|
|
|
}
|
2026-02-14 21:02:08 -05:00
|
|
|
return webhook;
|
2026-02-14 14:31:28 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 12:56:00 -05:00
|
|
|
/**
|
2026-02-14 21:02:08 -05:00
|
|
|
* Gets an existing webhook.
|
2026-02-19 21:45:10 -05:00
|
|
|
* @async
|
2026-02-14 12:56:00 -05:00
|
|
|
* @param {Client} client - The fluxer.js client.
|
2026-02-14 21:02:08 -05:00
|
|
|
* @param {Channel} channel - The channel the message was sent in.
|
|
|
|
|
* @returns {Webhook} A webhook object.
|
2026-02-14 12:56:00 -05:00
|
|
|
*/
|
2026-02-19 21:45:10 -05:00
|
|
|
wh.getWebhook = async function(client, channel) {
|
2026-02-14 21:02:08 -05:00
|
|
|
const channelWebhooks = await channel?.fetchWebhooks() ?? [];
|
|
|
|
|
if (channelWebhooks.length === 0) {
|
2026-02-14 12:56:00 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-14 21:02:08 -05:00
|
|
|
let pf_webhook;
|
|
|
|
|
channelWebhooks.forEach((webhook) => {
|
|
|
|
|
if (webhook.name === name) {
|
|
|
|
|
pf_webhook = webhook;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return pf_webhook;
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const webhookHelper = wh;
|