1 |
const MessageEmbed = require("../src/MessageEmbed"); |
2 |
const axios = require('axios').default; |
3 |
|
4 |
function url() { |
5 |
return `https://pixabay.com/api/?key=${process.env.PIXABAY_TOKEN}&safesearch=true&per_page=3`; |
6 |
} |
7 |
|
8 |
function random(arr) { |
9 |
let index = Math.floor(Math.random() * arr.length); |
10 |
return arr[index]; |
11 |
} |
12 |
|
13 |
async function image(msg, cm, type) { |
14 |
let genurl = `${url()}&image_type=${type}`; |
15 |
|
16 |
if (cm.args[1] !== undefined) { |
17 |
let args = [...cm.args]; |
18 |
args.shift(); |
19 |
genurl += `&q=${args.join(' ')}`; |
20 |
} |
21 |
|
22 |
console.log(genurl); |
23 |
|
24 |
axios.get(genurl) |
25 |
.then(res => { |
26 |
if (res && res.status === 200) { |
27 |
//console.log(res.data); |
28 |
msg.reply({ |
29 |
content: random(res.data.hits).largeImageURL |
30 |
}); |
31 |
} |
32 |
}) |
33 |
.catch(err => { |
34 |
msg.reply({ |
35 |
embeds: [ |
36 |
new MessageEmbed() |
37 |
.setColor('#f14a60') |
38 |
.setDescription('Too many requests at the same time, please try again after some time.') |
39 |
] |
40 |
}); |
41 |
}); |
42 |
} |
43 |
|
44 |
async function photo(msg, cm) { |
45 |
await image(msg, cm, 'photo'); |
46 |
} |
47 |
|
48 |
async function vector(msg, cm) { |
49 |
await image(msg, cm, 'vector'); |
50 |
} |
51 |
|
52 |
async function illustration(msg, cm) { |
53 |
await image(msg, cm, 'illustration'); |
54 |
} |
55 |
|
56 |
module.exports = { |
57 |
async handle(msg, cm) { |
58 |
if (cm.args[0] === undefined) { |
59 |
await msg.reply({ |
60 |
embeds: [ |
61 |
new MessageEmbed() |
62 |
.setColor('#f14a60') |
63 |
.setDescription('No subcommand provided.') |
64 |
] |
65 |
}); |
66 |
|
67 |
return; |
68 |
} |
69 |
|
70 |
if (cm.args[0] === 'photo') { |
71 |
await photo(msg, cm); |
72 |
} |
73 |
else if (cm.args[0] === 'vector') { |
74 |
await vector(msg, cm); |
75 |
} |
76 |
else if (cm.args[0] === 'illustration') { |
77 |
await illustration(msg, cm); |
78 |
} |
79 |
else if (cm.args[0] === 'image') { |
80 |
await image(msg, cm, 'all'); |
81 |
} |
82 |
else { |
83 |
await msg.reply({ |
84 |
embeds: [ |
85 |
new MessageEmbed() |
86 |
.setColor('#f14a60') |
87 |
.setDescription('Invalid subcommand provided.') |
88 |
] |
89 |
}); |
90 |
} |
91 |
} |
92 |
}; |