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

123 lines
4 KiB
Python
Raw Normal View History

2021-08-19 12:17:48 +00:00
import re
2022-04-04 06:16:39 +00:00
from typing import Union
from discord import Embed as DiscordEmbed
2021-08-19 12:17:48 +00:00
2022-06-12 07:07:53 +00:00
from core.elements.message.internal import Embed, EmbedField
2021-08-19 12:17:48 +00:00
2022-06-12 07:07:53 +00:00
def removeIneffectiveText(prefix: str, lst: list) -> list:
2021-10-01 01:18:04 +00:00
'''删除命令首尾的空格和换行以及重复命令。
:param prefix: 机器人的命令前缀
:param lst: 字符串List/Union
:returns: 净化后的字符串'''
2021-08-19 12:17:48 +00:00
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])
2021-08-19 12:17:48 +00:00
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
2022-06-12 07:07:53 +00:00
def removeDuplicateSpace(text: str) -> str:
2021-10-01 01:18:04 +00:00
'''删除命令中间多余的空格。
:param text: 字符串
:returns: 净化后的字符串'''
2021-08-19 12:17:48 +00:00
strip_display_space = text.split(' ')
display_list = [] # 清除指令中间多余的空格
for x in strip_display_space:
if x != '':
display_list.append(x)
text = ' '.join(display_list)
2021-08-23 12:44:31 +00:00
return text
2022-04-04 06:16:39 +00:00
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"]