2026-02-13 18:20:29 -05:00
|
|
|
const wh = {};
|
|
|
|
|
|
2026-02-13 20:37:02 -05:00
|
|
|
wh.getOrCreateWebhook = async function (api, channelId) {
|
2026-02-13 18:20:29 -05:00
|
|
|
const name = 'PluralFlux Proxy Webhook';
|
2026-02-13 20:37:02 -05:00
|
|
|
let webhook = await getWebhook(api, channelId, name);
|
2026-02-13 18:20:29 -05:00
|
|
|
if (webhook === undefined) {
|
2026-02-13 20:37:02 -05:00
|
|
|
webhook = await api.channels.createWebhook(channelId, {name: name});
|
2026-02-13 18:20:29 -05:00
|
|
|
}
|
|
|
|
|
return webhook;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 20:37:02 -05:00
|
|
|
async function getWebhook(api, channelId, name) {
|
|
|
|
|
const allWebhooks = await api.channels.getWebhooks(channelId);
|
|
|
|
|
if (allWebhooks.length === 0) {
|
2026-02-13 18:20:29 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let pf_webhook;
|
2026-02-13 20:37:02 -05:00
|
|
|
allWebhooks.forEach((webhook) => {
|
2026-02-13 18:20:29 -05:00
|
|
|
if (webhook.name === name) {
|
|
|
|
|
pf_webhook = webhook;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return pf_webhook;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 20:37:02 -05:00
|
|
|
wh.replaceMessage = async function (api, data, text, member) {
|
2026-02-13 18:20:29 -05:00
|
|
|
if (text.length > 0) {
|
2026-02-13 20:37:02 -05:00
|
|
|
const webhook = await wh.getOrCreateWebhook(api, data.channel_id);
|
2026-02-13 18:20:29 -05:00
|
|
|
await api.webhooks.execute(webhook.id, webhook.token, {content: text, username: member.displayname ?? member.name, propic: member.propic});
|
|
|
|
|
await api.channels.deleteMessage(data.channel_id, data.id);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
await api.channels.createMessage(data.channel_id, {content: '(Please input a message!)'});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const webhookHelper = wh;
|