/[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 51 - (hide annotations)
Mon Jul 29 17:28:23 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 4119 byte(s)
Release version 2.0
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     import path from 'path';
8     import { deleteFile, download, random } from '../../utils/util';
9    
10    
11     function url() {
12     return `https://pixabay.com/api/?key=${process.env.PIXABAY_TOKEN}&safesearch=true&per_page=3`;
13     }
14    
15     export async function image(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions, type: 'photo' | 'all' | 'illustration' | 'vector') {
16     let genurl = `${url()}&image_type=${type}`;
17     let query = options.isInteraction ? options.options.getString('query') : null;
18    
19     if (!options.isInteraction) {
20     let args = [...options.args];
21     query = args.join(' ');
22     }
23    
24     if (query && query.trim() !== '') {
25     let q = new URLSearchParams({q: query}).toString();
26     console.log(q);
27     genurl += `&${q}`;
28     }
29    
30     axios.get(genurl)
31     .then(async res => {
32     if (res && res.status === 200) {
33     //console.log(res.data.hits);
34     if (!res.data.hits || res.data.hits?.length < 1) {
35     await cmd.deferReply(msg, {
36     content: ":x: No search result found from the API."
37     });
38    
39     return;
40     }
41    
42     await cmd.deferReply(msg, {
43     content: random(res.data.hits).largeImageURL
44     });
45     }
46     })
47     .catch(async err => {
48     console.log(err.message);
49     await cmd.deferReply(msg, {
50     embeds: [
51     new MessageEmbed()
52     .setColor('#f14a60')
53     .setDescription('Too many requests at the same time, please try again after some time.')
54     ]
55     });
56     });
57     }
58    
59     export async function photo(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
60     await image(cmd, msg, options, 'photo');
61     }
62    
63     export async function vector(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
64     await image(cmd, msg, options, 'vector');
65     }
66    
67     export async function illustration(cmd: BaseCommand, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
68     await image(cmd, msg, options, 'illustration');
69     }
70    
71     export default class PixabayCommand extends BaseCommand {
72     supportsInteractions: boolean = true;
73    
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