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])
但是没有成功
你的想法很好,除了有时 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)