-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] }); | ||
}); |