【问题标题】:Python Discord Bot How To Send Multiple Lines In One Message?Python Discord Bot 如何在一条消息中发送多行?
【发布时间】:2020-09-17 13:55:15
【问题描述】:

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

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

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

【问题讨论】:

  • 那么您要做的是获取一条消息,然后在它的前后添加一些内容,对吗?

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


【解决方案1】:

您的机器人发布多条消息的原因是您的 await 在循环中。

你可以完全避免使用循环

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

【讨论】:

    猜你喜欢
    • 2021-01-12
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2021-06-17
    • 2020-08-16
    • 2018-05-03
    • 2020-08-31
    相关资源
    最近更新 更多