implementing webhooks?

This commit is contained in:
Aster Fialla
2026-02-14 12:56:00 -05:00
committed by pieartsy
parent a92f00e545
commit 851bae4ff8

View File

@@ -1,17 +1,25 @@
import {messageHelper} from "./messageHelper.js";
import {memberHelper} from "./memberHelper.js";
import { Webhook, Channel, Message } from '@fluxerjs/core';
import {enums} from "../enums.js";
const wh = {}; const wh = {};
const name = 'PluralFlux Proxy Webhook';
/** /**
* Gets or creates a webhook. * Gets or creates a webhook.
* *
* @param api - The discord.js API. * @param {Client} client - The fluxer.js client.
* @param {string} channelId - The channel the message was sent in. * @param {Channel} channel - The channel the message was sent in.
* @returns {Object} A webhook object. * @returns {Webhook} A webhook object.
* @throws {Error} When no webhooks are allowed in the channel.
*/ */
wh.getOrCreateWebhook = async function (api, channelId) { async function getOrCreateWebhook(client, channel) {
const name = 'PluralFlux Proxy Webhook'; if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
let webhook = await getWebhook(api, channelId, name); let webhook = await getWebhook(client, channel);
if (!webhook) { if (!webhook) {
webhook = await api.channels.createWebhook(channelId, {name: name}); webhook = await channel.createWebhook({name: name});
} }
return webhook; return webhook;
} }
@@ -19,18 +27,17 @@ wh.getOrCreateWebhook = async function (api, channelId) {
/** /**
* Gets an existing webhook. * Gets an existing webhook.
* *
* @param api - The discord.js API. * @param {Client} client - The fluxer.js client.
* @param {string} channelId - The channel the message was sent in. * @param {Channel} channel - The channel the message was sent in.
* @param {string} name - The name of the webhook. * @returns {Webhook} A webhook object.
* @returns {Object} A webhook object.
*/ */
async function getWebhook(api, channelId, name) { async function getWebhook(client, channel) {
const allWebhooks = await api.channels.getWebhooks(channelId); const channelWebhooks = await channel?.fetchWebhooks() ?? [];
if (allWebhooks.length === 0) { if (channelWebhooks.length === 0) {
return; return;
} }
let pf_webhook; let pf_webhook;
allWebhooks.forEach((webhook) => { channelWebhooks.forEach((webhook) => {
if (webhook.name === name) { if (webhook.name === name) {
pf_webhook = webhook; pf_webhook = webhook;
} }
@@ -40,21 +47,44 @@ async function getWebhook(api, channelId, name) {
/** /**
* Replaces a proxied message with a webhook using the member information. * Replaces a proxied message with a webhook using the member information.
* * @param {Client} client - The fluxer.js client.
* @param api - The discord.js API. * @param {Message} message - The message to be deleted.
* @param data - The discord.js data. * @param {string} channelId - The channel id to send the webhook message in.
* @param {string} text - The text to send via the webhook. * @param {string} text - The text to send via the webhook.
* @param {Object} member - A member object from the database. * @param {Object} member - A member object from the database.
* @throws {Error} When there's no message to send.
*/ */
wh.replaceMessage = async function (api, data, text, member) { async function replaceMessage(client, message, channelId, text, member) {
if (text.length > 0) { if (text.length > 0) {
const webhook = await wh.getOrCreateWebhook(api, data.channel_id); const channel = client.channels.get(channelId);
await api.webhooks.execute(webhook.id, webhook.token, {content: text, username: member.displayname ?? member.name, avatar_url: member.propic}); const webhook = await getOrCreateWebhook(client, channel);
await api.channels.deleteMessage(data.channel_id, data.id); await webhook.send({content: text, username: member.displayname ?? member.name, avatar_url: member.propic});
await message.delete();
} }
else { else {
await api.channels.createMessage(data.channel_id, {content: '(Please input a message!)'}); throw new Error(enums.err.NO_MESSAGE_SENT_WITH_PROXY);
} }
} }
/**
* 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) {
const proxyMatch = await messageHelper.parseProxyTags(message.author.id, content);
// 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);
}
const member = await memberHelper.getMemberByProxy(message.author.id, proxyMatch.proxy);
await replaceMessage(client, message, message.channelId, proxyMatch.message, member);
}
export const webhookHelper = wh; export const webhookHelper = wh;