Python如何拆分消息

时间:2018-10-20 07:26:07

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

在不和谐聊天中,每封邮件最多可包含2000个字符,是否有任何方法可以绕过它?

下面的代码中的

类似示例,当有人键入!ping僵尸程序发送嵌入消息时。因此,有可能使它在某行bot隐藏该消息之后或之前拆分消息,并提供查看或单击下一页或其他内容的选项。

@bot.command(pass_context=True)
async def ping(ctx):
    embed=discord.Embed(title="Something Title", description="something anything goes here")
    await bot.say(embed=embed)

2 个答案:

答案 0 :(得分:0)

您可以自己分割文本,也可以使用简单的方法,如uggestend py @Prashant Godhani here并使用textwrap.wrap() function

# easy way
import textwrap
import lorem

def sayLongLine(text, wrap_at=200):
    for line in textwrap.wrap(text, wrap_at):
        # use await bot.say - maybe add a delay if you have max says/second
        print(line) 

sayLongLine(lorem.paragraph(), 40)

如果您想自己复制textwrap模块的功能,可以通过在空格处将文本拆分为单词并组合单词直到它们超出允许使用的长度来实现。将该单词放在下一个句子中,将所有当前单词重新组合在一起并存储在列表中。循环直到完成,如果需要,添加最后一部分,然后返回列表:

# slightly more complex self-made wrapper:
import lorem
print("----------------------")

def sayLongLineSplitted(text,wrap_at=200):
    """Splits text at spaces and joins it to strings that are as long as 
    possible without overshooting wrap_at.

    Returns a list of strings shorter then wrap_at."""
    splitted = text.split(" ")
    def gimme():
        """Yields sentences of correct lenght."""
        len_parts = 0
        parts = []   
        for p in splitted: 
            len_p = len(p)
            if len_parts + len_p < wrap_at:
                parts.append(p)
                len_parts += len_p + 1  
            else:
                yield ' '.join(parts).strip()
                parts = [p]
                len_parts = len_p 
        if parts:
            yield ' '.join(parts).strip()

    return list(gimme()) 


for part in sayLongLineSplitted(lorem.paragraph(),40):
    print(part)

自制包装的输出:

# 234567890123456789012345678901234567890

Ut velit magnam sed sed. Eius modi
quiquia numquam. Quaerat eius tempora
tempora consectetur etincidunt est. Sit
dolor quaerat quaerat amet voluptatem
dolorem dolore. Sit adipisci non
etincidunt est aliquam etincidunt sit.
Quaerat porro sed sit.

textwrap的输出-示例:

# 234567890123456789012345678901234567890

Etincidunt aliquam etincidunt velit 
numquam. Quisquam porro labore velit. 
Modi modi porro quaerat dolor etincidunt 
quisquam. Ut ipsum quiquia non quisquam 
magnam ut sit. Voluptatem non non 
dolorem. Tempora quaerat neque quaerat 
dolorem velit magnam ipsum. 

答案 1 :(得分:-1)

您可以多次使用await bot.say。

用于行拆分的python textwrap库对于行拆分非常有用。请参考this示例,然后循环使用bot.say。