Skip to content

Commit

Permalink
feat: adds docSearch.ts (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
benw202 authored Jan 22, 2024
1 parent 16bd70c commit 9626be0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Update <small>_ January 2024</small>

- feat: docSearch command (22/01/2024)
- fix: vatsim events max fields (20/01/2024)
- fix: user and mod log exclude id's (18/01/2024)
- feat: New FBW Utils and Moderation bot (08/01/2024)
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@hokify/agenda": "^6.0.0",
"@octokit/request": "^8.1.1",
"bad-words": "^3.0.4",
"config": "^3.3.9",
"discord.js": "^14.11.0",
"moment": "^2.29.4",
Expand All @@ -26,6 +27,7 @@
},
"devDependencies": {
"@flybywiresim/eslint-config": "^0.1.0",
"@types/bad-words": "^3.0.3",
"@types/config": "^3.3.1",
"@types/node": "^18.0.0",
"@types/node-fetch": "^2.6.10",
Expand Down
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import birthday from './utils/birthday/birthday';
import count from './utils/count';
import vatsim from './utils/vatsim/vatsim';
import help from './utils/help';
import docSearch from './utils/docSearch';

const commandArray: SlashCommand[] = [
ping,
Expand Down Expand Up @@ -53,6 +54,7 @@ const commandArray: SlashCommand[] = [
count,
vatsim,
help,
docSearch,
];

export default commandArray;
58 changes: 58 additions & 0 deletions src/commands/utils/docSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ApplicationCommandOptionType, ApplicationCommandType, Colors } from 'discord.js';
import Filter from 'bad-words';
import { slashCommand, slashCommandStructure, makeEmbed } from '../../lib';

const data = slashCommandStructure({
name: 'doc_search',
description: 'Searches the FlyByWire Documentation for a given query.',
type: ApplicationCommandType.ChatInput,
options: [{
name: 'query',
description: 'The query to search for.',
type: ApplicationCommandOptionType.String,
max_length: 100,
required: true,
}],
});

const DOCS_BASE_URL = 'https://docs.flybywiresim.com';

export default slashCommand(data, async ({ interaction }) => {
const query = interaction.options.getString('query')!;

// Separates the query into an array.
const words = query.split(/\s+/);

// Itterate through the array and check if any of the words are a URL. Then check if any of the words are profanity.
for (const searchWord of words) {
try {
const _ = new URL(searchWord);
const URLEmbed = makeEmbed({
title: 'FlyByWire Documentation | Error',
description: 'Providing URLs to the Documentation search command is not allowed.',
color: Colors.Red,
});
return interaction.reply({ embeds: [URLEmbed] });
} catch (_) { /**/ }

const filter = new Filter();
if (filter.isProfane(searchWord)) {
const profanityEmbed = makeEmbed({
title: 'FlyByWire Documentation | Error',
description: 'Providing profanity to the Documentation search command is not allowed.',
color: Colors.Red,
});

return interaction.reply({ embeds: [profanityEmbed] });
}
}

// Safety to prevent users from entering unexpected data that might result in strange behavior in a URL.
const encodedSearchQuery = encodeURIComponent(query);

const queryEmbed = makeEmbed({
title: 'FlyByWire Documentation Search',
description: `Search the FlyByWire Documentation for "${query}" [here](${DOCS_BASE_URL}/?q=${encodedSearchQuery}).`,
});
return interaction.reply({ embeds: [queryEmbed] });
});

0 comments on commit 9626be0

Please sign in to comment.