使用 discord.py 读取文本文件并拆分为 python3.8 中的单独单词

Reading a text file and splitting into separate words in python3.8, using discord.py

我正在尝试使用 python 3.8 和 discord.py 在 pycharm 中制作一个不和谐的机器人。机器人的功能是读取文本文件并将每个单词作为单独的消息写出,并将其发送到不和谐服务器。现在我有了文本文件,它可以打印出单词,但它不能单独打印。

相关代码如下:

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()) 调用该函数,但这给我留下了错误,要求我更正语法。那么如何将文本文件的内容作为单独的消息发送呢?

async def script_message(channel):
    with open("words.txt") as f:
        data = f.readlines()
    for i in data:
        for j in i.split(" "):
            await channel.send(j)

并且在您的 on_message 活动中

elif message.content.startswith('r!start'):
    await message.channel.send('Starting Story...')
    await script_message(message.channel)