1 |
const MessageEmbed = require("../src/MessageEmbed"); |
2 |
const axios = require('axios').default; |
3 |
const path = require('path'); |
4 |
const { download, delete: deleteFile } = require("./cat"); |
5 |
|
6 |
/* |
7 |
axios.get("https://dog.ceo/api/breeds/image/random") |
8 |
.then(res => { |
9 |
if (res && res.status === 200 && res.data.status === 'success') { |
10 |
msg.reply({ |
11 |
content: res.data.message |
12 |
}); |
13 |
} |
14 |
}) |
15 |
.catch(err => { |
16 |
msg.reply({ |
17 |
embeds: [ |
18 |
new MessageEmbed() |
19 |
.setColor('#f14a60') |
20 |
.setDescription('Too many requests at the same time, please try again after some time.') |
21 |
] |
22 |
}); |
23 |
}); |
24 |
*/ |
25 |
|
26 |
module.exports = { |
27 |
async handle(msg, cm) { |
28 |
axios.get("https://api.thedogapi.com/v1/images/search", { |
29 |
headers: { |
30 |
"x-api-key": process.env.DOG_API_TOKEN |
31 |
} |
32 |
}) |
33 |
.then(res => { |
34 |
if (res && res.status === 200) { |
35 |
let name = res.data[0].url; |
36 |
const pos = name.indexOf('?'); |
37 |
|
38 |
if (pos !== -1) { |
39 |
name = name.substring(0, pos); |
40 |
} |
41 |
|
42 |
name = name.split(/\/+/); |
43 |
name = name[name.length - 1]; |
44 |
|
45 |
console.log(name); |
46 |
const filename = path.join(__dirname, '..', 'tmp', name); |
47 |
|
48 |
if (filename.endsWith('.false')) { |
49 |
filename = filename.replace(/\.false$/, '.png'); |
50 |
} |
51 |
|
52 |
download(res.data[0].url, filename) |
53 |
.then(() => { |
54 |
msg.reply({ |
55 |
files: [ |
56 |
{ |
57 |
attachment: filename, |
58 |
name |
59 |
} |
60 |
] |
61 |
}); |
62 |
|
63 |
deleteFile(filename); |
64 |
}) |
65 |
.catch(err => { |
66 |
console.log("DL error: " + err.message); |
67 |
|
68 |
deleteFile(filename); |
69 |
|
70 |
msg.reply({ |
71 |
embeds: [ |
72 |
new MessageEmbed() |
73 |
.setColor('#f14a60') |
74 |
.setDescription('Internal API error occured (download-time error), please try again.') |
75 |
] |
76 |
}); |
77 |
}); |
78 |
} |
79 |
else if (res?.status === 429) { |
80 |
msg.reply({ |
81 |
embeds: [ |
82 |
new MessageEmbed() |
83 |
.setColor('#f14a60') |
84 |
.setDescription('Too many requests at the same time, please try again after some time.') |
85 |
] |
86 |
}); |
87 |
} |
88 |
else { |
89 |
msg.reply({ |
90 |
embeds: [ |
91 |
new MessageEmbed() |
92 |
.setColor('#f14a60') |
93 |
.setDescription('Internal API error occured (pre-download time error), please try again.') |
94 |
] |
95 |
}); |
96 |
} |
97 |
}) |
98 |
.catch(err => { |
99 |
console.log(err); |
100 |
|
101 |
msg.reply({ |
102 |
embeds: [ |
103 |
new MessageEmbed() |
104 |
.setColor('#f14a60') |
105 |
.setDescription('Too many requests at the same time, please try again after some time.') |
106 |
] |
107 |
}); |
108 |
}); |
109 |
} |
110 |
}; |