Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Discord Bot如何在一条消息中发送多行?_Python_Python 3.x_Discord.py - Fatal编程技术网

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

Python Discord Bot如何在一条消息中发送多行?,python,python-3.x,discord.py,Python,Python 3.x,Discord.py,嘿,我正在尝试获取有多行的消息,在每行的开头和结尾添加文本,然后将这些行作为一条消息重新发送。主要问题是我在一条消息中发送多行时遇到问题。谁能帮帮我吗?我会包括我想要的输出,所以你知道我的意思。非常感谢 我当前正在使用的发送单个消息的代码 import discord class MyClient(discord.Client): async def on_ready(self): print('Logged on as', self.user) async

嘿,我正在尝试获取有多行的消息,在每行的开头和结尾添加文本,然后将这些行作为一条消息重新发送。主要问题是我在一条消息中发送多行时遇到问题。谁能帮帮我吗?我会包括我想要的输出,所以你知道我的意思。非常感谢

我当前正在使用的发送单个消息的代码

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

你的机器人发布多条消息的原因是因为你的等待在一个循环中

可以完全避免使用循环

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

你的机器人发布多条消息的原因是因为你的等待在一个循环中

可以完全避免使用循环

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

所以你要做的就是把一条信息,然后在它的前面和后面添加一些东西,对吗?那么你要做的就是把一条信息,然后在它的前面和后面添加一些东西,对吗?