1 |
rakinar2 |
577 |
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 |
|
|
coolDown = 4000; |
74 |
|
|
|
75 |
|
|
constructor() { |
76 |
|
|
super('pixabay', 'fun', []); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
80 |
|
|
if (!options.isInteraction && options.args[0] === undefined) { |
81 |
|
|
await msg.reply({ |
82 |
|
|
embeds: [ |
83 |
|
|
new MessageEmbed() |
84 |
|
|
.setColor('#f14a60') |
85 |
|
|
.setDescription('This command requires at least 1 argument.') |
86 |
|
|
] |
87 |
|
|
}); |
88 |
|
|
|
89 |
|
|
return; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
if (msg instanceof CommandInteraction) |
93 |
|
|
await msg.deferReply(); |
94 |
|
|
|
95 |
|
|
const subcmd = options.isInteraction ? options.options.getSubcommand(true) : options.args[0]; |
96 |
|
|
|
97 |
|
|
if (!options.isInteraction) |
98 |
|
|
await options.args.shift(); |
99 |
|
|
|
100 |
|
|
if (subcmd === 'photo') { |
101 |
|
|
await photo(this, msg, options); |
102 |
|
|
} |
103 |
|
|
else if (subcmd === 'vector') { |
104 |
|
|
await vector(this, msg, options); |
105 |
|
|
} |
106 |
|
|
else if (subcmd === 'illustration') { |
107 |
|
|
await illustration(this, msg, options); |
108 |
|
|
} |
109 |
|
|
else if (subcmd === 'image') { |
110 |
|
|
await image(this, msg, options, 'all'); |
111 |
|
|
} |
112 |
|
|
else { |
113 |
|
|
await this.deferReply(msg, { |
114 |
|
|
embeds: [ |
115 |
|
|
new MessageEmbed() |
116 |
|
|
.setColor('#f14a60') |
117 |
|
|
.setDescription('Invalid subcommand provided.') |
118 |
|
|
] |
119 |
|
|
}); |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
} |