我将如何保存 discord.py 收到的文件

How would I save a file received by discord.py

我正在开发我的 discord bot,试图实现一个电子邮件功能,您可以在其中嵌入一个文件,然后 discord bot 下载它并将其发送回服务器。我遇到了一个问题,我不知道如何开始保存文件。您可以在 https://github.com/Omar-Alabdalla/DiscordBot 找到我的代码。具有电子邮件功能的特定文件是 mailFunctions(discord 命令部分)和 basicMail(电子邮件命令部分)。

我查看了 nextcord.py 的文档,但找不到任何我能理解的简单方法。我可能只是错过了我应该找到的东西。

discord 命令代码:

@commands.command()
    async def mailFile(self, ctx, *stuff):
        # received if else statement from Whosebug: 
        if str(ctx.attachments) == "[]":  # This checks if there is an attachment on the message
            return "You didn't include a file"
        else:
            await save("mailFile

邮寄class编码:

def sendFileMail(rmail, message):
    mail_content = '''Hello,
    This is a test mail.
    In this mail we are sending some attachments.
    The mail is sent using Python SMTP library.
    Thank You
    '''
    # Setup the MIME
    message = MIMEMultipart()
    message['From'] = sender_email
    message['To'] = rmail
    message['Subject'] = 'A test mail sent by Python. It has an attachment.'

    # The subject line
    # The body and the attachments for the mail
    message.attach(MIMEText(mail_content, 'plain'))
    attach_file_name = 'TP_python_prev.pdf'
    attach_file = open(attach_file_name, 'rb')  # Open the file as binary mode
    payload = MIMEBase('application', 'octate-stream')
    payload.set_payload(attach_file.read())
    encoders.encode_base64(payload)  # encode the attachment

    # add payload header with filename
    payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
    message.attach(payload)

    # Create SMTP session for sending the mail
    session = smtplib.SMTP('smtp.gmail.com', 587)  # use gmail with port
    session.starttls()  # enable security
    session.login(sender_email, password)  # login with mail_id and password
    text = message.as_string()
    session.sendmail(sender_email, rmail, text)
    session.quit()
    print('Mail Sent')

对于第一次在堆栈溢出时发布之前没有包含代码表示歉意

参见Attachment.savehttps://nextcord.readthedocs.io/en/latest/api.html?highlight=attachment#nextcord.Attachment.save

for attachment in ctx.message.attachments:
    await attachment.save(attachment.filename)