![](/img/trans.png)
[英]How can I make my discord bot send messages from a .txt file (python)
[英]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
参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.