From 81fd627635ff2a6e0a582a1a6357530ff5e35d97 Mon Sep 17 00:00:00 2001 From: Daniel Guirra Date: Thu, 21 Mar 2024 12:26:26 -0300 Subject: [PATCH] encoder tutorial --- .dockerignore | 1 + .envexemple | 5 - .envexemple.json | 5 + .gitignore | 2 + .vscode/settings.json | 3 - README.md | 29 ++- newbible.json => data/json/newbible.json | 0 docke-compose.exemple.yml | 10 + googleImage/index.ts | 198 ----------------- package.json | 8 +- src/command/Builder.ts | 19 +- src/command/commandAdvice.ts | 2 +- src/command/commandAllSkinsOfChampLol.ts | 2 +- src/command/commandAvatar.ts | 2 +- src/command/commandBan.ts | 2 +- src/command/commandBible.ts | 4 +- src/command/commandBibleVerDay.ts | 2 +- src/command/commandBuzz.ts | 2 +- src/command/commandClearChannel.ts | 2 +- src/command/commandClimate.ts | 2 +- src/command/commandClimateDaily.ts | 2 +- src/command/commandCoins.ts | 2 +- src/command/commandConfiaWill.ts | 2 +- src/command/commandDay.ts | 2 +- src/command/commandDeleteMyUser.ts | 2 +- src/command/commandDice.ts | 2 +- src/command/commandDolar.ts | 2 +- src/command/commandDuel.ts | 2 +- src/command/commandEditMyUser.ts | 2 +- src/command/commandFry.ts | 2 +- src/command/commandGif.ts | 2 +- src/command/commandGoogleImage.ts | 4 +- src/command/commandGrandeDia.ts | 2 +- src/command/commandHelp.ts | 2 +- src/command/commandHour.ts | 2 +- src/command/commandItsFine.ts | 2 +- src/command/commandLanguages.ts | 2 +- src/command/commandLeroLero.ts | 2 +- src/command/commandLoreLeagueOfLegends.ts | 2 +- src/command/commandLove.ts | 2 +- src/command/commandMotivacao.ts | 4 +- src/command/commandPdl.ts | 2 +- src/command/commandPeace.ts | 2 +- src/command/commandPensador.ts | 4 +- src/command/commandPing.ts | 2 +- src/command/commandQueuqLol.ts | 2 +- src/command/commandSaveUser.ts | 2 +- src/interfaces/Command.ts | 24 +++ src/service/senders/sendDaily.ts | 4 +- src/service/senders/sendLoveMessageDaily.ts | 2 +- src/util/googleImage/index.ts | 203 ++++++++++++++++++ build.js => util/build.js | 26 +-- .../decoderBase64ToEnvs.js | 0 .../decoderBase64ToSqlite.js | 0 .../encoderToBase64Envs.js | 0 .../encoderToBase64SqliteDump.js | 0 56 files changed, 326 insertions(+), 293 deletions(-) delete mode 100644 .envexemple create mode 100644 .envexemple.json delete mode 100644 .vscode/settings.json rename newbible.json => data/json/newbible.json (100%) create mode 100644 docke-compose.exemple.yml delete mode 100644 googleImage/index.ts create mode 100644 src/interfaces/Command.ts create mode 100644 src/util/googleImage/index.ts rename build.js => util/build.js (94%) rename decoderBase64ToEnvs.js => util/decoderBase64ToEnvs.js (100%) rename decoderBase64ToSqlite.js => util/decoderBase64ToSqlite.js (100%) rename encoderToBase64Envs.js => util/encoderToBase64Envs.js (100%) rename encoderToBase64SqliteDump.js => util/encoderToBase64SqliteDump.js (100%) diff --git a/.dockerignore b/.dockerignore index 2dca1f4..7f0d437 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,6 @@ node_modules npm-debug.log +docker-compose.yml yarn.lock Procfile .eslintrc.js diff --git a/.envexemple b/.envexemple deleted file mode 100644 index c1aaba9..0000000 --- a/.envexemple +++ /dev/null @@ -1,5 +0,0 @@ -BOTTOKEN="your-token-here" -PORT=80 -EMAIL='capivaradoti@gmail.com' -HORA=0 -PREFIX="*" diff --git a/.envexemple.json b/.envexemple.json new file mode 100644 index 0000000..5de65fb --- /dev/null +++ b/.envexemple.json @@ -0,0 +1,5 @@ +{ + "BOTTOKEN": "", + "TENORKEY": "", + "HGTOKEN": "" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1a3f8a1..a7d657d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ !.yarn/cache #.pnp.* bin +docker-compose.yml +.vscode dist .env envs.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 7edbdd3..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "compile-hero.disable-compile-files-on-did-save-code": false -} diff --git a/README.md b/README.md index 1e68a32..8d5dfcb 100644 --- a/README.md +++ b/README.md @@ -19,27 +19,27 @@ Token TenorGif [Offical TenorApi website](https://tenor.com/gifapi) NodeJS -- #### Windows +- #### Windows - [Official Node.js website](https://nodejs.org/) and download the installer. + [Official Node.js website](https://nodejs.org/) and download the installer. -- #### Node installation on Ubuntu +- #### Node installation on Ubuntu - $ sudo apt install nodejs - $ sudo apt install npm + $ sudo apt install nodejs + $ sudo apt install npm -- #### Other Operating Systems - You can find more information about the installation on the [official Node.js website](https://nodejs.org/) and the [official NPM website](https://npmjs.org/). +- #### Other Operating Systems + You can find more information about the installation on the [official Node.js website](https://nodejs.org/) and the [official NPM website](https://npmjs.org/). --- I used this version of NodeJS, I don't guarantee it will work on older versions $ node --version - v16.16.0 + v20.11.1 $ npm --version - v8.15.1 + v10.2.4 ## Deploy @@ -74,6 +74,17 @@ You can deploy to Heroku or Replit or install locally yourself. $ yarn dev $ npm run dev +## Can't Use Sure Docker or Docker Compose + +#### Docker + + $ docker build . --f ./Dockerfile --tag ts-bot-image + $ docker run -p 4040:4040 --name ts-bot ts-bot-image + +#### Compose + + $ docker-compose -up -d + ## License [MIT](https://choosealicense.com/licenses/mit/) diff --git a/newbible.json b/data/json/newbible.json similarity index 100% rename from newbible.json rename to data/json/newbible.json diff --git a/docke-compose.exemple.yml b/docke-compose.exemple.yml new file mode 100644 index 0000000..f7e9f32 --- /dev/null +++ b/docke-compose.exemple.yml @@ -0,0 +1,10 @@ +version: '3' + +services: + ts-bot: + image: ts-bot-image + container_name: ts-bot + ports: + - '4040:4040' + restart: always + command: node ./dist/src/server.js diff --git a/googleImage/index.ts b/googleImage/index.ts deleted file mode 100644 index 0e07233..0000000 --- a/googleImage/index.ts +++ /dev/null @@ -1,198 +0,0 @@ -import cheerio from 'cheerio'; -import { ColorResolvable, CommandInteraction, GuildTextBasedChannel, Message } from 'discord.js'; -import flatten = require('lodash'); -import * as queryString from 'querystring'; -import request = require('request'); - -import { IPensador } from '../src/interfaces/PensadorMessage'; -import { embedBuilder } from '../src/util/getEmbed'; - -var baseURL = 'http://images.google.com/search?'; - -var imageFileExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg']; -/** - * - * @param text - * @param channel - * @param mensage - */ -export function googleImage( - text: string, - channel: GuildTextBasedChannel, - mensage: Message, -) { - imageFunc(text, done); - function done(error: any, results?: any | undefined) { - if (error) { - console.error(error); - } else { - /** - * No Heroku results.__wrapped__[1][1].url - */ - let c = 0; - if (results.__wrapped__[0] === undefined) { - c++; - if (results.__wrapped__[c] === undefined) { - mensage.edit(`Não consegui achar nada com a palavra *${text}*`); - return - } - } - const response = results.__wrapped__[c][0].url; - if (channel && mensage) { - mensage.edit(`Achei aqui resultado de ${text}`); - return channel.send(response); - } else { - console.log('Erro channel or Message its null'); - } - } - } -} -export async function googleImagePensador( - embed: { embedTitle: string; embedColor?: ColorResolvable }, - pensadorData: IPensador, - command?: CommandInteraction | Message, - channel?: GuildTextBasedChannel, -) { - await imageFunc(pensadorData.author, resultfu); - function resultfu(error: any, results: any) { - if (error) { - console.log(error); - } else { - try { - let c = 0; - if (results.__wrapped__[0].length === 0) c++; - const image = results.__wrapped__[c][0].url; - if (!embed.embedColor) embed.embedColor = 'Random'; - if (command) { - command.reply({ - embeds: [ - embedBuilder( - embed.embedTitle, - pensadorData.message, - image, - pensadorData.author, - image, - image, - embed.embedColor, - ), - ], - }); - } - if (channel) { - channel.send({ - embeds: [ - embedBuilder( - embed.embedTitle, - pensadorData.message, - image, - pensadorData.author, - image, - image, - embed.embedColor, - ), - ], - }); - } - } catch (error) { - console.log(error) - } - } - } -} -export const imageFunc = async function googleImage(opts: string, done: any) { - var searchTerm; - var filterOutDomains = ['gstatic.com']; - - if (typeof opts === 'string') { - searchTerm = opts; - } - - let url = - baseURL + - queryString.stringify({ - tbm: 'isch', - q: searchTerm, - }); - - if (filterOutDomains) { - url += encodeURIComponent( - ' ' + filterOutDomains.map(addSiteExcludePrefix).join(' '), - ); - } - - let reqOpts = { - url: url, - headers: { - 'User-Agent': - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36', - }, - }; - - // console.log(reqOpts.url); - request(reqOpts, parseGISResponse); - - function parseGISResponse(error: any, response: any, body: any) { - if (error) { - done(error); - return; - } - var $ = cheerio.load(body); - var scripts: any = $('script'); - var scriptContents = []; - for (var i = 0; i < scripts.length; ++i) { - if (scripts[i].children.length > 0) { - const content = scripts[i].children[0].data; - if (containsAnyImageFileExtension(content)) { - scriptContents.push(content); - } - } - } - - done(error, flatten(scriptContents.map(collectImageRefs))); - - function collectImageRefs(content: any) { - var refs = []; - var re = /\["(http.+?)",(\d+),(\d+)\]/g; - var result; - while ((result = re.exec(content)) !== null) { - if (result.length > 3) { - let ref = { - url: result[1], - width: +result[3], - height: +result[2], - }; - if (domainIsOK(ref.url)) { - refs.push(ref); - } - } - } - return refs; - } - - function domainIsOK(url: any) { - if (!filterOutDomains) { - return true; - } else { - return filterOutDomains.every(skipDomainIsNotInURL); - } - - function skipDomainIsNotInURL(skipDomain: any) { - return url.indexOf(skipDomain) === -1; - } - } - } - return; -}; - -function addSiteExcludePrefix(s: any) { - return '-site:' + s; -} - -function containsAnyImageFileExtension(s: any) { - var lowercase = s.toLowerCase(); - return imageFileExtensions.some(containsImageFileExtension); - - function containsImageFileExtension(ext: any) { - return lowercase.includes(ext); - } -} diff --git a/package.json b/package.json index fbd91d9..692b850 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { "name": "ts-bot", - "version": "1.0", + "version": "1.1", "icon": "https://cdn.discordapp.com/avatars/811255307673010246/8f145d7279847a9a6e46efd5ee3df6bf.webp", "repository": "https://github.com/danielguirra/ts-bot", "description": "DiscordBot in TypeScript", "license": "MIT", "author": "Daniel Guirra ", "scripts": { - "build": "node ./build.js", + "build": "node ./util/build.js", "dev": "npx nodemon ./src/server.ts", - "start": "node ./build && node ./dist/src/server.js", + "start": "npm run build && node ./dist/src/server.js", "test": "node --import tsx --test ./test/comand.spec.ts", "startnotbuild": "npx ts-node ./src/server.ts", "commandCreator": "node ./util/commandBaseTs/commandCreatorForName.js" @@ -44,4 +44,4 @@ "nodemon": "^2.0.13", "ts-node": "^10.9.2" } -} +} \ No newline at end of file diff --git a/src/command/Builder.ts b/src/command/Builder.ts index 9a87b6b..423684b 100644 --- a/src/command/Builder.ts +++ b/src/command/Builder.ts @@ -8,24 +8,7 @@ import { } from 'discord.js'; import { allComands } from './allComands'; - -export class Command { - constructor(comm: Command) { - Object.assign(this, comm); - } - - data!: - | SlashCommandBuilder - | Omit; - executeMessageCommand!: ( - message: Message - ) => Promise | void | undefined>; - executeSlashCommand!: ( - commandSlash: CommandInteraction - ) => Promise< - void | Message | InteractionResponse | undefined - >; -} +import { Command } from '../interfaces/Command'; export const commands = new Collection() as Collection; diff --git a/src/command/commandAdvice.ts b/src/command/commandAdvice.ts index 8a7a371..0538c85 100644 --- a/src/command/commandAdvice.ts +++ b/src/command/commandAdvice.ts @@ -8,7 +8,7 @@ import { import { embedBuilder } from '../../src/util/getEmbed'; import { translateText } from '../util/translate'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandAllSkinsOfChampLol.ts b/src/command/commandAllSkinsOfChampLol.ts index 5038d72..53b57f7 100644 --- a/src/command/commandAllSkinsOfChampLol.ts +++ b/src/command/commandAllSkinsOfChampLol.ts @@ -5,7 +5,7 @@ import { SlashCommandBuilder, } from 'discord.js'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandAvatar.ts b/src/command/commandAvatar.ts index 36ca8ca..ad146cc 100644 --- a/src/command/commandAvatar.ts +++ b/src/command/commandAvatar.ts @@ -5,7 +5,7 @@ import { SlashCommandBuilder, } from 'discord.js'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandBan.ts b/src/command/commandBan.ts index 5729c25..11b66db 100644 --- a/src/command/commandBan.ts +++ b/src/command/commandBan.ts @@ -7,7 +7,7 @@ import { User, } from 'discord.js'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandBible.ts b/src/command/commandBible.ts index 4df1b81..0549c19 100644 --- a/src/command/commandBible.ts +++ b/src/command/commandBible.ts @@ -8,7 +8,7 @@ import { import * as fs from 'fs'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export @@ -103,7 +103,7 @@ export class Bible { verse: number | undefined; static json: BibleJson = JSON.parse( - fs.readFileSync('./newbible.json', 'utf-8') + fs.readFileSync('./data/json/newbible.json', 'utf-8') ); static verifylengthOfBookBasedInName = (bookName: string) => { diff --git a/src/command/commandBibleVerDay.ts b/src/command/commandBibleVerDay.ts index 9e1cc09..add743e 100644 --- a/src/command/commandBibleVerDay.ts +++ b/src/command/commandBibleVerDay.ts @@ -7,7 +7,7 @@ import { } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; const bibleUrl = process.env.URLBIBLE; /** diff --git a/src/command/commandBuzz.ts b/src/command/commandBuzz.ts index a116a0e..ae8df1c 100644 --- a/src/command/commandBuzz.ts +++ b/src/command/commandBuzz.ts @@ -9,7 +9,7 @@ import { import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandClearChannel.ts b/src/command/commandClearChannel.ts index cbe925a..9936550 100644 --- a/src/command/commandClearChannel.ts +++ b/src/command/commandClearChannel.ts @@ -7,7 +7,7 @@ import { import { embedBuilder } from '../../src/util/getEmbed'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandClimate.ts b/src/command/commandClimate.ts index 5ae08d0..62b415e 100644 --- a/src/command/commandClimate.ts +++ b/src/command/commandClimate.ts @@ -7,7 +7,7 @@ import { import { sendClimateCurrentTime } from '../service/senders/sendClimate'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandClimateDaily.ts b/src/command/commandClimateDaily.ts index 499dda5..6809ba4 100644 --- a/src/command/commandClimateDaily.ts +++ b/src/command/commandClimateDaily.ts @@ -2,7 +2,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { sendClimate } from '../service/senders/sendClimate'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandCoins.ts b/src/command/commandCoins.ts index 1a04ae1..5b78fcd 100644 --- a/src/command/commandCoins.ts +++ b/src/command/commandCoins.ts @@ -1,7 +1,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandConfiaWill.ts b/src/command/commandConfiaWill.ts index 20bf77b..1a6776e 100644 --- a/src/command/commandConfiaWill.ts +++ b/src/command/commandConfiaWill.ts @@ -8,7 +8,7 @@ import { import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandDay.ts b/src/command/commandDay.ts index fadd5e2..c4bc1e5 100644 --- a/src/command/commandDay.ts +++ b/src/command/commandDay.ts @@ -3,7 +3,7 @@ import { Client, Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; import { dailySender } from '../service/senders/dailySender'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; import { env } from '../envs'; /** diff --git a/src/command/commandDeleteMyUser.ts b/src/command/commandDeleteMyUser.ts index 30766d9..5ece655 100644 --- a/src/command/commandDeleteMyUser.ts +++ b/src/command/commandDeleteMyUser.ts @@ -2,7 +2,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; import { UserDB } from '../database/users/user.class'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandDice.ts b/src/command/commandDice.ts index 3ab6be7..cb00aaa 100644 --- a/src/command/commandDice.ts +++ b/src/command/commandDice.ts @@ -1,7 +1,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandDolar.ts b/src/command/commandDolar.ts index bcac6e7..72a3921 100644 --- a/src/command/commandDolar.ts +++ b/src/command/commandDolar.ts @@ -2,7 +2,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { sendDolarDaily } from '../service/senders/sendDolarDaily'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandDuel.ts b/src/command/commandDuel.ts index bf03fbc..6618b71 100644 --- a/src/command/commandDuel.ts +++ b/src/command/commandDuel.ts @@ -8,7 +8,7 @@ import { import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandEditMyUser.ts b/src/command/commandEditMyUser.ts index 24f173d..f6c1392 100644 --- a/src/command/commandEditMyUser.ts +++ b/src/command/commandEditMyUser.ts @@ -2,7 +2,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; import { UserDB } from '../database/users/user.class'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandFry.ts b/src/command/commandFry.ts index 3c18ae1..70a0e59 100644 --- a/src/command/commandFry.ts +++ b/src/command/commandFry.ts @@ -8,7 +8,7 @@ import { import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export * Não esqueça de exportar diff --git a/src/command/commandGif.ts b/src/command/commandGif.ts index d07945d..47ab7df 100644 --- a/src/command/commandGif.ts +++ b/src/command/commandGif.ts @@ -1,7 +1,7 @@ import axios from 'axios'; import { Message, SlashCommandBuilder } from 'discord.js'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; const tenor = process.env.TENORKEY; /** diff --git a/src/command/commandGoogleImage.ts b/src/command/commandGoogleImage.ts index 5d56958..8e8de98 100644 --- a/src/command/commandGoogleImage.ts +++ b/src/command/commandGoogleImage.ts @@ -1,8 +1,8 @@ import { CommandInteraction, Message, SlashCommandBuilder } from 'discord.js'; -import { googleImage } from '../../googleImage'; +import { googleImage } from '../util/googleImage'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; export const ime: Command = { data: new SlashCommandBuilder() diff --git a/src/command/commandGrandeDia.ts b/src/command/commandGrandeDia.ts index 94ec477..f246b07 100644 --- a/src/command/commandGrandeDia.ts +++ b/src/command/commandGrandeDia.ts @@ -8,7 +8,7 @@ import { import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandHelp.ts b/src/command/commandHelp.ts index 93f9874..c4186ff 100644 --- a/src/command/commandHelp.ts +++ b/src/command/commandHelp.ts @@ -1,7 +1,7 @@ import { Guild, Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; import { env } from '../envs'; /** diff --git a/src/command/commandHour.ts b/src/command/commandHour.ts index 2d398e9..f86b2af 100644 --- a/src/command/commandHour.ts +++ b/src/command/commandHour.ts @@ -1,6 +1,6 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandItsFine.ts b/src/command/commandItsFine.ts index bb9ee6d..94ab30a 100644 --- a/src/command/commandItsFine.ts +++ b/src/command/commandItsFine.ts @@ -3,7 +3,7 @@ import { AttachmentBuilder, Message, SlashCommandBuilder } from 'discord.js'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandLanguages.ts b/src/command/commandLanguages.ts index 7efe4fb..c8b6104 100644 --- a/src/command/commandLanguages.ts +++ b/src/command/commandLanguages.ts @@ -1,7 +1,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandLeroLero.ts b/src/command/commandLeroLero.ts index 86239ef..743756d 100644 --- a/src/command/commandLeroLero.ts +++ b/src/command/commandLeroLero.ts @@ -2,7 +2,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; import { getLeroLero } from '../util/lerolero'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandLoreLeagueOfLegends.ts b/src/command/commandLoreLeagueOfLegends.ts index eb582a1..b4bbd25 100644 --- a/src/command/commandLoreLeagueOfLegends.ts +++ b/src/command/commandLoreLeagueOfLegends.ts @@ -4,7 +4,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import roles from '../../data/json/champRole.json'; import names from '../../data/json/nameslol.json'; import { embedBuilder } from '../../src/util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandLove.ts b/src/command/commandLove.ts index 69851f3..d91f146 100644 --- a/src/command/commandLove.ts +++ b/src/command/commandLove.ts @@ -2,7 +2,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { sendLoveMessageDaily } from '../service/senders/sendLoveMessageDaily'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandMotivacao.ts b/src/command/commandMotivacao.ts index 67297a4..be2a845 100644 --- a/src/command/commandMotivacao.ts +++ b/src/command/commandMotivacao.ts @@ -1,8 +1,8 @@ import { Message, SlashCommandBuilder } from 'discord.js'; -import { googleImagePensador } from '../../googleImage'; +import { googleImagePensador } from '../util/googleImage'; import { pensador } from '../util/pensador'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandPdl.ts b/src/command/commandPdl.ts index 815da63..b5366c8 100644 --- a/src/command/commandPdl.ts +++ b/src/command/commandPdl.ts @@ -9,7 +9,7 @@ import { import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandPeace.ts b/src/command/commandPeace.ts index c9b5689..7e7e066 100644 --- a/src/command/commandPeace.ts +++ b/src/command/commandPeace.ts @@ -9,7 +9,7 @@ import { import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; import { loadinCreator } from '../util/loadin'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandPensador.ts b/src/command/commandPensador.ts index 2494671..aba7240 100644 --- a/src/command/commandPensador.ts +++ b/src/command/commandPensador.ts @@ -1,8 +1,8 @@ import { Message, SlashCommandBuilder } from 'discord.js'; -import { googleImagePensador } from '../../googleImage'; +import { googleImagePensador } from '../util/googleImage'; import { pensador as uga } from '../util/pensador'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandPing.ts b/src/command/commandPing.ts index 78d7e9b..379de94 100644 --- a/src/command/commandPing.ts +++ b/src/command/commandPing.ts @@ -1,7 +1,7 @@ import { Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../util/getEmbed'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; export const ping: Command = { data: new SlashCommandBuilder() diff --git a/src/command/commandQueuqLol.ts b/src/command/commandQueuqLol.ts index 9e5a5ff..00381b0 100644 --- a/src/command/commandQueuqLol.ts +++ b/src/command/commandQueuqLol.ts @@ -7,7 +7,7 @@ import * as fs from 'fs'; import { embedBuilder } from '../../src/util/getEmbed'; import { channelItsGuildTextChannel } from '../util/channelItsGuildTextChannel'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/command/commandSaveUser.ts b/src/command/commandSaveUser.ts index a93b859..6f2b230 100644 --- a/src/command/commandSaveUser.ts +++ b/src/command/commandSaveUser.ts @@ -2,7 +2,7 @@ import { Interaction, Message, SlashCommandBuilder } from 'discord.js'; import { embedBuilder } from '../../src/util/getEmbed'; import { UserDB } from '../database/users/user.class'; -import { Command } from './Builder'; +import { Command } from '../interfaces/Command'; /** * Don't forget to export diff --git a/src/interfaces/Command.ts b/src/interfaces/Command.ts new file mode 100644 index 0000000..fe69fe0 --- /dev/null +++ b/src/interfaces/Command.ts @@ -0,0 +1,24 @@ +import { + SlashCommandBuilder, + Message, + CommandInteraction, + InteractionResponse, +} from 'discord.js'; + +export class Command { + constructor(comm: Command) { + Object.assign(this, comm); + } + + data!: + | SlashCommandBuilder + | Omit; + executeMessageCommand!: ( + message: Message + ) => Promise | void | undefined>; + executeSlashCommand!: ( + commandSlash: CommandInteraction + ) => Promise< + void | Message | InteractionResponse | undefined + >; +} diff --git a/src/service/senders/sendDaily.ts b/src/service/senders/sendDaily.ts index 2340674..d0b5e13 100644 --- a/src/service/senders/sendDaily.ts +++ b/src/service/senders/sendDaily.ts @@ -1,6 +1,6 @@ import { GuildTextBasedChannel } from 'discord.js'; -import { googleImagePensador } from '../../../googleImage'; +import { googleImagePensador } from '../../util/googleImage'; import { IPensador } from '../../interfaces/PensadorMessage'; import { pensador } from '../../util/pensador'; import { embedBuilder } from '../../util/getEmbed'; @@ -17,7 +17,7 @@ export const sendDaily = async (channelDaily: GuildTextBasedChannel) => { data.author, undefined, undefined, - 'DarkVividPink', + 'Random', url.href ); diff --git a/src/service/senders/sendLoveMessageDaily.ts b/src/service/senders/sendLoveMessageDaily.ts index 09e8ecf..9f7f5f0 100644 --- a/src/service/senders/sendLoveMessageDaily.ts +++ b/src/service/senders/sendLoveMessageDaily.ts @@ -1,6 +1,6 @@ import { GuildTextBasedChannel } from 'discord.js'; -import { googleImagePensador } from '../../../googleImage'; +import { googleImagePensador } from '../../util/googleImage'; import { IPensador } from '../../interfaces/PensadorMessage'; import { pensador } from '../../util/pensador'; import { embedBuilder } from '../../util/getEmbed'; diff --git a/src/util/googleImage/index.ts b/src/util/googleImage/index.ts new file mode 100644 index 0000000..b69dbb8 --- /dev/null +++ b/src/util/googleImage/index.ts @@ -0,0 +1,203 @@ +import cheerio from 'cheerio'; +import { + ColorResolvable, + CommandInteraction, + GuildTextBasedChannel, + Message, +} from 'discord.js'; +import flatten = require('lodash'); +import * as queryString from 'querystring'; +import request = require('request'); + +import { IPensador } from '../../interfaces/PensadorMessage'; +import { embedBuilder } from '../getEmbed'; + +var baseURL = 'http://images.google.com/search?'; + +var imageFileExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg']; +/** + * + * @param text + * @param channel + * @param mensage + */ +export function googleImage( + text: string, + channel: GuildTextBasedChannel, + mensage: Message +) { + imageFunc(text, done); + function done(error: any, results?: any | undefined) { + if (error) { + console.error(error); + } else { + /** + * No Heroku results.__wrapped__[1][1].url + */ + let c = 0; + if (results.__wrapped__[0] === undefined) { + c++; + if (results.__wrapped__[c] === undefined) { + mensage.edit(`Não consegui achar nada com a palavra *${text}*`); + return; + } + } + const response = results.__wrapped__[c][0].url; + if (channel && mensage) { + mensage.edit(`Achei aqui resultado de ${text}`); + return channel.send(response); + } else { + console.log('Erro channel or Message its null'); + } + } + } +} +export async function googleImagePensador( + embed: { embedTitle: string; embedColor?: ColorResolvable }, + pensadorData: IPensador, + command?: CommandInteraction | Message, + channel?: GuildTextBasedChannel +) { + await imageFunc(pensadorData.author, resultfu); + function resultfu(error: any, results: any) { + if (error) { + console.log(error); + } else { + try { + let c = 0; + if (results.__wrapped__[0].length === 0) c++; + const image = results.__wrapped__[c][0].url; + if (!embed.embedColor) embed.embedColor = 'Random'; + if (command) { + command.reply({ + embeds: [ + embedBuilder( + embed.embedTitle, + pensadorData.message, + image, + pensadorData.author, + image, + image, + embed.embedColor + ), + ], + }); + } + if (channel) { + channel.send({ + embeds: [ + embedBuilder( + embed.embedTitle, + pensadorData.message, + image, + pensadorData.author, + image, + image, + embed.embedColor + ), + ], + }); + } + } catch (error) { + console.log(error); + } + } + } +} +export const imageFunc = async function googleImage(opts: string, done: any) { + var searchTerm; + var filterOutDomains = ['gstatic.com']; + + if (typeof opts === 'string') { + searchTerm = opts; + } + + let url = + baseURL + + queryString.stringify({ + tbm: 'isch', + q: searchTerm, + }); + + if (filterOutDomains) { + url += encodeURIComponent( + ' ' + filterOutDomains.map(addSiteExcludePrefix).join(' ') + ); + } + + let reqOpts = { + url: url, + headers: { + 'User-Agent': + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36', + }, + }; + + // console.log(reqOpts.url); + request(reqOpts, parseGISResponse); + + function parseGISResponse(error: any, response: any, body: any) { + if (error) { + done(error); + return; + } + var $ = cheerio.load(body); + var scripts: any = $('script'); + var scriptContents = []; + for (var i = 0; i < scripts.length; ++i) { + if (scripts[i].children.length > 0) { + const content = scripts[i].children[0].data; + if (containsAnyImageFileExtension(content)) { + scriptContents.push(content); + } + } + } + + done(error, flatten(scriptContents.map(collectImageRefs))); + + function collectImageRefs(content: any) { + var refs = []; + var re = /\["(http.+?)",(\d+),(\d+)\]/g; + var result; + while ((result = re.exec(content)) !== null) { + if (result.length > 3) { + let ref = { + url: result[1], + width: +result[3], + height: +result[2], + }; + if (domainIsOK(ref.url)) { + refs.push(ref); + } + } + } + return refs; + } + + function domainIsOK(url: any) { + if (!filterOutDomains) { + return true; + } else { + return filterOutDomains.every(skipDomainIsNotInURL); + } + + function skipDomainIsNotInURL(skipDomain: any) { + return url.indexOf(skipDomain) === -1; + } + } + } + return; +}; + +function addSiteExcludePrefix(s: any) { + return '-site:' + s; +} + +function containsAnyImageFileExtension(s: any) { + var lowercase = s.toLowerCase(); + return imageFileExtensions.some(containsImageFileExtension); + + function containsImageFileExtension(ext: any) { + return lowercase.includes(ext); + } +} diff --git a/build.js b/util/build.js similarity index 94% rename from build.js rename to util/build.js index 689c7d5..0cf5620 100644 --- a/build.js +++ b/util/build.js @@ -1,13 +1,13 @@ -const { exec } = require("child_process"); - -const fs = require("fs"); - -const dir = "./dist"; - -if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); -} -exec("npx tsc", (f) => { - if (f) console.log(f.message); - else console.log("Build Ok"); -}); +const { exec } = require("child_process"); + +const fs = require("fs"); + +const dir = "./dist"; + +if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); +} +exec("npx tsc", (f) => { + if (f) console.log(f.message); + else console.log("Build Ok"); +}); diff --git a/decoderBase64ToEnvs.js b/util/decoderBase64ToEnvs.js similarity index 100% rename from decoderBase64ToEnvs.js rename to util/decoderBase64ToEnvs.js diff --git a/decoderBase64ToSqlite.js b/util/decoderBase64ToSqlite.js similarity index 100% rename from decoderBase64ToSqlite.js rename to util/decoderBase64ToSqlite.js diff --git a/encoderToBase64Envs.js b/util/encoderToBase64Envs.js similarity index 100% rename from encoderToBase64Envs.js rename to util/encoderToBase64Envs.js diff --git a/encoderToBase64SqliteDump.js b/util/encoderToBase64SqliteDump.js similarity index 100% rename from encoderToBase64SqliteDump.js rename to util/encoderToBase64SqliteDump.js