Repository: TheOdinProject/odin-bot-v2
Pull request #446 (2023-10-02 08:00:03)

!searchtop legacy command added
<!-- Thank you for taking the time to contribute to The Odin Project. In order to get this pull request (PR) merged in a reasonable amount of time, you must complete this entire template. -->

## Because
<!-- Summarize the purpose or reasons for this PR, e.g. what problem it solves or what benefit it provides. -->

- we are moving to slash based commands. the search commands are often used in a sentence and this one was not created in legacy form yet. 

## This PR
<!-- A bullet point list of one or more items describing the specific changes. -->
- adds `!searchtop` command with the same bot out put as `/searchtop`
- adds test file and snapshot

![Screenshot 2023-10-02 at 09 51 49](https://github.com/TheOdinProject/odin-bot-v2/assets/81025586/cb7fba59-704a-425e-be81-86be485252d3)
![Screenshot 2023-10-02 at 09 52 08](https://github.com/TheOdinProject/odin-bot-v2/assets/81025586/d65d10a7-84e2-49e8-82d6-f3179a4e6df8)
![Screenshot 2023-10-02 at 09 52 19](https://github.com/TheOdinProject/odin-bot-v2/assets/81025586/e84c2b0f-3b10-4e9e-ac48-7a3ec2f8fa25)

## Issue
<!--
If this PR closes an open issue in this repo, replace the XXXXX below with the issue number, e.g. Closes #2013.

If this PR closes an open issue in another TOP repo, replace the #XXXXX with the URL of the issue, e.g. Closes https://github.com/TheOdinProject/curriculum/issues/XXXXX

If this PR does not close, but is related to another issue or PR, you can link it as above without the 'Closes' keyword, e.g. 'Related to #2013'.
-->
Closes #XXXXX

## Additional Information
<!-- Any other information about this PR, such as a link to a Discord discussion. -->


## Pull Request Requirements
<!-- Replace the whitespace between the square brackets with an 'x', e.g. [x]. After you create the PR, they will become checkboxes that you can click on. -->
-   [x] I have thoroughly read and understand [The Odin Project Contributing Guide](https://github.com/TheOdinProject/.github/blob/main/CONTRIBUTING.md)
-   [x] The title of this PR follows the `location of change: brief description of change` format, e.g. `Callbacks command: Update verbiage`
-   [x] The `Because` section summarizes the reason for this PR
-   [x] The `This PR` section has a bullet point list describing the changes in this PR
-   [x] If this PR addresses an open issue, it is linked in the `Issue` section
-   [x] If this PR adds new features or functionality, I have added new tests
-   [x] If applicable, I have ensured all tests related to any command files included in this PR pass, and/or all snapshots are up to date



Patch:
diff --git a/botCommands/searchtop-legacy.js b/botCommands/searchtop-legacy.js
new file mode 100644
index 0000000..77672ea
--- /dev/null
+++ b/botCommands/searchtop-legacy.js
@@ -0,0 +1,18 @@
+const { EmbedBuilder } = require('discord.js');
+const { registerBotCommand } = require('../botEngine');
+
+const command = {
+  regex: /\B!searchtop\s+.+/,
+  cb: ({ content }) => {
+    const prompt = content.match(/\B!searchtop\s+(.+)/)[1];
+    const searchUrl = `https://www.google.com/search?q=site:theodinproject.com+${prompt.replaceAll(' ', '+')}`;
+    const topEmbed = new EmbedBuilder()
+      .setColor('#cc9543')
+      .setTitle('Search TOP')
+      .setDescription(`Here are the [Google results for TOP lessons containing "${prompt}"](${searchUrl})`);
+    return { embeds: [topEmbed]};
+  }
+};
+registerBotCommand(command.regex, command.cb);
+
+module.exports = command;


Test patch:
diff --git a/botCommands/__snapshots__/searchtop-legacy.test.js.snap b/botCommands/__snapshots__/searchtop-legacy.test.js.snap
new file mode 100644
index 0000000..cb898e9
--- /dev/null
+++ b/botCommands/__snapshots__/searchtop-legacy.test.js.snap
@@ -0,0 +1,13 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`!searchtop callback returns correct output 1`] = `
+{
+  "embeds": [
+    {
+      "color": 13407555,
+      "description": "Here are the [Google results for TOP lessons containing "The Odin Project"](https://www.google.com/search?q=site:theodinproject.com+The+Odin+Project)",
+      "title": "Search TOP",
+    },
+  ],
+}
+`;
diff --git a/botCommands/searchtop-legacy.test.js b/botCommands/searchtop-legacy.test.js
new file mode 100644
index 0000000..609cd98
--- /dev/null
+++ b/botCommands/searchtop-legacy.test.js
@@ -0,0 +1,65 @@
+const command = require('./searchtop-legacy');
+
+describe('!searchtop', () => {
+  describe('regex', () => {
+    it.each([
+      ['!searchtop query'],
+      [' !searchtop query'],
+      ['!searchtop query @odin-bot'],
+      ['@odin-bot !searchtop query'],
+    ])('correct strings trigger the callback', (string) => {
+      expect(command.regex.test(string)).toBeTruthy();
+    });
+
+    it.each([
+      ['!searchto'],
+      ['searchtop'],
+      ['!searcht'],
+      ['!search top'],
+      ['```function("!searchtop", () => {}```'],
+      ['!searchingtop'],
+      [''],
+      [' '],
+      [' !'],
+      ['@odin-bot ! searchtop'],
+      ['!search-top'],
+      ['!searc^htop'],
+      ['!searchtop!'],
+      ['@odin-bot! searchtop'],
+      ['https:!!searchtop.com'],
+    ])("'%s' does not trigger the callback", (string) => {
+      expect(command.regex.test(string)).toBeFalsy();
+    });
+
+    it.each([
+      ['Check this out! !searchtop query'],
+      ["Don't worry about !searchtop query"],
+      ['Hey @odin-bot, !searchtop query'],
+      ['!@odin-bot ^ !me !searchtop !tests$*'],
+    ])("'%s' - command can be anywhere in the string", (string) => {
+      expect(command.regex.test(string)).toBeTruthy();
+    });
+
+    it.each([
+      ['@user!searchtop'],
+      ["it's about!searchtop"],
+      ['!searchtopnow'],
+      ['!searchtop!'],
+      ['!searchtop*'],
+      ['!searchtop...'],
+    ])(
+      "'%s' - command should be its own word!group - no leading or trailing characters",
+      (string) => {
+        expect(command.regex.test(string)).toBeFalsy();
+      },
+    );
+  });
+
+  describe('callback', () => {
+    it('returns correct output', () => {
+      expect(
+        command.cb({ content: '!searchtop The Odin Project' }),
+      ).toMatchSnapshot();
+    });
+  });
+});