我是python的初学者,但我正在尝试创建一个不和谐的bot,当成员加入服务器时该bot发送欢迎消息。我想在聊天中发送使用PILLOW修改的图像。我做到了:
if message.content == ".welcome":
member = message.author
url = member.avatar_url
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib2.Request(url, headers=hdr)
result = urllib2.urlopen(req)
file = result.read()
img = Image.open(io.BytesIO(file))
name = str(member.display_name)
# await bot.send_message(testbot, member.avatar_url)
await bot.send_file(testbot, img)
但是我有这个错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/client_reqrep.py", line 407, in write_bytes
result = stream.send(value)
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/helpers.py", line 191, in _gen_form_data
yield from self._writer.serialize()
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/multipart.py", line 969, in serialize
yield from part.serialize()
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/multipart.py", line 768, in serialize
yield from self._maybe_encode_stream(self._serialize_obj())
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/multipart.py", line 781, in _serialize_obj
return self._serialize_default(obj)
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/multipart.py", line 813, in _serialize_default
raise TypeError('unknown body part type %r' % type(obj))
TypeError: unknown body part type <class 'PIL.WebPImagePlugin.WebPImageFile'>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.5/site-packages/discord/client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "/home/pi/Desktop/bot.py", line 88, in on_message
await bot.send_file(testbot, img)
File "/home/pi/.local/lib/python3.5/site-packages/discord/client.py", line 1235, in send_file
filename=filename, content=content, tts=tts)
File "/home/pi/.local/lib/python3.5/site-packages/discord/http.py", line 137, in request
r = yield from self.session.request(method, url, **kwargs)
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/client.py", line 555, in __iter__
resp = yield from self._coro
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/client.py", line 202, in _request
yield from resp.start(conn, read_until_eof)
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/client_reqrep.py", line 640, in start
message = yield from httpstream.read()
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/streams.py", line 641, in read
result = yield from super().read()
File "/home/pi/.local/lib/python3.5/site-packages/aiohttp/streams.py", line 476, in read
yield from self._waiter
File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
raise self._exception
aiohttp.errors.ClientRequestError: Can not write request body for https://discordapp.com/api/v6/channels/445990287152644096/messages
如果有人知道我为什么出现此错误...
答案 0 :(得分:0)
首先,您应该不在处理asyncio和协程时使用urllib或请求来发出http请求,因为它们不是异步安全的(它们的请求品牌屏蔽)。相反,在使用discord.py时,应使用异步请求库(即aiohttp)发出请求。
对于您的错误,send_file
使用一个类似文件的对象,而不是PIL图像,因此您需要先将该图像保存到文件或BytesIO
。
from re import search
import aiohttp
@bot.event
async def on_message(m):
member = m.author
url = member.avatar_url
file_name = search("/(\w+\.\w+)(?:\?.*?)?$", url)
if file_name is None:
file_name = "avatar.jpg" # If there's a problem with my regex
else:
file_name = file_name.group(1)
hdr = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
async with aiohttp.ClientSession() as client:
async with client.get(url, headers=hdr) as req:
bytes_image = await req.read()
with io.BytesIO(bytes_image) as f:
f.name = file_name
img = Image.open(f)
#######
# do stuff with `img`
#######
with io.BytesIO() as f:
f.name = file_name
img.save(f)
f.seek(0)
await bot.send_file(m.channel, f)