简体   繁体   English

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

[英]Python Discord Bot How To Send Multiple Lines In One Message?

Hey I am trying to take messages that has multiple lines, add text to the start and end of each line, then resend the lines as one message.嘿,我正在尝试接收具有多行的消息,在每行的开头和结尾添加文本,然后将这些行作为一条消息重新发送。 The main problem is I am having trouble with sending multiple lines in one message.主要问题是我在一条消息中发送多行时遇到问题。 Could someone please help me?有人可以帮我吗? I will include my desired output so you know what i mean.我将包括我想要的输出,所以你知道我的意思。 Thanks so much.非常感谢。

Code I am currently using that sends individual messages我目前使用的发送个人消息的代码

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

Current individual output messages sending:当前发送的单个输出消息:

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..

Desired single message sent:发送的所需单条消息:

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

The reason your bot posts multiple messages is because your await is inside a loop.您的机器人发布多条消息的原因是您的 await 处于循环中。

You can avoid using the loop altogether您可以完全避免使用循环

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

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

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