当我尝试解码 json 命令“imagine”引发异常时,出现此错误:TypeError:预期类似字节的对象,而不是 str。 有人可以帮我找到解决这个问题的方法吗?我查遍了互联网,但没有找到任何可以解决这个问题的方法。
import discord
from discord import app_commands
from discord.ext import commands
import aiohttp
import base64
import time
import json
from PIL import Image
from io import BytesIO
@bot.tree.command(name = "imagine", description = "Generate image")
@app_commands.describe(prompt = "Write here how the image should look like (generating people isn't good)")
async def generate(interaction = discord.Interaction, *, prompt: str):
ETA = int(time.time()+ 60)
msg = await interaction.response.send_message(f'Go grab a coffee, this may take some time... ETA: <t:{ETA}:R>')
async with aiohttp.request("POST", "https://backend.craiyon.com/generate", json={"prompt": prompt}) as resp:
r = await resp.json(content_type = None)
print(r)
images = r['images']
image = BytesIO(base64.decodebytes(images[0]).encode("utf-8"))
print(image)
return await interaction.response.send_message(content = "Content Generated by **craiyon.com**", files = discord.File(image, "generatedImage.png"), view = DropdownView(msg, images, interaction.user.id))
bot.run(TOKEN)```
This is the code, the input is a slash command that has a str parameter called prompt. The output should be a new message in the channel containing the first of the 9 pictures (craiyon generates 9 pictures). And before anyone trys to warn me about the token is in this file and not in a .env file. I know, but this is the test version of. my already existing bot and these are 2 different bots