Reputation: 59
I'm working on a command that searches maps for the game osu! on the site bloodcat.com and I made a way to filter the maps with:
.split('status=ranked').join('&c=b&s=1&m=&g=&l=')
.split('status=approved').join('&c=b&s=2&m=&g=&l=')
.split('status=qualified').join('&c=b&s=3&m=&g=&l=')
.split('status=loved').join('&c=b&s=4&m=&g=&l=')
.split('status=unranked').join('&c=b&s=0&m=&g=&l=')
.split('status=graveyarded').join('&c=b&s=0&m=&g=&l=');
Now someone can do !search 'map name' status=unranked
and that would look for https://bloodcat.com/osu/?mod=json&q='map name'&c=b&s=0&m=&g=&l=
but if someone does for example !search status=unranked 'map name'
that doesn't work is there any way.
Can I make the status=unranked
at the end even if the user doesn't put it in the end in the command?
Upvotes: 1
Views: 77
Reputation: 6796
I would do it like this: first, check if the string of arguments starts with status=
. If so, you'll need to switch them, otherwise, it should work as it's doing now.
In order to switch those, I would split the string with spaces, remove the first argument (status=*
), rejoin that with spaces and add the removed part:
function getQueryURL(argStr = "") { // argStr should be like "'map name' status=unranked" OR "status=unranked 'map name'"
if (argStr.startsWith('status=')) {
let arr = argStr.split(' ');
let removed = arr.shift();
argStr = arr.join(' ').trim() + removed.trim();
}
let res = argStr.split('status=ranked').join('&c=b&s=1&m=&g=&l=')
.split('status=approved').join('&c=b&s=2&m=&g=&l=')
.split('status=qualified').join('&c=b&s=3&m=&g=&l=')
.split('status=loved').join('&c=b&s=4&m=&g=&l=')
.split('status=unranked').join('&c=b&s=0&m=&g=&l=')
.split('status=graveyarded').join('&c=b&s=0&m=&g=&l=');
return "https://bloodcat.com/osu/?mod=json&q=" + res;
}
Upvotes: 2