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

blacklist tags #76

Closed
dimactavishy opened this issue Nov 15, 2021 · 6 comments
Closed

blacklist tags #76

dimactavishy opened this issue Nov 15, 2021 · 6 comments
Assignees

Comments

@dimactavishy
Copy link

dimactavishy commented Nov 15, 2021

just a simple question
is there a feature to blacklist tags on this package?
if not, i already have my own way to block certain words on my discord bot code with just using if(message.content.includes(blacklist)).
but this method couldn't blacklist more than one tags without making the code incredibly messy.

edit: even if i blacklisted a word using that code, it could still return image tagged with something that i want to blacklist if the tags are specific enough.

@AtoraSuunva
Copy link
Owner

You can use SearchResults#blacklist:

const posts = await booru.search('sb', ['cat'], { limit: 3 })
// Remove all posts from search results that have "dress" or "clouds" in their tags
const filtered = posts.blacklist(['dress', 'clouds'])
console.log(filtered[0])

Since this filters tags on your end (and not the API), you can end up with no results for 1 page and might need to search the next page:

const posts = await booru.search('sb', ['cat'], { limit: 3, page: 2 })

I'll leave it up to you to decide if you just want to search 1 page only, or if you want to paginate until you find a result for the user (but you'll need to make sure that you don't get ratelimited or fetch pages forever)


Most APIs will allow you to remove a tag from search results using -tag, but most APIs have a strict limit on max number of tags.

So blacklisting tags is done in the package, not the API, so you can blacklist more than 3 tags.


You can check out the implementation if you want to know how you could've implemented it in your own code (or if you need something more specific)

You need to compare the array of tags you want blacklisted with the array of tags of each post

If there's 1+ matches then you can filter out that post

@dimactavishy
Copy link
Author

dimactavishy commented Nov 16, 2021

Thanks, the tag that i desire to blacklist wasn't all that common anyways so i don't think it would return a "no result" message.

I'm sorry if this is out of the topic, but sometimes the embed for my bot doesn't show the image result (fileURL). Fortunately, i put a postView so i can still see the result and i figure that it's probably too high-res for Discord to put in on an embed. Is there a way to resize the output image? Or do i have to use my own custom code for it?

Edit: does SearchResults#blacklist works on promise or does it only work on async/await? And if it does, where to put it?

@AtoraSuunva
Copy link
Owner

You might be able to use Post#sampleUrl or Post#previewUrl but some Boorus don't send a medium/small preview of the image.

I know some file types and I think images over 10MB don't embed on Discord? If you have some examples I could check them out.

If the booru doesn't provide sample/preview sizes, then you'd have to resize it yourself, booru doesn't provide any image-editing code


Edit: does SearchResults#blacklist works on promise or does it only work on async/await? And if it does, where to put it?

Booru.search returns a Promise that resolves with SearchResults. async/await is just another way to work with promises instead of .then()

Or in other words:

booru.search('sb', ['cat'])
  .then(posts => {
    // "posts" is an instance of "SearchResults"
    const filtered = posts.blacklist(['dog'])
  })

is functionally the same as

const posts = await booru.search('sb', ['cat'])
// "posts" is an instance of "SearchResults"
const filtered = posts.blacklist(['dog'])

(await can only be used inside an async function, you can check out the MDN documentation for more info)

@dimactavishy
Copy link
Author

dimactavishy commented Nov 16, 2021

Thanks, i already figured out how to blacklist.

You might be able to use Post#sampleUrl or Post#previewUrl but some Boorus don't send a medium/small preview of the image.
I know some file types and I think images over 10MB don't embed on Discord? If you have some examples I could check them out.
If the booru doesn't provide sample/preview sizes, then you'd have to resize it yourself, booru doesn't provide any image-editing code

Then what about videos? I don't think it is possible to embed a video. Or is it possible to detect if the fileURL is a video so it would send a custom message instead?

edit: the bot still returns an image/gif with the blacklisted tag after sending a "post not found" message.

@AtoraSuunva
Copy link
Owner

Videos can't be embedded inside message embeds (but gifs can)

A video link (.mp4/.webm) can be embedded if you post the URL:

image

You'd have to write your own code (or use another library) to detect videos, on my own bot I have an array of embeddable types and just check that

edit: the bot still returns an image/gif with the blacklisted tag after sending a "post not found" message.

You might have to check your bot's logic to make sure you aren't continuing after you post the "post not found" message

@dimactavishy
Copy link
Author

Videos can't be embedded inside message embeds (but gifs can)

You'd have to write your own code (or use another library) to detect videos, on my own bot I have an array of embeddable types and just check that

Thanks for giving me a reference, i've made it so my bot sends a separate non-embed message if the file is over 10MB/a video.

However, it turns out that the embed limit was not actually 10MB and some images over 10MB still got embedded.
I think it's actually around 50 MB (maximum for nitro classic users) since my bot embeds a 36MB file at some point.
Don't know if it will consistently do so though, since it only returned a GIF above 10MB a few times.

BTW, sampleUrl freezes GIFs and videos, but that's from the booru site and not this package.

I also removed the blacklist and just added a if (message.content.includes(['forbidden', 'words']) since it's not like my friends will figure out a way to go around this anyway.

Much thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants