2026-02-14 21:10:39 -05:00
import { db } from '../db.js' ;
2026-02-13 21:23:33 -05:00
import { enums } from "../enums.js" ;
2026-02-14 21:10:39 -05:00
import { loadImage } from "canvas" ;
2026-02-14 13:10:43 -05:00
import { EmptyResultError } from "sequelize" ;
2026-02-14 21:10:39 -05:00
import { EmbedBuilder } from "@fluxerjs/core" ;
2026-02-13 18:20:29 -05:00
const mh = { } ;
2026-02-13 22:18:43 -05:00
// Has an empty "command" to parse the help message properly
2026-02-14 21:10:39 -05:00
const commandList = [ '--help' , 'add' , 'remove' , 'name' , 'list' , 'displayName' , 'proxy' , 'propic' , '' ] ;
2026-02-13 21:45:01 -05:00
2026-02-13 21:23:33 -05:00
/ * *
* Parses through the subcommands that come after "pf;member" and calls functions accordingly .
*
2026-02-14 21:10:39 -05:00
* @ async
* @ param { string } authorId - The id of the message author
* @ param { string } authorFull - The username and discriminator of the message author
2026-02-13 21:23:33 -05:00
* @ param { string [ ] } args - The message arguments
2026-02-14 18:10:53 -05:00
* @ param { string | null } attachmentUrl - The message attachment url .
2026-02-14 22:39:56 -05:00
* @ param { string | null } attachmentExpiration - The message attachment expiration ( if uploaded via Fluxer )
2026-02-14 18:10:53 -05:00
* @ returns { Promise < string > | Promise < EmbedBuilder > } A message , or an informational embed .
2026-02-14 21:10:39 -05:00
* @ throws { Error }
2026-02-13 21:23:33 -05:00
* /
2026-02-14 22:11:04 -05:00
mh . parseMemberCommand = async function ( authorId , authorFull , args , attachmentUrl = null , attachmentExpiration = null ) {
2026-02-13 22:18:43 -05:00
let member ;
// checks whether command is in list, otherwise assumes it's a name
2026-02-13 21:45:01 -05:00
if ( ! commandList . includes ( args [ 0 ] ) ) {
2026-02-14 21:10:20 -05:00
member = await mh . getMemberInfo ( authorId , args [ 0 ] ) . catch ( ( e ) => { throw e } ) ;
2026-02-13 21:45:01 -05:00
}
2026-02-13 18:20:29 -05:00
switch ( args [ 0 ] ) {
2026-02-13 20:08:24 -05:00
case '--help' :
2026-02-13 21:18:44 -05:00
return enums . help . MEMBER ;
2026-02-13 20:08:24 -05:00
case 'add' :
2026-02-14 21:10:20 -05:00
return await mh . addNewMember ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-13 18:56:36 -05:00
case 'remove' :
2026-02-14 21:10:20 -05:00
return await mh . removeMember ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 13:37:51 -05:00
case 'name' :
return enums . help . NAME ;
2026-02-13 21:30:22 -05:00
case 'displayname' :
2026-02-13 22:18:43 -05:00
return enums . help . DISPLAY _NAME ;
2026-02-13 21:30:22 -05:00
case 'proxy' :
return enums . help . PROXY ;
2026-02-14 01:00:23 -05:00
case 'propic' :
return enums . help . PROPIC ;
2026-02-14 21:02:08 -05:00
case 'list' :
2026-02-14 21:10:20 -05:00
return await mh . getAllMembersInfo ( authorId , authorFull ) . catch ( ( e ) => { throw e } ) ;
2026-02-13 21:30:22 -05:00
case '' :
return enums . help . MEMBER ;
2026-02-13 18:20:29 -05:00
}
switch ( args [ 1 ] ) {
2026-02-14 13:37:51 -05:00
case 'name' :
2026-02-14 21:10:20 -05:00
return await mh . updateName ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-13 18:20:29 -05:00
case 'displayname' :
2026-02-14 21:10:20 -05:00
return await mh . updateDisplayName ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-13 18:56:36 -05:00
case 'proxy' :
2026-02-14 21:10:20 -05:00
return await mh . updateProxy ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 00:20:36 -05:00
case 'propic' :
2026-02-14 22:14:13 -05:00
return await mh . updatePropic ( authorId , args , attachmentUrl , attachmentExpiration ) . catch ( ( e ) => { throw e } ) ;
2026-02-13 18:20:29 -05:00
default :
2026-02-13 22:18:43 -05:00
return member ;
2026-02-13 18:20:29 -05:00
}
}
2026-02-13 21:23:33 -05:00
/ * *
2026-02-14 16:08:00 -05:00
* Adds a member .
2026-02-13 21:23:33 -05:00
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
* @ param { string [ ] } args - The message arguments
2026-02-14 12:55:36 -05:00
* @ returns { Promise < string > } A successful addition .
2026-02-14 15:05:48 -05:00
* @ throws { Error } When the member exists , or creating a member doesn ' t work .
2026-02-13 21:23:33 -05:00
* /
2026-02-14 21:02:08 -05:00
mh . addNewMember = async function ( authorId , args ) {
2026-02-13 20:38:51 -05:00
if ( args [ 1 ] && args [ 1 ] === "--help" || ! args [ 1 ] ) {
2026-02-13 21:18:44 -05:00
return enums . help . ADD ;
2026-02-13 18:20:29 -05:00
}
2026-02-13 20:38:51 -05:00
const memberName = args [ 1 ] ;
const displayName = args [ 2 ] ;
2026-02-14 21:02:08 -05:00
return await mh . addFullMember ( authorId , memberName , displayName ) . then ( ( member ) => {
let success = ` Member was successfully added. \n Name: ${ member . dataValues . name } `
success += displayName ? ` \n Display name: ${ member . dataValues . displayname } ` : "" ;
2026-02-13 18:20:29 -05:00
return success ;
} ) . catch ( e => {
2026-02-14 12:55:36 -05:00
throw new Error ( ` ${ enums . err . ADD _ERROR } : ${ e . message } ` )
2026-02-13 18:20:29 -05:00
} )
}
2026-02-14 13:37:51 -05:00
/ * *
* Updates the name for a member .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-14 13:37:51 -05:00
* @ param { string } authorId - The author of the message
* @ param { string [ ] } args - The message arguments
* @ returns { Promise < string > } A successful update .
* @ throws { RangeError } When the name doesn ' t exist .
* /
2026-02-14 21:02:08 -05:00
mh . updateName = async function ( authorId , args ) {
2026-02-14 13:37:51 -05:00
if ( args [ 1 ] && args [ 1 ] === "--help" || ! args [ 1 ] ) {
return enums . help . DISPLAY _NAME ;
}
const name = args [ 2 ] ;
2026-02-14 15:55:32 -05:00
const trimmedName = name ? name . trim ( ) : null ;
if ( ! name || trimmedName === null ) {
2026-02-14 13:37:51 -05:00
throw new RangeError ( ` Display name ${ enums . err . NO _VALUE } ` ) ;
}
2026-02-14 21:10:20 -05:00
return await mh . updateMemberField ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 13:37:51 -05:00
}
2026-02-13 21:23:33 -05:00
/ * *
* Updates the display name for a member .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
* @ param { string [ ] } args - The message arguments
2026-02-14 12:55:36 -05:00
* @ returns { Promise < string > } A successful update .
* @ throws { RangeError } When the display name is too long or doesn ' t exist .
2026-02-13 21:23:33 -05:00
* /
2026-02-14 21:02:08 -05:00
mh . updateDisplayName = async function ( authorId , args ) {
2026-02-13 20:38:51 -05:00
if ( args [ 1 ] && args [ 1 ] === "--help" || ! args [ 1 ] ) {
2026-02-13 22:18:43 -05:00
return enums . help . DISPLAY _NAME ;
2026-02-13 18:20:29 -05:00
}
2026-02-13 20:38:51 -05:00
const memberName = args [ 0 ] ;
const displayName = args [ 2 ] ;
2026-02-14 15:55:32 -05:00
const trimmedName = displayName ? displayName . trim ( ) : null ;
2026-02-13 20:38:51 -05:00
2026-02-14 15:55:32 -05:00
if ( ! displayName || trimmedName === null ) {
2026-02-14 21:02:08 -05:00
return await mh . getMemberByName ( authorId , memberName ) . then ( ( member ) => {
if ( member . displayname ) {
return ` Display name for ${ memberName } is: \" ${ member . displayname } \" . ` ;
}
throw new RangeError ( ` Display name ${ enums . err . NO _VALUE } ` ) ;
2026-02-14 21:10:20 -05:00
} ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 21:02:08 -05:00
2026-02-13 18:20:29 -05:00
}
2026-02-13 21:23:33 -05:00
else if ( displayName . length > 32 ) {
2026-02-14 12:55:36 -05:00
throw new RangeError ( enums . err . DISPLAY _NAME _TOO _LONG ) ;
2026-02-13 18:56:36 -05:00
}
2026-02-14 21:10:20 -05:00
return await mh . updateMemberField ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-13 18:20:29 -05:00
}
2026-02-13 21:23:33 -05:00
/ * *
* Updates the proxy for a member , first checking that no other members attached to the author have the tag .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
* @ param { string [ ] } args - The message arguments
2026-02-14 15:55:32 -05:00
* @ returns { Promise < string > } A successful update .
2026-02-14 12:55:36 -05:00
* @ throws { RangeError | Error } When an empty proxy was provided , or no proxy exists .
2026-02-13 21:23:33 -05:00
* /
2026-02-14 21:02:08 -05:00
mh . updateProxy = async function ( authorId , args ) {
2026-02-13 20:38:51 -05:00
if ( args [ 1 ] && args [ 1 ] === "--help" || ! args [ 1 ] ) {
2026-02-13 21:18:44 -05:00
return enums . help . PROXY ;
2026-02-13 20:38:51 -05:00
}
2026-02-14 21:02:08 -05:00
const proxyExists = await mh . checkIfProxyExists ( authorId , args [ 2 ] ) . then ( ( proxyExists ) => {
return proxyExists ;
2026-02-14 21:10:20 -05:00
} ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 15:55:32 -05:00
if ( ! proxyExists ) {
2026-02-14 21:10:20 -05:00
return await mh . updateMemberField ( authorId , args ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 15:55:32 -05:00
}
}
2026-02-14 01:00:23 -05:00
/ * *
* Updates the profile pic for a member , based on either the attachment or the args provided .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-14 01:00:23 -05:00
* @ param { string } authorId - The author of the message
* @ param { string [ ] } args - The message arguments
2026-02-14 22:11:04 -05:00
* @ param { string } attachmentUrl - The url of the first attachment in the message
2026-02-14 22:14:13 -05:00
* @ param { string | null } attachmentExpiry - The expiration date of the first attachment in the message ( if uploaded to Fluxer )
2026-02-14 12:55:36 -05:00
* @ returns { Promise < string > } A successful update .
* @ throws { Error } When loading the profile picture from a URL doesn ' t work .
2026-02-14 01:00:23 -05:00
* /
2026-02-14 22:14:13 -05:00
mh . updatePropic = async function ( authorId , args , attachmentUrl , attachmentExpiry = null ) {
2026-02-14 00:20:36 -05:00
if ( args [ 1 ] && args [ 1 ] === "--help" ) {
return enums . help . PROPIC ;
}
let img ;
const updatedArgs = args ;
2026-02-14 22:11:04 -05:00
if ( ! updatedArgs [ 1 ] && ! attachmentUrl ) {
2026-02-14 00:20:36 -05:00
return enums . help . PROPIC ;
2026-02-14 22:11:04 -05:00
} else if ( attachmentUrl ) {
updatedArgs [ 2 ] = attachmentUrl ;
updatedArgs [ 3 ] = attachmentExpiry ;
2026-02-14 00:20:36 -05:00
}
if ( updatedArgs [ 2 ] ) {
img = updatedArgs [ 2 ] ;
}
2026-02-14 21:02:08 -05:00
const loadedImage = await loadImage ( img ) . then ( ( li ) => {
return li ;
2026-02-14 00:20:36 -05:00
} ) . catch ( ( err ) => {
2026-02-14 12:55:36 -05:00
throw new Error ( ` ${ enums . err . PROPIC _CANNOT _LOAD } : ${ err . message } ` ) ;
2026-02-14 00:20:36 -05:00
} ) ;
2026-02-14 21:02:08 -05:00
if ( loadedImage ) {
2026-02-14 21:10:20 -05:00
return await mh . updateMemberField ( authorId , updatedArgs ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 21:02:08 -05:00
}
2026-02-14 00:20:36 -05:00
}
2026-02-13 21:23:33 -05:00
/ * *
* Removes a member .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
* @ param { string [ ] } args - The message arguments
2026-02-14 12:55:36 -05:00
* @ returns { Promise < string > } A successful removal .
* @ throws { EmptyResultError } When there is no member to remove .
2026-02-13 21:23:33 -05:00
* /
2026-02-14 21:02:08 -05:00
mh . removeMember = async function ( authorId , args ) {
2026-02-14 12:55:36 -05:00
if ( args [ 1 ] && args [ 1 ] === "--help" || ! args [ 1 ] ) {
2026-02-13 21:18:44 -05:00
return enums . help . REMOVE ;
2026-02-13 20:38:51 -05:00
}
const memberName = args [ 1 ] ;
return await db . members . destroy ( { where : { name : memberName , userid : authorId } } ) . then ( ( ) => {
2026-02-14 10:07:27 -05:00
return ` Member " ${ memberName } " has been deleted. ` ;
2026-02-13 18:20:29 -05:00
} ) . catch ( e => {
2026-02-14 12:55:36 -05:00
throw new EmptyResultError ( ` ${ enums . err . NO _MEMBER } : ${ e . message } ` ) ;
2026-02-13 18:20:29 -05:00
} ) ;
}
2026-02-14 00:20:36 -05:00
/*======Non-Subcommands======*/
2026-02-13 21:23:33 -05:00
2026-02-14 16:08:00 -05:00
/ * *
* Adds a member with full details , first checking that there is no member of that name associated with the author .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-14 16:08:00 -05:00
* @ param { string } authorId - The author of the message
* @ param { string } memberName - The name of the member .
2026-02-14 21:49:01 -05:00
* @ param { string | null } displayName - The display name of the member .
2026-02-14 21:02:08 -05:00
* @ param { string | null } proxy - The proxy tag of the member .
* @ param { string | null } propic - The profile picture URL of the member .
2026-02-15 00:23:30 -05:00
* @ returns { Promise < model > } A successful addition .
2026-02-14 16:08:00 -05:00
* @ throws { Error | RangeError } When the member already exists , there are validation errors , or adding a member doesn ' t work .
* /
2026-02-14 21:49:01 -05:00
mh . addFullMember = async function ( authorId , memberName , displayName = null , proxy = null , propic = null ) {
2026-02-15 00:23:30 -05:00
const member = await mh . getMemberByName ( authorId , memberName ) . catch ( ( e ) => { console . log ( "Now we can add the member." ) } ) ;
2026-02-14 15:55:32 -05:00
if ( member ) {
2026-02-14 16:08:00 -05:00
throw new Error ( ` Can't add ${ memberName } . ${ enums . err . MEMBER _EXISTS } ` ) ;
2026-02-14 15:55:32 -05:00
}
2026-02-14 21:49:01 -05:00
if ( displayName ) {
const trimmedName = displayName ? displayName . trim ( ) : null ;
if ( trimmedName && trimmedName . length > 32 ) {
throw new RangeError ( ` Can't add ${ memberName } . ${ enums . err . DISPLAY _NAME _TOO _LONG } ` ) ;
}
}
if ( proxy ) {
await mh . checkIfProxyExists ( authorId , proxy ) . catch ( ( e ) => { throw e } ) ;
2026-02-14 15:55:32 -05:00
}
if ( propic ) {
await loadImage ( propic ) . catch ( ( err ) => {
2026-02-14 16:08:00 -05:00
throw new Error ( ` Can't add ${ memberName } . ${ enums . err . PROPIC _CANNOT _LOAD } : ${ err . message } ` ) ;
2026-02-14 15:55:32 -05:00
} ) ;
}
return await db . members . create ( {
name : memberName ,
userid : authorId ,
displayname : displayName ,
proxy : proxy ,
propic : propic ,
} ) . catch ( e => {
2026-02-14 16:08:00 -05:00
throw new Error ( ` Can't add ${ memberName } . ${ enums . err . ADD _ERROR } : ${ e . message } ` )
2026-02-14 15:55:32 -05:00
} )
}
2026-02-13 21:23:33 -05:00
/ * *
2026-02-14 15:55:32 -05:00
* Updates one fields for a member in the database .
2026-02-13 21:23:33 -05:00
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
* @ param { string [ ] } args - The message arguments
2026-02-14 12:55:36 -05:00
* @ returns { Promise < string > } A successful update .
* @ throws { EmptyResultError | Error } When the member is not found , or catchall error .
2026-02-13 21:23:33 -05:00
* /
2026-02-14 21:02:08 -05:00
mh . updateMemberField = async function ( authorId , args ) {
2026-02-13 20:38:51 -05:00
const memberName = args [ 0 ] ;
const columnName = args [ 1 ] ;
const value = args [ 2 ] ;
2026-02-14 01:00:23 -05:00
let fluxerPropicWarning ;
// indicates that an attachment was uploaded on Fluxer directly
if ( columnName === "propic" && args [ 3 ] ) {
2026-02-14 21:02:08 -05:00
fluxerPropicWarning = mh . setExpirationWarning ( args [ 3 ] ) ;
2026-02-14 01:00:23 -05:00
}
2026-02-13 20:38:51 -05:00
return await db . members . update ( { [ columnName ] : value } , { where : { name : memberName , userid : authorId } } ) . then ( ( ) => {
2026-02-14 10:07:27 -05:00
return ` Updated ${ columnName } for ${ memberName } to " ${ value } " ${ fluxerPropicWarning ? ? '' } . ` ;
2026-02-13 18:20:29 -05:00
} ) . catch ( e => {
2026-02-14 12:55:36 -05:00
if ( e === EmptyResultError ) {
2026-02-14 16:08:00 -05:00
throw new EmptyResultError ( ` Can't update ${ memberName } . ${ enums . err . NO _MEMBER } : ${ e . message } ` ) ;
2026-02-14 12:55:36 -05:00
}
else {
2026-02-14 16:08:00 -05:00
throw new Error ( ` Can't update ${ memberName } . ${ e . message } ` ) ;
2026-02-14 12:55:36 -05:00
}
2026-02-13 18:20:29 -05:00
} ) ;
}
2026-02-14 01:00:23 -05:00
/ * *
* Sets the warning for an expiration date .
*
* @ param { string } expirationString - An expiration date string .
2026-02-14 12:55:36 -05:00
* @ returns { string } A description of the expiration , interpolating the expiration string .
2026-02-14 01:00:23 -05:00
* /
2026-02-14 22:14:13 -05:00
mh . setExpirationWarning = function ( expirationString ) {
2026-02-14 01:00:23 -05:00
let expirationDate = new Date ( expirationString ) ;
if ( ! isNaN ( expirationDate . valueOf ( ) ) ) {
expirationDate = expirationDate . toDateString ( ) ;
return ` \n **NOTE:** Because this profile picture was uploaded via Fluxer, it will currently expire on * ${ expirationDate } *. To avoid this, upload the picture to another website like <https://imgbb.com/> and link to it directly. `
}
}
2026-02-13 21:23:33 -05:00
/ * *
* Gets the details for a member .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
* @ param { string } memberName - The message arguments
2026-02-14 14:27:28 -05:00
* @ returns { Promise < EmbedBuilder > } The member ' s info .
2026-02-14 21:02:08 -05:00
* @ throws { Error }
2026-02-13 21:23:33 -05:00
* /
2026-02-14 21:02:08 -05:00
mh . getMemberInfo = async function ( authorId , memberName ) {
return await mh . getMemberByName ( authorId , memberName ) . then ( ( member ) => {
return new EmbedBuilder ( )
2026-02-14 21:14:59 -05:00
. setTitle ( member . name )
. setDescription ( ` Details for ${ member . name } ` )
2026-02-14 21:02:08 -05:00
. addFields (
{ name : 'Display name: ' , value : member . displayname ? ? 'unset' , inline : true } ,
{ name : 'Proxy tag: ' , value : member . proxy ? ? 'unset' , inline : true } ,
)
. setImage ( member . propic ) ;
2026-02-14 21:10:20 -05:00
} ) . catch ( ( e ) => { throw e } )
2026-02-13 20:38:51 -05:00
}
2026-02-14 14:27:28 -05:00
/ * *
* Gets all members for an author .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-14 14:27:28 -05:00
* @ param { string } authorId - The id of the message author
* @ param { string } authorName - The id name the message author
* @ returns { Promise < EmbedBuilder > } The info for all members .
2026-02-14 21:02:08 -05:00
* @ throws { Error }
2026-02-14 14:27:28 -05:00
* /
2026-02-14 21:02:08 -05:00
mh . getAllMembersInfo = async function ( authorId , authorName ) {
2026-02-14 21:10:20 -05:00
const members = await mh . getMembersByAuthor ( authorId ) . catch ( e => { throw e } ) ;
2026-02-14 21:49:01 -05:00
const fields = [ ... members . entries ( ) ] . map ( ( [ name , member ] ) => ( {
2026-02-14 14:27:28 -05:00
name : member . name ,
2026-02-14 21:49:01 -05:00
value : ` (Proxy: \` ${ member . proxy ? ? "unset" } \` ) ` ,
2026-02-14 14:27:28 -05:00
inline : true ,
} ) ) ;
return new EmbedBuilder ( )
. setTitle ( ` Members for ${ authorName } ` )
. addFields ( ... fields ) ;
}
2026-02-13 21:23:33 -05:00
/ * *
* Gets a member based on the author and proxy tag .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message .
2026-02-14 16:08:00 -05:00
* @ param { string } memberName - The member ' s name .
2026-02-14 12:55:36 -05:00
* @ returns { Promise < model > } The member object .
* @ throws { EmptyResultError } When the member is not found .
2026-02-13 21:23:33 -05:00
* /
2026-02-14 16:08:00 -05:00
mh . getMemberByName = async function ( authorId , memberName ) {
2026-02-14 22:39:56 -05:00
return await db . members . findOne ( { where : { userid : authorId , name : memberName } } ) . then ( ( result ) => {
if ( ! result ) {
2026-02-14 22:41:45 -05:00
throw new EmptyResultError ( ` Can't get ${ memberName } . ${ enums . err . NO _MEMBER } ` ) ;
2026-02-14 22:39:56 -05:00
}
return result ;
2026-02-13 21:23:33 -05:00
} ) ;
}
/ * *
* Gets a member based on the author and proxy tag .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
* @ param { string } proxy - The proxy tag
2026-02-14 12:55:36 -05:00
* @ returns { Promise < model > } The member object .
* @ throws { EmptyResultError } When the member is not found .
2026-02-13 21:23:33 -05:00
* /
2026-02-13 20:38:51 -05:00
mh . getMemberByProxy = async function ( authorId , proxy ) {
2026-02-14 22:39:56 -05:00
return await db . members . findOne ( { where : { userid : authorId , proxy : proxy } } ) . then ( ( result ) => {
if ( ! result ) {
2026-02-14 22:41:45 -05:00
throw new EmptyResultError ( ` Can't find member with that proxy. ${ enums . err . NO _MEMBER } . ` ) ;
2026-02-14 22:39:56 -05:00
}
return result ;
2026-02-13 18:20:29 -05:00
} ) ;
}
2026-02-13 21:23:33 -05:00
/ * *
* Gets all members belonging to the author .
*
2026-02-14 21:02:08 -05:00
* @ async
2026-02-13 21:23:33 -05:00
* @ param { string } authorId - The author of the message
2026-02-14 12:55:36 -05:00
* @ returns { Promise < model [ ] > } The member object array .
* @ throws { EmptyResultError } When no members are found .
2026-02-13 21:23:33 -05:00
* /
2026-02-13 20:38:51 -05:00
mh . getMembersByAuthor = async function ( authorId ) {
2026-02-14 22:39:56 -05:00
return await db . members . findAll ( { where : { userid : authorId } } ) . then ( ( result ) => {
if ( result . length === 0 ) {
throw new EmptyResultError ( ` ${ enums . err . USER _NO _MEMBERS } : ${ e . message } ` ) ;
}
return result ;
2026-02-13 18:20:29 -05:00
} ) ;
}
2026-02-14 22:11:04 -05:00
/ * *
* Checks if proxy exists for a member .
*
* @ param { string } authorId - The author of the message
* @ param { string } proxy - The proxy tag .
* @ returns { Promise < boolean > } Whether the proxy exists .
* @ throws { Error } When an empty proxy was provided , or no proxy exists .
* /
mh . checkIfProxyExists = async function ( authorId , proxy ) {
const trimmedProxy = proxy ? proxy . trim ( ) : null ;
if ( trimmedProxy == null ) throw new RangeError ( ` Proxy ${ enums . err . NO _VALUE } ` ) ;
const splitProxy = proxy . trim ( ) . split ( "text" ) ;
if ( splitProxy . length < 2 ) throw new Error ( enums . err . NO _TEXT _FOR _PROXY ) ;
if ( ! splitProxy [ 0 ] && ! splitProxy [ 1 ] ) throw new Error ( enums . err . NO _PROXY _WRAPPER ) ;
await mh . getMembersByAuthor ( authorId ) . then ( ( memberList ) => {
const proxyExists = memberList . some ( member => member . proxy === proxy ) ;
if ( proxyExists ) {
throw new Error ( enums . err . PROXY _EXISTS ) ;
}
} ) . catch ( e => { throw e } ) ;
}
2026-02-13 18:20:29 -05:00
export const memberHelper = mh ;