booru api auth

This commit is contained in:
Ashley Graves 2024-10-10 11:15:19 +02:00
parent c878e7fd04
commit ba50f40868
5 changed files with 45 additions and 31 deletions

3
.gitignore vendored
View file

@ -72,7 +72,8 @@ web_modules/
# Yarn Integrity file # Yarn Integrity file
.yarn-integrity .yarn-integrity
# dotenv environment variable files # dotenv environment variable files and credentials
credentials.json
.env .env
.env.development.local .env.development.local
.env.test.local .env.test.local

6
credentials.example.json Normal file
View file

@ -0,0 +1,6 @@
{
"gelbooru.com": {
"user_id": "42069",
"api_key": "api key"
}
}

View file

@ -11,6 +11,7 @@
"booru": "^2.6.8", "booru": "^2.6.8",
"discord.js": "^14.16.3", "discord.js": "^14.16.3",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"groq-sdk": "^0.7.0" "groq-sdk": "^0.7.0",
"html-entities": "^2.5.2"
} }
} }

View file

@ -20,6 +20,9 @@ importers:
groq-sdk: groq-sdk:
specifier: ^0.7.0 specifier: ^0.7.0
version: 0.7.0 version: 0.7.0
html-entities:
specifier: ^2.5.2
version: 2.5.2
packages: packages:
@ -144,6 +147,9 @@ packages:
groq-sdk@0.7.0: groq-sdk@0.7.0:
resolution: {integrity: sha512-OgPqrRtti5MjEVclR8sgBHrhSkTLdFCmi47yrEF29uJZaiCkX3s7bXpnMhq8Lwoe1f4AwgC0qGOeHXpeSgu5lg==} resolution: {integrity: sha512-OgPqrRtti5MjEVclR8sgBHrhSkTLdFCmi47yrEF29uJZaiCkX3s7bXpnMhq8Lwoe1f4AwgC0qGOeHXpeSgu5lg==}
html-entities@2.5.2:
resolution: {integrity: sha512-K//PSRMQk4FZ78Kyau+mZurHn3FH0Vwr+H36eE0rPbeYkRRi9YxceYPhuN60UwWorxyKHhqoAJl2OFKa4BVtaA==}
humanize-ms@1.2.1: humanize-ms@1.2.1:
resolution: {integrity: sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==} resolution: {integrity: sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==}
@ -381,6 +387,8 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- encoding - encoding
html-entities@2.5.2: {}
humanize-ms@1.2.1: humanize-ms@1.2.1:
dependencies: dependencies:
ms: 2.1.3 ms: 2.1.3

View file

@ -1,5 +1,7 @@
const { InteractionContextType, ApplicationIntegrationType, SlashCommandBuilder, EmbedBuilder, escapeMarkdown, bold } = require("discord.js"); 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 Booru = require('booru');
const boorus = []; const boorus = [];
@ -54,7 +56,7 @@ function notEmpty(str) {
const regexCutTags = /[\S\s]{1,75}[^,]{0,25}/; const regexCutTags = /[\S\s]{1,75}[^,]{0,25}/;
function formatTags(tags) { function formatTags(tags) {
const tagString = tags.join(', ') const tagString = decode(tags.join(', '))
if (tagString.length < 100) { if (tagString.length < 100) {
return tagString return tagString
@ -100,9 +102,11 @@ const aiTags = [
]; ];
function isAIPost(post) { 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 = { module.exports = {
data, data,
async execute(interaction) { async execute(interaction) {
@ -110,19 +114,19 @@ module.exports = {
const booru = interaction.options.getString("booru") ?? defaultBooru; const booru = interaction.options.getString("booru") ?? defaultBooru;
const nsfw = interaction.options.getBoolean("nsfw") ?? false; const nsfw = interaction.options.getBoolean("nsfw") ?? false;
await interaction.deferReply(); //await interaction.deferReply();
console.log(booru, tags); console.log(booru, tags);
const startTime = process.hrtime.bigint(); const startTime = process.hrtime.bigint();
var post = null; var post = null;
while (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) { if (newPost == null) {
await interaction.followUp("Could not find any post matching tags."); await interaction.reply("Could not find any post matching tags.");
return; return;
} }
if (!nsfw && isNSFWPost(newPost) || isAIPost(newPost)) continue; if (/*!nsfw && isNSFWPost(newPost) || */isAIPost(newPost)) continue;
post = newPost; post = newPost;
} }
@ -148,44 +152,38 @@ module.exports = {
timeTaken ? formatTime(timeTaken) : '', timeTaken ? formatTime(timeTaken) : '',
].filter(notEmpty).join(' · ') ].filter(notEmpty).join(' · ')
if (isEmbeddableFileType(ext)) { if (isEmbeddableFileType(ext)) {
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor("#cba6f7") .setColor("#cba6f7")
.setTitle(`Post #${post.id}`) .setTitle(`Post #${post.id}`)
.setURL(post.postView) .setURL(post.postView)
//.setDescription(description) .setDescription(description)
.setFields([{ /* .setFields([{
name: "Score", name: "Score",
value: formatScore(post.score), value: formatScore(post.score),
inline: true inline: true
}, { }, {
name: "Rating", name: "Rating",
value: formatRating(post.rating), value: formatRating(post.rating),
inline: true inline: true
}, { }, {
name: "File", name: "Tags",
value: `[${basename(post.fileUrl)}](${post.fileUrl})`, value: formatTags(post.tags),
inline: true inline: false
}, { }])*/
name: "Tags",
value: formatTags(post.tags),
inline: false
}])
.setImage(post.fileUrl) .setImage(post.fileUrl)
.setFooter({ .setFooter({
text: footerText, text: footerText,
iconURL: `https://${post.booru.domain}/favicon.ico`, iconURL: `https://${post.booru.domain}/favicon.ico`,
}) })
await interaction.followUp({ embeds: [embed] }); await interaction.reply({ embeds: [embed.data] });
} else { } else {
await interaction.followUp( await interaction.reply(
'>>> ' + bold(`[Post #${post.id}](<${post.postView}>)`) + "\n" + '>>> ' + bold(`[Post #${post.id}](<${post.postView}>)`) + "\n" +
description + "\n" + description + "\n" +
footerText footerText
); );
} }
}, },
}; };