【发布时间】:2021-03-05 14:52:41
【问题描述】:
@commands.command()
async def trash(self, ctx, user: discord.Member):
auth = str(os.getenv("FLIPNOTE_API"))
url2 = f'https://api.alexflipnote.dev/trash?face={ctx.author.avatar_url_as(format="png")}&trash={user.avatar_url_as(format="png")}'
headers = {'Authorization': auth}
async with aiohttp.ClientSession() as session:
async with session.get(url2, headers=headers) as res:
link = re.compile(r"(?<=<ClientResponse\()(.*)(?=\))").search(repr(res)).group(1)
link2 = BytesIO(link)
image = await link2.read()
file = discord.File(image, filename="lol.png")
await ctx.send(file=file)
这是我正在使用的代码,错误是 AttributeError: 'str' object has no attribute 'read' 请告诉我如何解决这个问题,我将不胜感激 这是追溯顺便说一句:
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/runner/DayStride/cogs/fun.py", line 1461, in trashmoment
link2 = BytesIO(link)
TypeError: a bytes-like object is required, not 'str'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: a bytes-like object is required, not 'str'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 71, in wrapped
ret = await coro(*args, **kwargs)
File "/home/runner/DayStride/cogs/fun.py", line 1480, in trasherror
await await ctx.send(error)
TypeError: object NoneType can't be used in 'await' expression
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 943, in on_message
await self.process_commands(message)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 940, in process_commands
await self.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 907, in invoke
await ctx.command.dispatch_error(ctx, exc)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 422, in dispatch_error
await injected(cog, ctx, error)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 77, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object NoneType can't be used in 'await' expression
它说 NoneType 不能用于 await 表达式,但我认为主要问题是 str 的东西,但我不确定
【问题讨论】:
-
请发布回溯消息,以便我们在上下文中查看完整消息和失败行。
-
顺便说一句,
re保留了最近使用的正则表达式的缓存。如果你这样做link = re.search(r"(?<=<ClientResponse\()(.*)(?=\))", repr(res)).group(1),它可能比每次都重新编译更有效。 -
我已经添加回溯
-
您的帖子标题显示“我收到一条错误消息,提示 AttributeError: 'str' object has no attribute 'read'”,但我在您发布的回溯中的任何地方都没有看到。
-
好的,告诉我你打算如何让代码工作。你做一些正则表达式的事情并从用户的消息中得到一个字符串,你将它保存为
link。然后你想尝试从那个字符串创建一个BytesIO对象......为什么?然后你想从那个流中读取......这应该以某种方式创建一个image?怎么样?
标签: python discord.py