【问题标题】:Python how to split messagesPython如何拆分消息
【发布时间】:2018-10-20 07:26:06
【问题描述】:

在不和谐聊天中,每条消息限制为 2000 个字符,那么有什么办法可以绕过它?

当有人键入!ping 机器人发送嵌入消息时,如下代码中的示例。那么是否可以在某个线路机器人隐藏该消息并提供查看或单击下一页或其他内容的选项之后或之前拆分消息。

@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)

【问题讨论】:

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


    【解决方案1】:

    您可以自己拆分文本或使用简单的方法,如建议 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-example 的输出:

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

    【讨论】:

    • 所以只要把def sayLongLineSplitted这个函数添加到bot。我需要在我的机器人命令@bot.command(pass_context=True) async def ping(ctx): 中添加任何内容吗?
    • @Demotry 拆分功能 sayLongLineSplitted() 将您的文本拆分为长度合适的部分。您需要按顺序为该列表的每个部分调用机器人一次。
    猜你喜欢
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多