Python Discord Bot如何在一条消息中发送多行?

时间:2020-09-17 13:55:15

标签: python python-3.x discord.py

嘿,我正在尝试接收多行消息,将文本添加到每行的开头和结尾,然后将这些行作为一条消息重新发送。主要问题是我在一条消息中发送多行时遇到麻烦。有人可以帮我吗?我将包含所需的输出,以便您了解我的意思。非常感谢。

我当前使用的发送个人消息的代码

"${!arrayname[@]}"

当前发送的单个输出消息:

import discord

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as', self.user)

    async def on_message(self, message):
        if message.author == self.user:
            return

        channel = client.get_channel(754584125108912150) #change to your channel id
        if message.channel.id == channel.id:
            if "placeholder first line" in message.content.lower():
                messagee = message.content.split('\n')
                for line in messagee:
                    testt = "test_start "+line+" test_end"
                    await channel.send(testt)

client = MyClient()
client.run(TOKENHERE) #bot token here

已发送所需的单个消息:

BOT_username Today at 9:41 AM
test_start apple first line test_end

BOT_username Today at 9:41 AM
test_start week test_end

BOT_username Today at 9:41 AM
test_start food test_end

BOT_username Today at 9:41 AM
test_start fork test_end

etc..

1 个答案:

答案 0 :(得分:0)

您的漫游器发布多条消息的原因是因为您的等待处于循环中。

您可以避免完全使用循环

messagee = message.content.split('\n')
output_text = '\n'.join(('test_start' + line + 'test_end') for line in messagee)
await channel.send(output_text)