我为discord.py编写了这个图像搜索命令,但我不知道如何让它看起来不错

问题描述 投票:0回答:1

this is what i want it to look like

this is what it looks like

It also sends this in the start of every search for some reason

from google_images_download import google_images_download


@client.command()
async def image(ctx, query):
    response = requests.get(f"https://www.google.com/search?tbm=isch&q={query}")
    soup = BeautifulSoup(response.text, 'html.parser')
    image_elements = soup.find_all('img')
    url_list = [image['src'] for image in image_elements]
    current_index = 0
    message = await ctx.send(url_list[current_index])
    await message.add_reaction('◀️')
    await message.add_reaction('▶️')
    while True:
        reaction, user = await client.wait_for('reaction_add', check=lambda reaction, user: user == ctx.author and reaction.message.id == message.id)
        if str(reaction.emoji) == '▶️':
            current_index = (current_index + 1) % len(url_list)
            await message.edit(content=url_list[current_index])
        elif str(reaction.emoji) == '◀️':
            current_index = (current_index - 1) % len(url_list)
            await message.edit(content=url_list[current_index])
        await message.remove_reaction(reaction, user)

我尝试改变

    message = await ctx.send(url_list[current_index])

进入这个

    embed = discord.Embed(title="Image Viewer", color=0x00ff00)
    embed.set_image(url=url_list[current_index])

但是没有成功

image discord discord.py bots
1个回答
0
投票

你的想法很好,除了有时 url_list[current_index] 返回一个 str。

我做到了,所以第一张图片将是嵌入的,剩下的就交给你了。使用相同的逻辑,你就不会有问题。

代码:

@client.command()
async def image(ctx, query):
    response = requests.get(f"https://www.google.com/search?tbm=isch&q={query}")
    soup = BeautifulSoup(response.text, 'html.parser')
    image_elements = soup.find_all('img')
    url_list = [image['src'] for image in image_elements]
    current_index = 0
    while True:
        try:
            embed = discord.Embed(title="Image",description=query)
            embed.set_image(url = url_list[current_index])
            message = await ctx.send(embed=embed)
            break
        except:
            current_index += 1
    await message.add_reaction('◀️')
    await message.add_reaction('▶️')
    while True:
        reaction, user = await client.wait_for('reaction_add', check=lambda reaction, user: user == ctx.author and reaction.message.id == message.id)
        if str(reaction.emoji) == '▶️':
            current_index = (current_index + 1) % len(url_list)
            await message.edit(content=url_list[current_index])
        elif str(reaction.emoji) == '◀️':
            current_index = (current_index - 1) % len(url_list)
            await message.edit(content=url_list[current_index])
        await message.remove_reaction(reaction, user)
© www.soinside.com 2019 - 2024. All rights reserved.