/[sudobot]/trunk/src/commands/fun/PixabayCommand.ts
ViewVC logotype

Annotation of /trunk/src/commands/fun/PixabayCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 344 - (hide annotations)
Mon Jul 29 17:29:40 2024 UTC (8 months, 3 weeks ago) by rakin
File MIME type: application/typescript
File size: 4093 byte(s)
chore: eslint autofix
1 rakin 51 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 rakin 344 import { random } from '../../utils/util';
8 rakin 51
9    
10     function url() {
11     return `https://pixabay.com/api/?key=${process.env.PIXABAY_TOKEN}&safesearch=true&per_page=3`;
12     }
13    
14     export async function image(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions, type: 'photo' | 'all' | 'illustration' | 'vector') {
15     let genurl = `${url()}&image_type=${type}`;
16     let query = options.isInteraction ? options.options.getString('query') : null;
17    
18     if (!options.isInteraction) {
19     let args = [...options.args];
20     query = args.join(' ');
21     }
22    
23     if (query && query.trim() !== '') {
24     let q = new URLSearchParams({q: query}).toString();
25     console.log(q);
26     genurl += `&${q}`;
27     }
28    
29     axios.get(genurl)
30     .then(async res => {
31     if (res && res.status === 200) {
32     //console.log(res.data.hits);
33     if (!res.data.hits || res.data.hits?.length < 1) {
34     await cmd.deferReply(msg, {
35     content: ":x: No search result found from the API."
36     });
37    
38     return;
39     }
40    
41     await cmd.deferReply(msg, {
42     content: random(res.data.hits).largeImageURL
43     });
44     }
45     })
46     .catch(async err => {
47     console.log(err.message);
48     await cmd.deferReply(msg, {
49     embeds: [
50     new MessageEmbed()
51     .setColor('#f14a60')
52     .setDescription('Too many requests at the same time, please try again after some time.')
53     ]
54     });
55     });
56     }
57    
58     export async function photo(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
59     await image(cmd, msg, options, 'photo');
60     }
61    
62     export async function vector(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
63     await image(cmd, msg, options, 'vector');
64     }
65    
66     export async function illustration(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
67     await image(cmd, msg, options, 'illustration');
68     }
69    
70     export default class PixabayCommand extends BaseCommand {
71     supportsInteractions: boolean = true;
72 rakin 56 coolDown = 4000;
73 rakin 51
74     constructor() {
75     super('pixabay', 'fun', []);
76     }
77    
78     async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
79     if (!options.isInteraction && options.args[0] === undefined) {
80     await msg.reply({
81     embeds: [
82     new MessageEmbed()
83     .setColor('#f14a60')
84     .setDescription('This command requires at least 1 argument.')
85     ]
86     });
87    
88     return;
89     }
90    
91     if (msg instanceof CommandInteraction)
92     await msg.deferReply();
93    
94     const subcmd = options.isInteraction ? options.options.getSubcommand(true) : options.args[0];
95    
96     if (!options.isInteraction)
97     await options.args.shift();
98    
99     if (subcmd === 'photo') {
100     await photo(this, msg, options);
101     }
102     else if (subcmd === 'vector') {
103     await vector(this, msg, options);
104     }
105     else if (subcmd === 'illustration') {
106     await illustration(this, msg, options);
107     }
108     else if (subcmd === 'image') {
109     await image(this, msg, options, 'all');
110     }
111     else {
112     await this.deferReply(msg, {
113     embeds: [
114     new MessageEmbed()
115     .setColor('#f14a60')
116     .setDescription('Invalid subcommand provided.')
117     ]
118     });
119     }
120     }
121     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26