-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathweaponSearch.js
More file actions
46 lines (43 loc) · 1.28 KB
/
weaponSearch.js
File metadata and controls
46 lines (43 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* This file defines functionality to search through known weapons and return matching results
* @module weaponSearch
*/
const Discord = require('discord.js');
const weaponsJSON = require('./static/weapons.json');
const {badQuery} = require('./utils.js');
module.exports = {
/**
* Look up a weapon by name and return a list of matching results
* @param {string} name - The name of the weapon to search for
* @returns a discord embed of matching results
* @throws if the query is not a valid weapon or contains invalid characters
*/
lookup: async function(name){
if(badQuery(name)){
throw "Weapon search contains disallowed characters";
}
name = name.toLowerCase();
let curLength = 0;
let found = [];
for(const id in weaponsJSON){
if(weaponsJSON[id].name.toLowerCase().includes(name)){
let info = `${weaponsJSON[id].name} [${id}]`;
if((curLength+info.length) > 1020){
found.push("...");
break;
}
else{
found.push(info);
curLength += info.length+1;
}
}
}
if(found.length == 0){
throw "No weapons found matching that query";
}
let resEmbed = new Discord.EmbedBuilder();
resEmbed.setTitle("Weapon search results");
resEmbed.addFields({name: '\u200b', value: `${found}`.replace(/,/g, '\n')});
return resEmbed;
}
}