Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Google API System #6

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Bot_Token=
Bot_Prefix=
Bot_embedColor=
Bot_ownerID=
TOKEN=
PREFIX=
COLOR=
GOOGLE_KEY=
ENGINE_ID=
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "test",
"main": "infotrons.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"api": "node ./google_api.js"
},
"repository": {
"type": "git",
Expand All @@ -21,6 +22,8 @@
"homepage": "https://github.com/devstrons/infotrons#readme",
"dependencies": {
"discord.js": "^13.8.1",
"dotenv": "^16.0.1"
"dotenv": "^16.0.1",
"google-search-results-nodejs": "^2.1.0",
"node-superfetch": "^0.3.3"
}
}
7 changes: 3 additions & 4 deletions src/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ class Infotrons extends Client {
this.commands = new Collection();
this.slashCommands = new Collection();
this.aliases = new Collection();
this.owner = this.config.Bot_owenerID;
this.prefix = this.config.prefix;
this.emoji = require('./utility/emojis.json');
this.color = this.config.Bot_embedColor;
this.color = this.config.color;
}

// –– Events Handler ––––––––––––––––––––––––––––––––––––––––——–––––––––––––
Expand Down Expand Up @@ -99,10 +98,10 @@ class Infotrons extends Client {

login() {

if (!this.config.Bot_Token)
if (!this.config.token)
throw new Error("You must pass the token for the client...");

super.login(this.config.Bot_Token);
super.login(this.config.token);

}

Expand Down
2 changes: 1 addition & 1 deletion src/SlashCommands/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
*/
run: async(client, interaction, args) => {
const embed = new MessageEmbed()
.setColor(client.embedColor)
.setColor(client.color)
.setDescription(`${client.emojis.ping_pong} Pong!\n${client.ws.ping}ms`)

interaction.reply({embeds: [embed]})
Expand Down
57 changes: 57 additions & 0 deletions src/SlashCommands/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { MessageEmbed, Client, CommandInteraction, Message } = require('discord.js');

module.exports = {
name: 'info',
description: 'search the query!',
options: [
{
name: "query",
type: "STRING",
description: "What you want to search",
required: true,
},
],
/**
* @param {Client} client
* @param {CommandInteraction} interaction
* @param {String[]} args
*/
run: async(client, interaction, args) => {

await interaction.defer();

let key = client.config.google_KEY;
let csx = client.config.engine_id
let query = interaction.options.get("query");

if (!query)
return interaction.reply(`Provide a query to google !! \`${client.config.prefix}google Apple\``);

async function search(query) {
const { body } = await request.get("https://www.googleapis.com/customsearch/v1").query({
key: key, cx: csx, safe: "off", q: query
});

if (!body.items) return null;
return body.items[0];

}

let href = await search(query);
if (!href)
return interaction.reply(`Couldn't search **${query}**`)

const embed = new MessageEmbed()
.setTitle(href.title)
.setDescription(href.snippet)
.setImage(href.pagemap ? href.pagemap.cse_thumbnail[0].src : null)
.setURL(href.link)
.setColor(client.color)
.setFooter({text:`Requested by ${interaction.author.username}`, iconURL: interaction.author.displayAvatarURL({ dynamic: true })})
.setTimestamp()


await interaction.editReply({ embeds: [embed] });

},
};
54 changes: 54 additions & 0 deletions src/commands/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { MessageEmbed } = require("discord.js")
const request = require('node-superfetch');

module.exports = {
name: "search",
description: "Sends the bot ping",
category: "",
cooldown: 3,
aliases: [],
usage: [`ping `],
example: [`ping `],
botPerms: [],
Permissions: [],
owner: false,
execute: async (message, args, client) => {

let key = client.config.google_KEY;
let csx = client.config.engine_id
let query = args.join(" ");

if (!query)
return message.reply(`Provide a query to google !! \`${client.config.prefix}google Apple\``);

async function search(query) {
const { body } = await request.get("https://www.googleapis.com/customsearch/v1").query({
key: key, cx: csx, safe: "off", q: query
});

if (!body.items) return null;
return body.items[0];

}

let href = await search(query);
if (!href)
return message.reply(`Couldn't search **${query}**`)

const embed = new MessageEmbed()
.setTitle(href.title)
.setDescription(href.snippet)
.setImage(href.pagemap ? href.pagemap.cse_thumbnail[0].src : null)
.setURL(href.link)
.setColor(client.color)
.setFooter({text:`Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL({ dynamic: true })})
.setTimestamp()


return message.channel.send({embeds: [embed]})

}


}

9 changes: 5 additions & 4 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require("dotenv").config();

module.exports = {
Bot_Token: process.env.Bot_Token || "",
Bot_Prefix: process.env.Bot_Prefix || "!",
Bot_embedColor: process.env.Bot_embedColor || "RED",
Bot_owenerID: process.env.Bot_owenerID || ""
token: process.env.TOKEN || "",
prefix: process.env.PREFIX || "!",
color: process.env.COLOR || "RED",
google_KEY: process.env.GOOGLE_KEY || "",
engine_id: process.env.ENGINE_ID || ""
}
2 changes: 1 addition & 1 deletion src/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
let prefix = client.prefix;
const channel = message?.channel;

let datab = client.config.Bot_owenerID
let datab = ['801478547893387345']


const mentionRegexPrefix = RegExp(`^<@!?${client.user.id}>`)
Expand Down
13 changes: 13 additions & 0 deletions src/google-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
async function google_api(query) {
const { body } = await request.get("https://www.googleapis.com/customsearch/v1").query({
key: key, cx: csx, safe: "off", q: query
});

if (!body.items) return null;
return body.items[0];

}

module.exports = {
google_api,
};