繁体   English   中英

如何使用 discord.py 发送图像

[英]How do I send an image using discord.py

我要做的是首先获取用户的消息,将其绘制到图像上,然后发送

一切正常,除了它应该发送图像的部分,那我该怎么做?

from msilib.schema import File
from tkinter import font
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import discord


@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(f'{username}: {user_message}({channel})')

    if username == "ChrisMightShootYou":
        font = ImageFont.truetype(font='D:\DOWNLOAD\DiscordBot\FuturaBookFont.ttf', size=84)
        image = Image.open('D:\DOWNLOAD\DiscordBot\erdemoji.png')
        draw = ImageDraw.Draw(im=image)
        draw.text(xy=(340, 50), text=user_message, font=font, fill='white', anchor='mm',stroke_width=4,stroke_fill='black')
        image.show()
        async def send(message):
            await client.send_message(client.get_channel(), File=discord.File(image))

你可以阅读这篇文章

您必须以rb (读取二进制)模式打开文件。 然后使用 discord.File 发送它。

但是在这个 class 中,我们有一个if

if isinstance(fp, io.IOBase):
    if not (fp.seekable() and fp.readable()):
        raise ValueError(f'File buffer {fp!r} must be seekable and readable')
    self.fp = fp
    self._original_pos = fp.tell()
    self._owner = False
else:
    self.fp = open(fp, 'rb')
    self._original_pos = 0
    self._owner = True

如果您输入文件路径,这将自动完成。

所以你只需要给 discord.File 的文件路径:)

await channel.send(file=discord.File('path\to\photo'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM