【发布时间】:2021-06-09 11:43:13
【问题描述】:
我有传入消息,例如 <a:GG:123456789> <:1Copy:12345678><:14:1256678>:eyes:Hello friend!:eyes:,我希望此输出为 [GG] [1Copy][14][eyes]Hello friend![eyes]
下面的代码是我目前拥有的,它的工作原理有点。上面传入的例子输出[GG] [1Copy] [14] [eyes]
def shorten_emojis(content):
seperators = ("<a:", "<:")
output = []
for chunk in content.split():
if any(match in chunk for match in seperators):
parsed_chunk = []
new_chunk = chunk.replace("<", ";<").replace(">", ">;")
for emo in new_chunk.split(";"):
if emo.startswith(seperators):
emo = f"<{splits[1]}>" if len(splits := emo.split(":")) == 3 else emo
parsed_chunk.append(emo)
chunk = "".join(parsed_chunk)
output.append(chunk)
output = " ".join(output)
for e in re.findall(":.+?:", content):
output = output.replace(e, f"<{e.replace(':', '')}>")
return output
测试 #1
输入:<a:GG:123456789> <:1Copy:12345678><:14:1256678>:eyes:Hello friend!:eyes:
输出:[GG] [1Copy] [14] :eyes:Hello friend!:eyes:
想要的[GG] [1Copy][14][eyes]Hello friend![eyes]
测试 #2
输入:<a:cryLaptop:738450655395446814><:1Copy:817543814481707030><:14:817543815401439232> <:thoonk:621279654711656448><:coolbutdepressed:621279653675532290><:KL1Heart:585547199480332318>Nice<:dogwonder:621251869058269185> OK:eyes:
输出:[cryLaptop] [1Copy] [14] [thoonk] [coolbutdepressed] [KL1Heart] Nice [dogwonder] OK:eyes:
想要的[cryLaptop] [GG] [1Copy] [14] [thoonk] [coolbutdepressed] [KL1Heart] Nice [dogwonder] OK[eyes]
编辑
我已经编辑了我的代码块,现在可以正常工作了。
【问题讨论】: