used await instead of .then().catch() (KhaaZ)

merge-requests/7/head
DedShot™#9195 2020-05-09 22:33:32 -04:00
parent b9f789e628
commit cd86c3f7ee
1 changed files with 39 additions and 39 deletions

View File

@ -2,7 +2,7 @@ import { Message } from 'eris';
import axios from 'axios';
import { Client, Command, RichEmbed } from '../class';
export default class Mdn extends Command {
export default class MDN extends Command {
constructor(client: Client) {
super(client);
this.name = 'mdn';
@ -20,46 +20,46 @@ export default class Mdn extends Command {
args[0] = args[0].replace('.', '.prototype.');
args[0] = args[0].replace('#', '.prototype.');
return axios.get(`https://mdn.pleb.xyz/search?q=${args[0]}`)
.then((res) => {
const { data } = res;
if (!data.Title || !data.Summary || !data.URL) return this.error(message.channel, 'Could not find information. Try something else.');
let res;
try {
res = await axios.get(`https://mdn.pleb.xyz/search?q=${args[0]}`);
} catch (err) {
this.error(message.channel, 'Please try again later, something unexpected happened.');
return this.client.util.handleError(err, message, this);
}
const { data } = res;
if (!data || !data.Title || !data.Summary || !data.URL) return this.error(message.channel, 'Could not find information. Try something else.');
// Do not touch to the next block of code. Don't even look at it. your brain could stop working
data.Summary = data.Summary.replace(/<code><strong>/g, '<strong><code>');
data.Summary = data.Summary.replace(/<\/strong><\/code>/g, '</code></strong>');
data.Summary = data.Summary.replace(/<code>/g, '`');
data.Summary = data.Summary.replace(/<\/code>/g, '`');
data.Summary = data.Summary.replace(/<strong>/g, '**');
data.Summary = data.Summary.replace(/<\/strong>/g, '**');
const url = data.Summary.match(/<a href=.*>/);
if (url !== null) {
url[0] = url[0].replace('<a href="', '');
url[0] = url[0].replace('</a>', '');
const name = url[0].match(/>.*/);
name[0] = name[0].replace('>', '');
url[0] = url[0].replace(/>.*/, '');
url[0] = url[0].replace('"', '');
const title = url[0].match(/title=".*"/);
if (title !== null) url[0] = url[0].replace(title[0], '');
data.Summary = data.Summary.replace(/<a href=.*>/, `[${name[0]}](https://developer.mozilla.org${url[0]})`);
}
// Do not touch to the next block of code. Don't even look at it. your brain could stop working
data.Summary = data.Summary.replace(/<code><strong>/g, '<strong><code>');
data.Summary = data.Summary.replace(/<\/strong><\/code>/g, '</code></strong>');
data.Summary = data.Summary.replace(/<code>/g, '`');
data.Summary = data.Summary.replace(/<\/code>/g, '`');
data.Summary = data.Summary.replace(/<strong>/g, '**');
data.Summary = data.Summary.replace(/<\/strong>/g, '**');
const url = data.Summary.match(/<a href=.*>/);
if (url !== null) {
url[0] = url[0].replace('<a href="', '');
url[0] = url[0].replace('</a>', '');
const name = url[0].match(/>.*/);
name[0] = name[0].replace('>', '');
url[0] = url[0].replace(/>.*/, '');
url[0] = url[0].replace('"', '');
const title = url[0].match(/title=".*"/);
if (title !== null) url[0] = url[0].replace(title[0], '');
data.Summary = data.Summary.replace(/<a href=.*>/, `[${name[0]}](https://developer.mozilla.org${url[0]})`);
}
const embed = new RichEmbed();
embed.setAuthor('Mozilla Developer Network', 'https://i.imgur.com/DFGXabG.png', 'https://developer.mozilla.org/en-US');
embed.setTitle(data.Title);
embed.setDescription(data.Summary);
embed.setURL(`developer.mozilla.org${data.URL}`);
embed.setColor(0x066fad);
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
return message.channel.createMessage({ embed });
})
.catch((err) => {
this.error(message.channel, 'Please try again later, something unexpected happened.');
this.client.util.handleError(err, message, this);
});
const embed = new RichEmbed();
embed.setAuthor('Mozilla Developer Network', 'https://i.imgur.com/DFGXabG.png', 'https://developer.mozilla.org/en-US');
embed.setTitle(data.Title);
embed.setDescription(data.Summary);
embed.setURL(`developer.mozilla.org${data.URL}`);
embed.setColor(0x066fad);
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
embed.setTimestamp();
return message.channel.createMessage({ embed });
} catch (err) {
return this.client.util.handleError(err, message, this);
}