mirror of
https://github.com/pieartsy/PluralFlux.git
synced 2026-04-16 17:45:28 +10:00
changed error handling to not return error when database returns null as sometimes we want that
This commit is contained in:
@@ -24,7 +24,7 @@ mh.parseMemberCommand = async function(authorId, authorFull, args, attachmentUrl
|
|||||||
let member;
|
let member;
|
||||||
// checks whether command is in list, otherwise assumes it's a name
|
// checks whether command is in list, otherwise assumes it's a name
|
||||||
if(!commandList.includes(args[0])) {
|
if(!commandList.includes(args[0])) {
|
||||||
member = await mh.getMemberInfo(authorId, args[0]).catch((e) =>{throw e});
|
member = await mh.getMemberInfo(authorId, args[0]);
|
||||||
}
|
}
|
||||||
switch(args[0]) {
|
switch(args[0]) {
|
||||||
case '--help':
|
case '--help':
|
||||||
@@ -127,12 +127,13 @@ mh.updateDisplayName = async function(authorId, args) {
|
|||||||
|
|
||||||
if (!displayName || trimmedName === null ) {
|
if (!displayName || trimmedName === null ) {
|
||||||
return await mh.getMemberByName(authorId, memberName).then((member) => {
|
return await mh.getMemberByName(authorId, memberName).then((member) => {
|
||||||
if (member.displayname) {
|
if (member && member.displayname) {
|
||||||
return `Display name for ${memberName} is: \"${member.displayname}\".`;
|
return `Display name for ${memberName} is: \"${member.displayname}\".`;
|
||||||
}
|
}
|
||||||
|
else if (member) {
|
||||||
throw new RangeError(`Display name ${enums.err.NO_VALUE}`);
|
throw new RangeError(`Display name ${enums.err.NO_VALUE}`);
|
||||||
}).catch((e) =>{throw e});
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else if (displayName.length > 32) {
|
else if (displayName.length > 32) {
|
||||||
throw new RangeError(enums.err.DISPLAY_NAME_TOO_LONG);
|
throw new RangeError(enums.err.DISPLAY_NAME_TOO_LONG);
|
||||||
@@ -187,7 +188,6 @@ mh.updatePropic = async function(authorId, args, attachmentUrl, attachmentExpiry
|
|||||||
if (updatedArgs[2]) {
|
if (updatedArgs[2]) {
|
||||||
img = updatedArgs[2];
|
img = updatedArgs[2];
|
||||||
}
|
}
|
||||||
console.log(img);
|
|
||||||
const isValidImage = await mh.checkImageFormatValidity(img).catch((e) =>{throw e});
|
const isValidImage = await mh.checkImageFormatValidity(img).catch((e) =>{throw e});
|
||||||
if (isValidImage) {
|
if (isValidImage) {
|
||||||
return await mh.updateMemberField(authorId, updatedArgs).catch((e) =>{throw e});
|
return await mh.updateMemberField(authorId, updatedArgs).catch((e) =>{throw e});
|
||||||
@@ -249,10 +249,11 @@ mh.removeMember = async function(authorId, args) {
|
|||||||
* @throws {Error | RangeError} When the member already exists, there are validation errors, or adding a member doesn't work.
|
* @throws {Error | RangeError} When the member already exists, there are validation errors, or adding a member doesn't work.
|
||||||
*/
|
*/
|
||||||
mh.addFullMember = async function(authorId, memberName, displayName = null, proxy = null, propic= null) {
|
mh.addFullMember = async function(authorId, memberName, displayName = null, proxy = null, propic= null) {
|
||||||
const member = await mh.getMemberByName(authorId, memberName).catch(() =>{console.log("Now we can add the member.")});
|
await mh.getMemberByName(authorId, memberName).then((member) => {
|
||||||
if (member) {
|
if (member) {
|
||||||
throw new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`);
|
throw new Error(`Can't add ${memberName}. ${enums.err.MEMBER_EXISTS}`);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
if (displayName) {
|
if (displayName) {
|
||||||
const trimmedName = displayName ? displayName.trim() : null;
|
const trimmedName = displayName ? displayName.trim() : null;
|
||||||
if (trimmedName && trimmedName.length > 32) {
|
if (trimmedName && trimmedName.length > 32) {
|
||||||
@@ -329,10 +330,10 @@ mh.setExpirationWarning = function(expirationString) {
|
|||||||
* @param {string} authorId - The author of the message
|
* @param {string} authorId - The author of the message
|
||||||
* @param {string} memberName - The message arguments
|
* @param {string} memberName - The message arguments
|
||||||
* @returns {Promise<EmbedBuilder>} The member's info.
|
* @returns {Promise<EmbedBuilder>} The member's info.
|
||||||
* @throws {Error}
|
|
||||||
*/
|
*/
|
||||||
mh.getMemberInfo = async function (authorId, memberName) {
|
mh.getMemberInfo = async function (authorId, memberName) {
|
||||||
return await mh.getMemberByName(authorId, memberName).then((member) => {
|
return await mh.getMemberByName(authorId, memberName).then((member) => {
|
||||||
|
if (member) {
|
||||||
return new EmbedBuilder()
|
return new EmbedBuilder()
|
||||||
.setTitle(member.name)
|
.setTitle(member.name)
|
||||||
.setDescription(`Details for ${member.name}`)
|
.setDescription(`Details for ${member.name}`)
|
||||||
@@ -341,7 +342,8 @@ mh.getMemberInfo = async function(authorId, memberName) {
|
|||||||
{name: 'Proxy tag: ', value: member.proxy ?? 'unset', inline: true},
|
{name: 'Proxy tag: ', value: member.proxy ?? 'unset', inline: true},
|
||||||
)
|
)
|
||||||
.setImage(member.propic);
|
.setImage(member.propic);
|
||||||
}).catch((e) =>{throw e})
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -351,10 +353,11 @@ mh.getMemberInfo = async function(authorId, memberName) {
|
|||||||
* @param {string} authorId - The id of the message author
|
* @param {string} authorId - The id of the message author
|
||||||
* @param {string} authorName - The id name the message author
|
* @param {string} authorName - The id name the message author
|
||||||
* @returns {Promise<EmbedBuilder>} The info for all members.
|
* @returns {Promise<EmbedBuilder>} The info for all members.
|
||||||
* @throws {Error}
|
* @throws {Error} When there are no members for an author.
|
||||||
*/
|
*/
|
||||||
mh.getAllMembersInfo = async function(authorId, authorName) {
|
mh.getAllMembersInfo = async function(authorId, authorName) {
|
||||||
const members = await mh.getMembersByAuthor(authorId).catch(e =>{throw e});
|
const members = await mh.getMembersByAuthor(authorId);
|
||||||
|
if (members == null) throw Error(enums.err.USER_NO_MEMBERS);
|
||||||
const fields = [...members.entries()].map(([name, member]) => ({
|
const fields = [...members.entries()].map(([name, member]) => ({
|
||||||
name: member.name,
|
name: member.name,
|
||||||
value: `(Proxy: \`${member.proxy ?? "unset"}\`)`,
|
value: `(Proxy: \`${member.proxy ?? "unset"}\`)`,
|
||||||
@@ -375,12 +378,7 @@ mh.getAllMembersInfo = async function(authorId, authorName) {
|
|||||||
* @throws { EmptyResultError } When the member is not found.
|
* @throws { EmptyResultError } When the member is not found.
|
||||||
*/
|
*/
|
||||||
mh.getMemberByName = async function(authorId, memberName) {
|
mh.getMemberByName = async function(authorId, memberName) {
|
||||||
return await db.members.findOne({ where: { userid: authorId, name: memberName } }).then((result) => {
|
return await db.members.findOne({ where: { userid: authorId, name: memberName } });
|
||||||
if (!result) {
|
|
||||||
throw new EmptyResultError(`Can't get ${memberName}. ${enums.err.NO_MEMBER}`);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -390,15 +388,9 @@ mh.getMemberByName = async function(authorId, memberName) {
|
|||||||
* @param {string} authorId - The author of the message
|
* @param {string} authorId - The author of the message
|
||||||
* @param {string} proxy - The proxy tag
|
* @param {string} proxy - The proxy tag
|
||||||
* @returns {Promise<model>} The member object.
|
* @returns {Promise<model>} The member object.
|
||||||
* @throws { EmptyResultError } When the member is not found.
|
|
||||||
*/
|
*/
|
||||||
mh.getMemberByProxy = async function(authorId, proxy) {
|
mh.getMemberByProxy = async function(authorId, proxy) {
|
||||||
return await db.members.findOne({ where: { userid: authorId, proxy: proxy } }).then((result) => {
|
return await db.members.findOne({ where: { userid: authorId, proxy: proxy } });
|
||||||
if (!result) {
|
|
||||||
throw new EmptyResultError(`Can't find member with that proxy. ${enums.err.NO_MEMBER}.`);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -406,16 +398,10 @@ mh.getMemberByProxy = async function(authorId, proxy) {
|
|||||||
*
|
*
|
||||||
* @async
|
* @async
|
||||||
* @param {string} authorId - The author of the message
|
* @param {string} authorId - The author of the message
|
||||||
* @returns {Promise<model[]>} The member object array.
|
* @returns {Promise<model[] | null>} The member object array.
|
||||||
* @throws { EmptyResultError } When no members are found.
|
|
||||||
*/
|
*/
|
||||||
mh.getMembersByAuthor = async function(authorId) {
|
mh.getMembersByAuthor = async function(authorId) {
|
||||||
return await db.members.findAll({ where: { userid: authorId } }).then((result) => {
|
return await db.members.findAll({ where: { userid: authorId } });
|
||||||
if (result.length === 0) {
|
|
||||||
throw new EmptyResultError(`${enums.err.USER_NO_MEMBERS}: ${e.message}`);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -31,15 +31,20 @@ msgh.parseCommandArgs = function(content, commandName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses proxy tags and sees if they match the tags of any member belonging to an author.
|
* Parses messages to see if any part of the text matches the tags of any member belonging to an author.
|
||||||
*
|
*
|
||||||
* @param {string} authorId - The author of the message.
|
* @param {string} authorId - The author of the message.
|
||||||
* @param {Object} attachment - An attachment for the message, if any exists.
|
* @param {Object} attachment - An attachment for the message, if any exists.
|
||||||
* @param {string} content - The full message content.
|
* @param {string} content - The full message content.
|
||||||
* @returns {Object} The proxy message object.
|
* @returns {Object} The proxy message object.
|
||||||
|
* @throws {Error} If a proxy message is sent with no message within it.
|
||||||
*/
|
*/
|
||||||
msgh.parseProxyTags = async function (authorId, attachment, content){
|
msgh.parseProxyTags = async function (authorId, attachment, content){
|
||||||
const members = await memberHelper.getMembersByAuthor(authorId).catch(e =>{throw e});
|
const members = await memberHelper.getMembersByAuthor(authorId);
|
||||||
|
// If an author has no members, no sense in searching for proxy
|
||||||
|
if (members.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const proxyMessage = {}
|
const proxyMessage = {}
|
||||||
members.filter(member => member.proxy).forEach(member => {
|
members.filter(member => member.proxy).forEach(member => {
|
||||||
|
|||||||
@@ -24,9 +24,11 @@ wh.sendMessageAsMember = async function(client, message, content) {
|
|||||||
if (!message.guildId) {
|
if (!message.guildId) {
|
||||||
throw new Error(enums.err.NOT_IN_SERVER);
|
throw new Error(enums.err.NOT_IN_SERVER);
|
||||||
}
|
}
|
||||||
const member = await memberHelper.getMemberByProxy(message.author.id, proxyMatch.proxy).catch(e =>{throw e});
|
const member = await memberHelper.getMemberByProxy(message.author.id, proxyMatch.proxy);
|
||||||
|
if (member) {
|
||||||
await replaceMessage(client, message, message.channelId, proxyMatch.message, member).catch(e =>{throw e});
|
await replaceMessage(client, message, message.channelId, proxyMatch.message, member).catch(e =>{throw e});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces a proxied message with a webhook using the member information.
|
* Replaces a proxied message with a webhook using the member information.
|
||||||
|
|||||||
Reference in New Issue
Block a user