diff --git a/src/commands/SearchCommand.ts b/src/commands/SearchCommand.ts index 0cf52c97..be888f85 100644 --- a/src/commands/SearchCommand.ts +++ b/src/commands/SearchCommand.ts @@ -11,11 +11,27 @@ export default class SearchCommand extends PrefixCommand { return false; } - const plainArgs = args.replace( /"|<|>/g, '' ); + const plainArgs = args.replace( /<|>/g, '' ); + const modifierRegex = new RegExp( /_[a-z]+\s(([a-zA-Z0-9]+)|(["'][^"']+["']))|:query/, 'g' ); + const modifiers = plainArgs.match( modifierRegex ); + const textArgs = plainArgs.replace( modifierRegex, '' ).trim(); try { const embed = new MessageEmbed(); - const searchFilter = `text ~ "${ plainArgs }" AND project in (${ BotConfig.projects.join( ', ' ) })`; + let searchFilter = `project in (${ BotConfig.projects.join( ', ' ) })`; + + if ( modifiers ) { + for ( const modifier of modifiers ) { + if ( modifier == ':query' ) { + searchFilter = textArgs; + break; + } + if ( [ 'project', 'creator', 'reporter', 'assignee', 'version', 'resolution', 'status', 'fixversion', 'confirmation', 'gamemode', 'id', 'labels', 'key', 'priority', 'mp' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase().replace( 'confirmation', '"Confirmation Status"' ).replace( 'gamemode', '"Game Mode"' ).replace( 'mp', '"Mojang Priority"' ) } = ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }`; + if ( [ 'comment', 'summary', 'description', 'environment' ].includes( modifier.split( /_|\s/g )[1] ) ) searchFilter += ` AND ${ modifier.split( /_|\s/g )[1].toLowerCase() } ~ ${ modifier.split( /\s/g ).slice( 1 ).join( ' ' ) }`; + } + } + if ( textArgs && !modifiers.includes( ':query' ) ) searchFilter += ` AND text ~ "${ textArgs }"`; + const searchResults = await MojiraBot.jira.issueSearch.searchForIssuesUsingJqlGet( { jql: searchFilter, maxResults: BotConfig.maxSearchResults, @@ -23,7 +39,7 @@ export default class SearchCommand extends PrefixCommand { } ); if ( !searchResults.issues ) { - embed.setTitle( `No results found for "${ Util.escapeMarkdown( plainArgs ) }"` ); + embed.setTitle( `No results found for "${ Util.escapeMarkdown( textArgs ) }"` ); await message.channel.send( { embeds: [embed] } ); return false; } @@ -41,7 +57,7 @@ export default class SearchCommand extends PrefixCommand { await message.channel.send( { embeds: [embed] } ); } catch { const embed = new MessageEmbed(); - embed.setTitle( `No results found for "${ Util.escapeMarkdown( plainArgs ) }"` ); + embed.setTitle( `No results found for "${ Util.escapeMarkdown( textArgs ) }"` ); await message.channel.send( { embeds: [embed] } ); return false; } @@ -52,4 +68,4 @@ export default class SearchCommand extends PrefixCommand { public asString( args: string ): string { return `!jira search ${ args }`; } -} +} \ No newline at end of file