1 |
import { CommandInteraction, Message, MessageEmbed } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import axios from 'axios'; |
7 |
import path from 'path'; |
8 |
import { deleteFile, download } from '../../utils/util'; |
9 |
|
10 |
export default class CatCommand extends BaseCommand { |
11 |
supportsInteractions: boolean = true; |
12 |
|
13 |
constructor() { |
14 |
super('cat', 'fun', []); |
15 |
} |
16 |
|
17 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
18 |
if (msg instanceof CommandInteraction) |
19 |
await msg.deferReply(); |
20 |
|
21 |
axios.get("https://api.thecatapi.com/v1/images/search", { |
22 |
headers: { |
23 |
"x-api-key": process.env.CAT_API_TOKEN! |
24 |
} |
25 |
}) |
26 |
.then(res => { |
27 |
if (res && res.status === 200) { |
28 |
let name = res.data[0].url; |
29 |
const pos = name.indexOf('?'); |
30 |
|
31 |
if (pos !== -1) { |
32 |
name = name.substring(0, pos); |
33 |
} |
34 |
|
35 |
name = name.split(/\/+/); |
36 |
name = name[name.length - 1]; |
37 |
|
38 |
console.log(name); |
39 |
let filename = path.join(__dirname, '../../..', 'tmp', name); |
40 |
|
41 |
if (filename.endsWith('.false')) { |
42 |
filename = filename.replace(/\.false$/, '.png'); |
43 |
} |
44 |
|
45 |
download(res.data[0].url, filename) |
46 |
.then(async () => { |
47 |
if (msg instanceof CommandInteraction) { |
48 |
await msg.editReply({ |
49 |
files: [ |
50 |
{ |
51 |
attachment: filename, |
52 |
name |
53 |
} |
54 |
] |
55 |
}); |
56 |
} |
57 |
else { |
58 |
await msg.reply({ |
59 |
files: [ |
60 |
{ |
61 |
attachment: filename, |
62 |
name |
63 |
} |
64 |
] |
65 |
}); |
66 |
} |
67 |
|
68 |
await deleteFile(filename); |
69 |
}) |
70 |
.catch(err => { |
71 |
console.log("DL error: " + err.message); |
72 |
|
73 |
deleteFile(filename); |
74 |
|
75 |
if (msg instanceof CommandInteraction) |
76 |
msg.editReply({ |
77 |
embeds: [ |
78 |
new MessageEmbed() |
79 |
.setColor('#f14a60') |
80 |
.setDescription('Internal API error occured (download-time error), please try again.') |
81 |
] |
82 |
}); |
83 |
else |
84 |
msg.reply({ |
85 |
embeds: [ |
86 |
new MessageEmbed() |
87 |
.setColor('#f14a60') |
88 |
.setDescription('Internal API error occured (download-time error), please try again.') |
89 |
] |
90 |
}); |
91 |
}); |
92 |
} |
93 |
else if (res?.status === 429) { |
94 |
if (msg instanceof CommandInteraction) |
95 |
msg.editReply({ |
96 |
embeds: [ |
97 |
new MessageEmbed() |
98 |
.setColor('#f14a60') |
99 |
.setDescription('Too many requests at the same time, please try again after some time.') |
100 |
] |
101 |
}); |
102 |
else |
103 |
msg.reply({ |
104 |
embeds: [ |
105 |
new MessageEmbed() |
106 |
.setColor('#f14a60') |
107 |
.setDescription('Too many requests at the same time, please try again after some time.') |
108 |
] |
109 |
}); |
110 |
} |
111 |
else { |
112 |
if (msg instanceof CommandInteraction) |
113 |
msg.editReply({ |
114 |
embeds: [ |
115 |
new MessageEmbed() |
116 |
.setColor('#f14a60') |
117 |
.setDescription('Internal API error occured (pre-download time error), please try again.') |
118 |
] |
119 |
}); |
120 |
else |
121 |
msg.reply({ |
122 |
embeds: [ |
123 |
new MessageEmbed() |
124 |
.setColor('#f14a60') |
125 |
.setDescription('Internal API error occured (pre-download time error), please try again.') |
126 |
] |
127 |
}); |
128 |
} |
129 |
}) |
130 |
.catch(err => { |
131 |
if (msg instanceof CommandInteraction) |
132 |
msg.editReply({ |
133 |
embeds: [ |
134 |
new MessageEmbed() |
135 |
.setColor('#f14a60') |
136 |
.setDescription('Too many requests at the same time, please try again after some time.') |
137 |
] |
138 |
}); |
139 |
else |
140 |
msg.reply({ |
141 |
embeds: [ |
142 |
new MessageEmbed() |
143 |
.setColor('#f14a60') |
144 |
.setDescription('Too many requests at the same time, please try again after some time.') |
145 |
] |
146 |
}); |
147 |
}); |
148 |
} |
149 |
} |