From ba50f4086830a5069aa328fed1adad07b2948557 Mon Sep 17 00:00:00 2001 From: Ashley Graves Date: Thu, 10 Oct 2024 11:15:19 +0200 Subject: [PATCH] booru api auth --- .gitignore | 3 +- credentials.example.json | 6 ++++ package.json | 3 +- pnpm-lock.yaml | 8 ++++++ src/commands/fun/gelbooru.js | 56 +++++++++++++++++------------------- 5 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 credentials.example.json diff --git a/.gitignore b/.gitignore index 6a7d6d8..1b3e370 100644 --- a/.gitignore +++ b/.gitignore @@ -72,7 +72,8 @@ web_modules/ # Yarn Integrity file .yarn-integrity -# dotenv environment variable files +# dotenv environment variable files and credentials +credentials.json .env .env.development.local .env.test.local diff --git a/credentials.example.json b/credentials.example.json new file mode 100644 index 0000000..fa2ef60 --- /dev/null +++ b/credentials.example.json @@ -0,0 +1,6 @@ +{ + "gelbooru.com": { + "user_id": "42069", + "api_key": "api key" + } +} \ No newline at end of file diff --git a/package.json b/package.json index d0cec1a..8ef416c 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "booru": "^2.6.8", "discord.js": "^14.16.3", "dotenv": "^16.4.5", - "groq-sdk": "^0.7.0" + "groq-sdk": "^0.7.0", + "html-entities": "^2.5.2" } } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66393b2..0627501 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: groq-sdk: specifier: ^0.7.0 version: 0.7.0 + html-entities: + specifier: ^2.5.2 + version: 2.5.2 packages: @@ -144,6 +147,9 @@ packages: groq-sdk@0.7.0: resolution: {integrity: sha512-OgPqrRtti5MjEVclR8sgBHrhSkTLdFCmi47yrEF29uJZaiCkX3s7bXpnMhq8Lwoe1f4AwgC0qGOeHXpeSgu5lg==} + html-entities@2.5.2: + resolution: {integrity: sha512-K//PSRMQk4FZ78Kyau+mZurHn3FH0Vwr+H36eE0rPbeYkRRi9YxceYPhuN60UwWorxyKHhqoAJl2OFKa4BVtaA==} + humanize-ms@1.2.1: resolution: {integrity: sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==} @@ -381,6 +387,8 @@ snapshots: transitivePeerDependencies: - encoding + html-entities@2.5.2: {} + humanize-ms@1.2.1: dependencies: ms: 2.1.3 diff --git a/src/commands/fun/gelbooru.js b/src/commands/fun/gelbooru.js index de6911d..e820404 100644 --- a/src/commands/fun/gelbooru.js +++ b/src/commands/fun/gelbooru.js @@ -1,5 +1,7 @@ const { InteractionContextType, ApplicationIntegrationType, SlashCommandBuilder, EmbedBuilder, escapeMarkdown, bold } = require("discord.js"); -const { basename, extname } = require("node:path"); +const { readFileSync } = require("node:fs"); +const { extname } = require("node:path"); +const { decode } = require("html-entities"); const Booru = require('booru'); const boorus = []; @@ -54,7 +56,7 @@ function notEmpty(str) { const regexCutTags = /[\S\s]{1,75}[^,]{0,25}/; function formatTags(tags) { - const tagString = tags.join(', ') + const tagString = decode(tags.join(', ')) if (tagString.length < 100) { return tagString @@ -100,9 +102,11 @@ const aiTags = [ ]; function isAIPost(post) { - return aiTags.map(i=>post.tags.includes(i)).includes(true); + return aiTags.map(i => post.tags.includes(i)).includes(true); } +var credentials = JSON.parse(readFileSync("credentials.json")); + module.exports = { data, async execute(interaction) { @@ -110,19 +114,19 @@ module.exports = { const booru = interaction.options.getString("booru") ?? defaultBooru; const nsfw = interaction.options.getBoolean("nsfw") ?? false; - await interaction.deferReply(); + //await interaction.deferReply(); console.log(booru, tags); const startTime = process.hrtime.bigint(); var post = null; while (post == null) { - var newPost = (await Booru.search(booru, tags, { limit: 1, random: true }))[0]; + var newPost = (await Booru.search(booru, ["-rating:explicit", ...tags], { limit: 1, random: true, credentials: credentials[booru] ?? null }))[0]; if (newPost == null) { - await interaction.followUp("Could not find any post matching tags."); + await interaction.reply("Could not find any post matching tags."); return; } - if (!nsfw && isNSFWPost(newPost) || isAIPost(newPost)) continue; + if (/*!nsfw && isNSFWPost(newPost) || */isAIPost(newPost)) continue; post = newPost; } @@ -148,44 +152,38 @@ module.exports = { timeTaken ? formatTime(timeTaken) : '', ].filter(notEmpty).join(' ยท ') - if (isEmbeddableFileType(ext)) { const embed = new EmbedBuilder() .setColor("#cba6f7") .setTitle(`Post #${post.id}`) .setURL(post.postView) - //.setDescription(description) - .setFields([{ - name: "Score", - value: formatScore(post.score), - inline: true - }, { - name: "Rating", - value: formatRating(post.rating), - inline: true - }, { - name: "File", - value: `[${basename(post.fileUrl)}](${post.fileUrl})`, - inline: true - }, { - name: "Tags", - value: formatTags(post.tags), - inline: false - }]) + .setDescription(description) + /* .setFields([{ + name: "Score", + value: formatScore(post.score), + inline: true + }, { + name: "Rating", + value: formatRating(post.rating), + inline: true + }, { + name: "Tags", + value: formatTags(post.tags), + inline: false + }])*/ .setImage(post.fileUrl) .setFooter({ text: footerText, iconURL: `https://${post.booru.domain}/favicon.ico`, }) - await interaction.followUp({ embeds: [embed] }); + await interaction.reply({ embeds: [embed.data] }); } else { - await interaction.followUp( + await interaction.reply( '>>> ' + bold(`[Post #${post.id}](<${post.postView}>)`) + "\n" + description + "\n" + footerText ); } - }, };