-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfleetyards-bot.js
60 lines (45 loc) · 1.51 KB
/
fleetyards-bot.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const fetch = require('node-fetch');
module.exports = class FleetyardsBot {
constructor(embedComponent) {
this.embedComponent = embedComponent;
}
fleetyardsHost = 'https://api.fleetyards.net/v1';
primaryColor = '#428bca';
async resolve(args) {
const searchTerm = args.join(' ');
const results = await this.search(searchTerm);
if (!results || !results.length) {
return { content: 'Could not find any results' };
}
const item = results[0];
const embed = this.embedComponent
.setTitle(item.name)
.setDescription(item.description)
.setThumbnail(item.storeImageMedium)
.setColor(this.primaryColor);
if (item.links && item.links.frontend) {
embed.setURL(item.links.frontend);
}
if (item.resultType == 'model') {
embed
.addField('Length', item.lengthLabel, true)
.addField('Beam', item.beamLabel, true)
.addField('Height', item.heightLabel, true)
.addField('Cargo', item.cargoLabel, true)
.addField('Min. Crew', item.minCrewLabel, true)
.addField('Max. Crew', item.maxCrewLabel, true);
}
return { embeds: [embed] };
}
async search(searchTerm) {
const searchUrl = `${this.fleetyardsHost}/search/?q[search]=${searchTerm}`;
// console.info('Making request to', searchUrl);
const response = await fetch(searchUrl);
if (response.status === 200) {
return response.json();
} else {
console.error(response.statusText, searchTerm);
}
return null;
}
};