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

Diff of /trunk/commands/dog.js

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by rakin, Mon Jul 29 17:28:11 2024 UTC revision 10 by rakin, Mon Jul 29 17:28:12 2024 UTC
# Line 1  Line 1 
1  const MessageEmbed = require("../src/MessageEmbed");  const MessageEmbed = require("../src/MessageEmbed");
2  const axios = require('axios').default;  const axios = require('axios').default;
3    const path = require('path');
4    const { download, delete: deleteFile } = require("./cat");
5    
6  module.exports = {  /*
7      async handle(msg, cm) {  axios.get("https://dog.ceo/api/breeds/image/random")
         axios.get("https://dog.ceo/api/breeds/image/random")  
8          .then(res => {          .then(res => {
9              if (res && res.status === 200 && res.data.status === 'success') {              if (res && res.status === 200 && res.data.status === 'success') {
10                  msg.reply({                  msg.reply({
# Line 15  module.exports = { Line 16  module.exports = {
16              msg.reply({              msg.reply({
17                  embeds: [                  embeds: [
18                      new MessageEmbed()                      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')                      .setColor('#f14a60')
105                      .setDescription('Too many requests at the same time, please try again after some time.')                      .setDescription('Too many requests at the same time, please try again after some time.')
106                  ]                  ]

Legend:
Removed from v.5  
changed lines
  Added in v.10

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26