JoshKoiro / Nautical-Net

A Discord bot that provides images from Unsplash and Pexels to copy as links within Discord
GNU General Public License v3.0
0 stars 0 forks source link

Add ability to define qty of results using -- syntax #8

Closed JoshKoiro closed 12 months ago

JoshKoiro commented 12 months ago

You should be able to enter --10 or --8 as a parameter in the discord command to allow to select the number of items to return in the message result

JoshKoiro commented 12 months ago

What needs to happen is a function that is called before the try block to run the query to the API in the search command:

if (commandName === 'search-pexels') {
      const query = interaction.options.getString('query');
      // CODE TO RUN HERE --> let flags = getFlags()
      try {

This GetFlags function needs to not only be able to separate out the relevant flags into an array that can be iterated over, but also be able to remove all flags out of a query. Another way of looking at this could be that there should be a getFlags() function as well as a getQuery function. contained within a queryParse.js file

JoshKoiro commented 12 months ago

perhaps a better idea is to have the parseQuery() function return an object with this format:

const query = {
text: 
flags: []
}

to get the text you can run this code against every flag that is pulled out of the discord query value:

let queryStr = queryStr.replace("--flag", "");

then pass every subsequent queryStr to the next one.

JoshKoiro commented 12 months ago

To get the query text value, we need to search the text removing every non-space word after the string "--"

I want this to be done for any number of flags entered in the query. This can be done using this code:

let str = "/search cozy home --unsplash --8 --test --15 --example --42 --lastword";

let regex = /--(\w+)(?:--(\d+))?/g;

let matches = [];

for (let match of str.matchAll(regex)) {
    matches.push(match[1]);
    if (match[2]) {
        matches.push(match[2]);
    }
}

A flag must be entered without a space between the double hyphans (--) and the word or number