繁体   English   中英

如何在 Discord Bot 上使用 Python 发送 a.txt 文件?

[英]How to send a .txt file using Python on a Discord Bot?

对不起,我是一个初学者。 因此,我更需要你以正确的方向面对我,而不是为我做这件事。

我有一个 discord 机器人,它可以在 JS 中运行,但是在尝试从 Raspberry Pi 24/7 运行它时,只需将其转换为 Python 就更容易了。 原始的 JS 代码是功能性的,可以响应!movies!TV并以.txt文件的形式列出我下载的要观看的内容。 我有一系列.bat文件,它们定期编译列表并将其 scp/ssh 到我的 Pi。 所以.txt文件位于我的 Pi 上。 我可以得到 Python 版本来响应带有文本回复的!movies但无法确定发送本地.txt文件。 我能找到的任何指南都基于代码创建的.txt文件。 我需要帮助的只是让发送 function 发送.txt文件而不是短信,这是我的代码:

import discord

from discord.ext 

import commands

bot = commands.Bot(command_prefix='!')


@bot.command(pass_context=True)

async def movies(ctx):
    
await ctx.send("Here ya go:")
    
bot.run('***MYTOKEN***')

如前所述,此代码功能齐全,我必须学习哪些功能才能发送.txt文件,提前谢谢。

discord.py 提供对文件的直接支持

with open('my_file.txt', 'r') as fp:
    await ctx.send(file=discord.File(fp, 'new_filename.png'))

或者,如果您想发送文件的内容

with open('my_file.txt', 'r') as fp:
   await ctx.send(fp.read()) # wont work if number of characters is greater than 3000

参考

  1. 上传图片

暂无
暂无

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

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