简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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