/[sudobot]/branches/2.x/src/commands/fun/CatCommand.ts
ViewVC logotype

Contents of /branches/2.x/src/commands/fun/CatCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26