簡體   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