-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegacy-index.js
247 lines (201 loc) · 6.07 KB
/
legacy-index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require('dotenv').config();
const { Client, Embed } = require('guilded.ts');
const fetch = require('node-fetch');
const { resourceLimits } = require('worker_threads');
const Sentry = require('@sentry/node');
Sentry.init({
dsn: 'https://[email protected]/6455239',
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
setTimeout(() => {
try {
run();
} catch (e) {
Sentry.captureException(e);
}
}, 99);
const client = new Client();
const guilded_token = process.env.GUILDED_API_TOKEN;
const fleetyardsHost = 'https://api.fleetyards.net/v1';
const primaryColor = '#428bca';
const validTopics = ['ship', 'station', 'item', 'help'];
const validQuestions = {
help: [],
ship: [
'options',
'info',
'cargo',
'length',
'beam',
'height',
'crew',
'weapons',
],
station: ['options', 'info', 'docking', 'habs'],
item: ['options', 'info', 'price', 'where'],
};
const resolveShipQuestion = async (subject, question) => {
const shipName = subject.toLowerCase().trim().replace(' ', '-');
const ship = await fetchShip(shipName);
if (!ship) {
return { content: 'Could not load requested Information' };
}
if (question == 'info') {
const embed = new Embed()
.setTitle(ship.name)
.setURL(ship.links.frontend)
.setDescription(ship.description)
.setThumbnail(ship.storeImageMedium)
.addField('Length', ship.lengthLabel, true)
.addField('Beam', ship.beamLabel, true)
.addField('Height', ship.heightLabel, true)
.addField('Cargo', ship.cargoLabel, true)
.addField('Min. Crew', ship.minCrewLabel, true)
.addField('Max. Crew', ship.maxCrewLabel, true)
.setColor(primaryColor);
return { embeds: [embed] };
} else {
if (question === 'weapons') {
const embed = new Embed()
.setTitle(`${ship.name} Weapons`)
.setURL(ship.links.frontend)
.setColor(primaryColor);
return { embeds: [embed] };
} else if (question === 'crew') {
const embed = new Embed()
.setTitle(`${ship.name} Crew`)
.setURL(ship.links.frontend)
.addField('Min. Crew', ship.minCrewLabel, true)
.addField('Max. Crew', ship.maxCrewLabel, true)
.setColor(primaryColor);
return { embeds: [embed] };
} else {
return { content: ship[`${question}Label`] };
}
}
};
const fetchShip = async (searchTerm) => {
const response = await fetch(`${fleetyardsHost}/models/${searchTerm}`);
if (response.status === 200) {
return response.json();
} else {
console.error(response.statusText, searchTerm);
}
return null;
};
const resolveStationQuestion = (subject, question) => {
return { content: 'Not yet ready' };
};
const resolveItemQuestion = (subject, question) => {
return { content: 'Not yet ready' };
};
const helpContent = () => {
return {
content: 'Available Commands are:',
embeds: [
new Embed()
.addField('help', 'Display this help message')
.addField(
'ship [question] <subject>',
'Get Information about a specific Ship',
)
.addField(
'station [question] <subject>',
'Get Information about a specific Station',
)
.addField(
'item [question] <subject>',
'Get Information about a specific Item (Weapon, Armor etc.)',
)
.setColor(primaryColor),
],
};
};
const resolveByTopic = (topic, subject, question) => {
if (question == 'options') {
return {
content: `Valid questions for ${topic} are: ${validQuestions[topic].join(
', ',
)}`,
};
}
if (!subject || !subject.length) {
return { content: 'Please provide a valid Subject' };
}
switch (topic) {
case 'help':
return helpContent();
break;
case 'ship':
return resolveShipQuestion(subject, question);
break;
case 'station':
return resolveStationQuestion(subject, question);
break;
case 'item':
return resolveItemQuestion(subject, question);
break;
default:
return { content: 'Something wrong!' };
break;
}
};
const resolveQuestion = async (args) => {
const searchTerm = args.join(' ');
const results = await search(searchTerm);
console.log(results);
if (!results || !results.length) {
return { content: 'Could not find any results' };
}
item = results[0];
return { content: item.name };
};
const search = async (searchTerm) => {
const searchUrl = `${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;
};
// This event is emitted when your bot is connected to Guilded's Gateway API.
client.once('ready', () =>
console.info(`[READY] Logged in as ${client.user.name}.`),
);
// This event is emitted when a message is sent on Guilded.
client.on('messageCreate', async (message) => {
if (message.createdBy === client.user.id) return;
const [botName, ...args] = message.content.split(' ');
if (botName !== '@F3-Y4') return;
console.info('[MESSAGE]', message.content);
console.log(args);
const result = await resolveQuestion(args);
message.reply(result);
// const questionArgs = message.content
// .replace(/@Yards Bot/g, '')
// .trim()
// .split(' ');
// let topic = questionArgs.shift();
// if (!validTopics.includes(topic)) {
// questionArgs.unshift(topic);
// topic = 'ship';
// }
// let question = questionArgs.shift();
// let subject = questionArgs.join(' ');
// if (!validQuestions[topic].includes(question)) {
// if (question !== undefined) {
// subject = `${question} ${subject}`;
// }
// question = 'info';
// }
// const result = await resolveByTopic(topic, subject, question);
// message.reply(result);
});
// Log into guilded
client.login(guilded_token);