jokelbaf / hhaven

A well-documented and typed API wrapper for Hentai Haven.
https://hhaven.jokelbaf.tech
MIT License
7 stars 1 forks source link

so im trying to create discord bot from the api you posted but it doesnt seem to be working #9

Closed shjee-afridi closed 1 year ago

shjee-afridi commented 1 year ago
          so im trying to create discord bot from the api you posted but it doesnt seem to be working
import discord
from discord.ext import commands
from hhaven import Client
import m3u8_To_MP4

intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot = commands.Bot(command_prefix='!', intents=intents)

client = Client()

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

@bot.command()
async def newest(ctx):
    try:
        await ctx.send("Fetching newest hentai video...")

        results = await client.search("hentai", sort_by="created_at", order="desc", limit=1)
        hentai = await results[0].full()
        episode = await hentai.episodes[0].full()

        m3u8_To_MP4.multithread_download(episode.content)

        await ctx.send(f"Downloaded the newest video: {hentai.title} - {episode.name}")
    except Exception as e:
        traceback.print_exc()
        await ctx.send(f"An error occurred: {e}")

bot.run('')

Originally posted by @BlackReaper33 in https://github.com/JokelBaf/hhaven/issues/8#issuecomment-1678710773

jokelbaf commented 1 year ago

I have so many questions to your code...

First of all, from docs:

In order to properly initialize client, build() function must be called.

Just doing client = Client() won't do the job. You need to call build() function otherwise you won't be able to use client.

If you want your bot to receive prefixed commands like !newest, you need to enable Message content intent in Discord developers portal > Your application > Bot. Then add intents.message_content = True in your code since it is not a default one.

client.search method does not have sort_by, order and limit arguments. Where did you find them?? They are simply not present in HH's API. However, I can add these to the library, if you really need it.

In order to get latest episode, better use episode = await home.last_episodes[0].full().

Make sure you installed m3u8_To_MP4 and all it's dependencies properly (e.g. ffmpeg).

I really do not recommend to use m3u8_To_MP4's multithread_download with async libraries like discord.py because it is a blocking function. Your bot will probably throw tons of errors because of code blockage.

After all these changes, your code should look like this:

from discord.ext import commands
import discord, m3u8_To_MP4,
from hhaven import Client

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

@bot.command()
async def newest(ctx):
    try:
        await ctx.send("Fetching newest hentai video...")

        client = await Client().build()
        home = await client.home()
        episode = await home.last_episodes[0].full()

        m3u8_To_MP4.multithread_download(episode.content)

        await ctx.send(f"Downloaded the newest video: {episode.hentai_title} - {episode.name}")
    except Exception as e:
        await ctx.send(f"An error occurred: {e}")

bot.run('')
jokelbaf commented 1 year ago

Since no more questions or comments from you on this issue, I assume the problem is solved. Closing as completed.