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
# dotenv environment variable files
# dotenv environment variable files and credentials
credentials.json
.env
.env.development.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",
"discord.js": "^14.16.3",
"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:
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

View file

@ -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
);
}
},
};