/[sudobot]/trunk/commands/pixabay.js
ViewVC logotype

Annotation of /trunk/commands/pixabay.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations)
Mon Jul 29 17:28:12 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: text/javascript
File size: 2286 byte(s)
Added new features
1 rakin 5 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 rakin 10 genurl += `&q=${escape(args.join(' '))}`;
20 rakin 5 }
21    
22     axios.get(genurl)
23     .then(res => {
24     if (res && res.status === 200) {
25     //console.log(res.data);
26     msg.reply({
27     content: random(res.data.hits).largeImageURL
28     });
29     }
30     })
31     .catch(err => {
32     msg.reply({
33     embeds: [
34     new MessageEmbed()
35     .setColor('#f14a60')
36     .setDescription('Too many requests at the same time, please try again after some time.')
37     ]
38     });
39     });
40     }
41    
42     async function photo(msg, cm) {
43     await image(msg, cm, 'photo');
44     }
45    
46     async function vector(msg, cm) {
47     await image(msg, cm, 'vector');
48     }
49    
50     async function illustration(msg, cm) {
51     await image(msg, cm, 'illustration');
52     }
53    
54     module.exports = {
55     async handle(msg, cm) {
56     if (cm.args[0] === undefined) {
57     await msg.reply({
58     embeds: [
59     new MessageEmbed()
60     .setColor('#f14a60')
61     .setDescription('No subcommand provided.')
62     ]
63     });
64    
65     return;
66     }
67    
68     if (cm.args[0] === 'photo') {
69     await photo(msg, cm);
70     }
71     else if (cm.args[0] === 'vector') {
72     await vector(msg, cm);
73     }
74     else if (cm.args[0] === 'illustration') {
75     await illustration(msg, cm);
76     }
77     else if (cm.args[0] === 'image') {
78     await image(msg, cm, 'all');
79     }
80     else {
81     await msg.reply({
82     embeds: [
83     new MessageEmbed()
84     .setColor('#f14a60')
85     .setDescription('Invalid subcommand provided.')
86     ]
87     });
88     }
89     }
90     };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26