forked from PluralFlux/PluralFlux
think i fixed weird error with webhook sending error when a user has no members
This commit is contained in:
@@ -16,18 +16,14 @@ wh.sendMessageAsMember = async function(client, message) {
|
|||||||
const attachmentUrl = message.attachments.size > 0 ? message.attachments.first().url : null;
|
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});
|
const proxyMatch = await messageHelper.parseProxyTags(message.author.id, message.content, attachmentUrl).catch(e =>{throw e});
|
||||||
// If the message doesn't match a proxy, just return.
|
// If the message doesn't match a proxy, just return.
|
||||||
if (!proxyMatch.member) {
|
if (!proxyMatch || !proxyMatch.member) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs
|
// If the message does match a proxy but is not in a guild server (ex: in the Bot's DMs
|
||||||
if (!message.guildId) {
|
if (!message.guildId) {
|
||||||
throw new Error(enums.err.NOT_IN_SERVER);
|
throw new Error(enums.err.NOT_IN_SERVER);
|
||||||
}
|
}
|
||||||
|
await wh.replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
|
||||||
if (proxyMatch.message === enums.misc.ATTACHMENT_SENT_BY) {
|
|
||||||
return await message.reply(`${enums.misc.ATTACHMENT_SENT_BY} ${proxyMatch.member.displayname}`)
|
|
||||||
}
|
|
||||||
await replaceMessage(client, message, proxyMatch.message, proxyMatch.member).catch(e =>{throw e});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,10 +34,10 @@ wh.sendMessageAsMember = async function(client, message) {
|
|||||||
* @param {model} member - A member object from the database.
|
* @param {model} member - A member object from the database.
|
||||||
* @throws {Error} When there's no message to send.
|
* @throws {Error} When there's no message to send.
|
||||||
*/
|
*/
|
||||||
async function replaceMessage(client, message, text, member) {
|
wh.replaceMessage = async function(client, message, text, member) {
|
||||||
if (text.length > 0 || message.attachments.size > 0) {
|
if (text.length > 0 || message.attachments.size > 0) {
|
||||||
const channel = client.channels.get(message.channelId);
|
const channel = client.channels.get(message.channelId);
|
||||||
const webhook = await getOrCreateWebhook(client, channel).catch((e) =>{throw e});
|
const webhook = await wh.getOrCreateWebhook(client, channel).catch((e) =>{throw e});
|
||||||
const username = member.displayname ?? member.name;
|
const username = member.displayname ?? member.name;
|
||||||
await webhook.send({content: text, username: username, avatar_url: member.propic});
|
await webhook.send({content: text, username: username, avatar_url: member.propic});
|
||||||
await message.delete();
|
await message.delete();
|
||||||
@@ -59,10 +55,10 @@ async function replaceMessage(client, message, text, member) {
|
|||||||
* @returns {Webhook} A webhook object.
|
* @returns {Webhook} A webhook object.
|
||||||
* @throws {Error} When no webhooks are allowed in the channel.
|
* @throws {Error} When no webhooks are allowed in the channel.
|
||||||
*/
|
*/
|
||||||
async function getOrCreateWebhook(client, channel) {
|
wh.getOrCreateWebhook = async function(client, channel) {
|
||||||
// If channel doesn't allow webhooks
|
// If channel doesn't allow webhooks
|
||||||
if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
|
if (!channel?.createWebhook) throw new Error(enums.err.NO_WEBHOOKS_ALLOWED);
|
||||||
let webhook = await getWebhook(client, channel).catch((e) =>{throw e});
|
let webhook = await wh.getWebhook(client, channel).catch((e) =>{throw e});
|
||||||
if (!webhook) {
|
if (!webhook) {
|
||||||
webhook = await channel.createWebhook({name: name});
|
webhook = await channel.createWebhook({name: name});
|
||||||
}
|
}
|
||||||
@@ -76,7 +72,7 @@ async function getOrCreateWebhook(client, channel) {
|
|||||||
* @param {Channel} channel - The channel the message was sent in.
|
* @param {Channel} channel - The channel the message was sent in.
|
||||||
* @returns {Webhook} A webhook object.
|
* @returns {Webhook} A webhook object.
|
||||||
*/
|
*/
|
||||||
async function getWebhook(client, channel) {
|
wh.getWebhook = async function(client, channel) {
|
||||||
const channelWebhooks = await channel?.fetchWebhooks() ?? [];
|
const channelWebhooks = await channel?.fetchWebhooks() ?? [];
|
||||||
if (channelWebhooks.length === 0) {
|
if (channelWebhooks.length === 0) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user