-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsearch.js
More file actions
52 lines (47 loc) · 1.81 KB
/
Copy pathsearch.js
File metadata and controls
52 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed, MessageActionRow, MessageButton } = require('discord.js');
const imageSearch = require('../image_search');
const resultMap = require('../resultMap');
require('dotenv').config();
module.exports = {
data: new SlashCommandBuilder()
.setName('search')
.setDescription('Search for an image')
.addStringOption(option =>
option.setName('input')
.setDescription('The search term')
.setRequired(true)),
async runSearch(input) {
const query = interaction.options.getString('input');
},
async execute(interaction) {
const query = interaction.options.getString('input');
const searchResult = await imageSearch.search(query);
const url = searchResult.currentSearch().link;
// Format the search term to replace spaces with %20 for custom search engine URL
const cseURL = query.replaceAll(' ', '%20');
const resultEmbed = new MessageEmbed()
.setTitle(`Images of ${query}`)
.setURL(`https://cse.google.com/cse?cx=${process.env.GG_CX}#gsc.q=${cseURL}`)
.setDescription(`Result ${searchResult.currentResult + 1} of ${searchResult.resultArray.length}`)
.addField(searchResult.currentSearch().title, searchResult.currentSearch().displayLink)
.setImage(url);
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('prev')
.setLabel('Previous')
.setStyle('PRIMARY'),
new MessageButton()
.setCustomId('next')
.setLabel('Next')
.setStyle('PRIMARY'),
new MessageButton()
.setLabel('View Original')
.setStyle('LINK')
.setURL(searchResult.currentSearch().image.contextLink),
);
const response = await interaction.reply({ embeds: [resultEmbed], components: [row], fetchReply: true });
await resultMap.set(response.id, searchResult);
},
};