在 discord 聊天中,每条消息有 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)
请您参考如下方法:
您可以自己拆分文本或使用简单的方法作为 suggestend 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.
