Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/utils/message.py

122 lines
4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from typing import Union
from discord import Embed as DiscordEmbed
from core.elements.message.internal import Embed, EmbedField
def removeIneffectiveText(prefix: str, lst: list) -> list:
'''删除命令首尾的空格和换行以及重复命令。
:param prefix: 机器人的命令前缀。
:param lst: 字符串List/Union
:returns: 净化后的字符串。'''
remove_list = ['\n', ' '] # 首尾需要移除的东西
for x in remove_list:
list_cache = []
for y in lst:
split_list = y.split(x)
for _ in split_list:
if split_list[0] == '':
del split_list[0]
if len(split_list) > 0:
if split_list[-1] == '':
del split_list[-1]
for _ in split_list:
if len(split_list) > 0:
spl0 = split_list[0]
if spl0.startswith(prefix) and spl0 != '':
split_list[0] = re.sub(r'^' + prefix, '', split_list[0])
list_cache.append(x.join(split_list))
lst = list_cache
duplicated_list = [] # 移除重复命令
for x in lst:
if x not in duplicated_list:
duplicated_list.append(x)
lst = duplicated_list
return lst
def removeDuplicateSpace(text: str) -> str:
'''删除命令中间多余的空格。
:param text: 字符串。
:returns: 净化后的字符串。'''
strip_display_space = text.split(' ')
display_list = [] # 清除指令中间多余的空格
for x in strip_display_space:
if x != '':
display_list.append(x)
text = ' '.join(display_list)
return text
def convertDiscordEmbed(embed: Union[DiscordEmbed, dict]) -> Embed:
'''将DiscordEmbed转换为Embed。
:param embed: DiscordEmbed。
:returns: Embed。'''
embed_ = Embed()
if isinstance(embed, DiscordEmbed):
embed = embed.to_dict()
if isinstance(embed, dict):
if 'title' in embed:
embed_.title = embed['title']
if 'description' in embed:
embed_.description = embed['description']
if 'url' in embed:
embed_.url = embed['url']
if 'color' in embed:
embed_.color = embed['color']
if 'timestamp' in embed:
embed_.timestamp = embed['timestamp']
if 'footer' in embed:
embed_.footer = embed['footer']['text']
if 'image' in embed:
embed_.image = embed['image']
if 'thumbnail' in embed:
embed_.thumbnail = embed['thumbnail']
if 'author' in embed:
embed_.author = embed['author']
if 'fields' in embed:
fields = []
for field_value in embed['fields']:
fields.append(EmbedField(field_value['name'], field_value['value'], field_value['inline']))
embed_.fields = fields
return embed_
def split_multi_arguments(lst: list):
new_lst = []
for x in lst:
spl = list(filter(None, re.split(r"(\(.*?\))", x)))
if len(spl) > 1:
for y in spl:
index_y = spl.index(y)
mat = re.match(r"\((.*?)\)", y)
if mat:
spl1 = mat.group(1).split('|')
for s in spl1:
cspl = spl.copy()
cspl.insert(index_y, s)
del cspl[index_y + 1]
new_lst.append(''.join(cspl))
else:
mat = re.match(r"\((.*?)\)", spl[0])
if mat:
spl1 = mat.group(1).split('|')
for s in spl1:
new_lst.append(s)
else:
new_lst.append(spl[0])
split_more = False
for n in new_lst:
if re.match(r"\((.*?)\)", n):
split_more = True
if split_more:
return split_multi_arguments(new_lst)
else:
return list(set(new_lst))
__all__ = ['removeDuplicateSpace', 'removeIneffectiveText', 'convertDiscordEmbed', "split_multi_arguments"]