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

Annotation of /trunk/commands/pixabay.js

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26