-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSearchCommand.ts
64 lines (54 loc) · 2.43 KB
/
SearchCommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { EmbedBuilder, escapeMarkdown, ChatInputCommandInteraction } from 'discord.js';
import SlashCommand from './commandHandlers/SlashCommand.js';
import BotConfig from '../BotConfig.js';
import MojiraBot from '../MojiraBot.js';
import { ChannelConfigUtil } from '../util/ChannelConfigUtil.js';
export default class SearchCommand extends SlashCommand {
public readonly slashCommandBuilder = this.slashCommandBuilder
.setName( 'search' )
.setDescription( 'Search for issues in Jira.' )
.addStringOption( option =>
option.setName( 'query' )
.setDescription( 'The query to search for.' )
.setRequired( true )
);
public async run( interaction: ChatInputCommandInteraction ): Promise<boolean> {
const plainArgs = interaction.options.getString( 'query' )?.replace( /"|<|>/g, '' );
if ( plainArgs == null ) return false;
try {
const embed = new EmbedBuilder();
const searchFilter = `(description ~ "${ plainArgs }" OR summary ~ "${ plainArgs }") AND project in (${ BotConfig.projects.join( ', ' ) })`;
const searchResults = await MojiraBot.jira.issueSearch.searchForIssuesUsingJql( {
jql: searchFilter,
maxResults: BotConfig.maxSearchResults,
fields: [ 'key', 'summary' ],
} );
if ( !searchResults.issues ) {
embed.setTitle( `No results found for "${ escapeMarkdown( plainArgs ) }"` );
await interaction.reply( { embeds: [embed], ephemeral: true } );
return true;
}
embed.setTitle( '**Results:**' );
embed.setFooter( { text: interaction.user.tag, iconURL: interaction.user.avatarURL() ?? undefined } );
for ( const issue of searchResults.issues ) {
embed.addFields( {
name: issue.key,
value: `[${ issue.fields.summary }](https://bugs.mojang.com/browse/${ issue.key })`,
} );
}
const escapedJql = encodeURIComponent( searchFilter ).replace( /\(/g, '%28' ).replace( /\)/g, '%29' );
embed.setDescription( `__[See all results](https://bugs.mojang.com/issues/?jql=${ escapedJql })__` );
if ( interaction.channel !== null && ChannelConfigUtil.publicSearch( interaction.channel ) ) {
await interaction.reply( { embeds: [embed], ephemeral: false } );
} else {
await interaction.reply( { embeds: [embed], ephemeral: true } );
}
} catch {
const embed = new EmbedBuilder();
embed.setTitle( `No results found for "${ escapeMarkdown( plainArgs ) }"` );
await interaction.reply( { embeds: [embed], ephemeral: true } );
return false;
}
return true;
}
}