簡體   English   中英

Python Discord Bot 如何在一條消息中發送多行?

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

嘿,我正在嘗試接收具有多行的消息,在每行的開頭和結尾添加文本,然后將這些行作為一條消息重新發送。 主要問題是我在一條消息中發送多行時遇到問題。 有人可以幫我嗎? 我將包括我想要的輸出,所以你知道我的意思。 非常感謝。

我目前使用的發送個人消息的代碼

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

發送的所需單條消息:

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

您的機器人發布多條消息的原因是您的 await 處於循環中。

您可以完全避免使用循環

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