【发布时间】:2020-11-30 01:36:58
【问题描述】:
我正在尝试在 pycharm 中使用 python 3.8 和 discord.py 制作一个不和谐的机器人。该机器人的功能是读取文本文件并将每个单词写成单独的消息并将其发送到不和谐服务器。现在我有了文本文件,它可以打印出单词,但不能单独打印。
下面是相关代码:
f = open("test.py")
@client.event
async def on_message(message):
if message.content.startswith('r!help'):
channel = message.channel
await channel.send('Help')
elif message.content.startswith('r!start'):
channel = message.channel
await channel.send('Starting Story...')
await channel.send(f.readlines())
我阅读了一些其他答案,他们说f.readlines() 可以解决问题,但它只发送一行文本。我试过了
def script_message():
with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
然后尝试使用await channel.send(script_message()) 调用该函数,但这给我留下了要求我更正语法的错误。那么如何将文本文件的内容作为单独的消息发送呢?
【问题讨论】:
-
for line in f: await channel.send(line)显然是这方面的尝试。你试过了吗?
标签: python discord.py discord.py-rewrite