2026-02-14 12:56:00 -05:00
|
|
|
import {messageHelper} from "./messageHelper.js";
|
|
|
|
|
import {memberHelper} from "./memberHelper.js";
|
|
|
|
|
import { Webhook, Channel, Message } from '@fluxerjs/core';
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* Gets or creates a webhook.
|
|
|
|
|
*
|
2026-02-14 12:56:00 -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-13 21:23:33 -05:00
|
|
|
*/
|
2026-02-14 12:56:00 -05:00
|
|
|
async function getOrCreateWebhook(client, channel) {
|
|
|
|
|
if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
|
|
|
|
|
let webhook = await getWebhook(client, channel);
|
2026-02-13 21:23:33 -05:00
|
|
|
if (!webhook) {
|
2026-02-14 12:56:00 -05:00
|
|
|
webhook = await channel.createWebhook({name: name});
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
return webhook;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 21:23:33 -05:00
|
|
|
/**
|
|
|
|
|
* Gets an existing webhook.
|
|
|
|
|
*
|
2026-02-14 12:56:00 -05:00
|
|
|
* @param {Client} client - The fluxer.js client.
|
|
|
|
|
* @param {Channel} channel - The channel the message was sent in.
|
|
|
|
|
* @returns {Webhook} A webhook object.
|
2026-02-13 21:23:33 -05:00
|
|
|
*/
|
2026-02-14 12:56:00 -05:00
|
|
|
async function getWebhook(client, channel) {
|
|
|
|
|
const channelWebhooks = await channel?.fetchWebhooks() ?? [];
|
|
|
|
|
if (channelWebhooks.length === 0) {
|
2026-02-13 18:20:29 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let pf_webhook;
|
2026-02-14 12:56:00 -05:00
|
|
|
channelWebhooks.forEach((webhook) => {
|
2026-02-13 18:20:29 -05:00
|
|
|
if (webhook.name === name) {
|
|
|
|
|
pf_webhook = webhook;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return pf_webhook;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 21:23:33 -05:00
|
|
|
/**
|
|
|
|
|
* Replaces a proxied message with a webhook using the member information.
|
2026-02-14 12:56:00 -05:00
|
|
|
* @param {Client} client - The fluxer.js client.
|
|
|
|
|
* @param {Message} message - The message to be deleted.
|
|
|
|
|
* @param {string} channelId - The channel id to send the webhook message in.
|
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-14 12:56:00 -05:00
|
|
|
async function replaceMessage(client, message, channelId, text, member) {
|
2026-02-13 18:20:29 -05:00
|
|
|
if (text.length > 0) {
|
2026-02-14 12:56:00 -05:00
|
|
|
const channel = client.channels.get(channelId);
|
|
|
|
|
const webhook = await getOrCreateWebhook(client, channel);
|
2026-02-14 14:31:28 -05:00
|
|
|
const username = member.displayname ?? member.name;
|
|
|
|
|
await webhook.send({content: text, username: username, avatar_url: member.propic});
|
2026-02-14 12:56:00 -05:00
|
|
|
await message.delete();
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
else {
|
2026-02-14 12:56:00 -05:00
|
|
|
throw new Error(enums.err.NO_MESSAGE_SENT_WITH_PROXY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 14:31:28 -05:00
|
|
|
/**
|
|
|
|
|
* Fluxer collapses same-name messages, so if two would be sent by different users, break them up with a tiny space.
|
|
|
|
|
* @param {string} channelId - The channel id to send the webhook message in.
|
|
|
|
|
* @param {string} username - The text to send via the webhook.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function preventSameNameCollapse(channel, username) {
|
|
|
|
|
|
|
|
|
|
if(bot.recent[msg.channel.id] && msg.author.id !== bot.recent[msg.channel.id][0].user_id && un === bot.recent[msg.channel.id][0].name) {
|
|
|
|
|
username = un.substring(0,1) + "\u200a" + un.substring(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 12:56:00 -05:00
|
|
|
/**
|
|
|
|
|
* Replaces a proxied message with a webhook using the member information.
|
|
|
|
|
* @param {Client} client - The fluxer.js client.
|
|
|
|
|
* @param {Message} message - The full message object.
|
|
|
|
|
* @param {string} content - The full content of the message.
|
|
|
|
|
* @throws {Error} When the proxy message is not in a server.
|
|
|
|
|
*/
|
|
|
|
|
wh.sendMessageAsMember = async function(client, message, content) {
|
|
|
|
|
|
2026-02-14 15:20:05 -05:00
|
|
|
const proxyMatch = await messageHelper.parseProxyTags(message.author.id, message.attachments[0] ?? null, content);
|
2026-02-14 12:56:00 -05:00
|
|
|
// If the message doesn't match a proxy, just return.
|
|
|
|
|
if (!proxyMatch.proxy) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!message.guildId) {
|
|
|
|
|
throw new Error(enums.err.NOT_IN_SERVER);
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
2026-02-14 12:56:00 -05:00
|
|
|
const member = await memberHelper.getMemberByProxy(message.author.id, proxyMatch.proxy);
|
|
|
|
|
await replaceMessage(client, message, message.channelId, proxyMatch.message, member);
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const webhookHelper = wh;
|