Skip to content

Commit

Permalink
feat: 添加子频道转跳相关工具 (#89)
Browse files Browse the repository at this point in the history
* Add files via upload

* Update channel_jump_util.py

* Update channel_jump_util.py

* Update channel_jump_util.py

* Update channel_jump_util.py
  • Loading branch information
single-ptilopsis committed Jun 8, 2022
1 parent 60fdd6d commit 3f03aae
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions botpy/ext/channel_jump_util.py
@@ -0,0 +1,71 @@
"""
对子频道转跳进行操作(#name)
注意:
1、发送格式要求严格(#name ),自动添加的空格不能删除
2、无法识别真假转跳
3、当子频道重名时无法准确识别
4、当提供子频道转跳字段时请弃用本模块
"""

__all__ = [
"get_channel_jump",
"get_channel_jump_strict",
"escape_channel_jump"
]

import re
from typing import List, Dict

from botpy import BotAPI
from botpy.message import Message

CHANNEL_JUMP_RE = re.compile(r"#(.{1,12}?)(?= )")


def get_channel_jump(text: str = None, message: Message = None) -> List[str]:
"""
识别文本中的子频道转跳(粗略)
:param message: 消息对象
:param text: 文本,为空则message.content
:return: 子频道名称列表(不带#)
"""
return CHANNEL_JUMP_RE.findall(message.content if text is None else text)


async def get_channel_jump_strict(api: BotAPI, message: Message = None, text: str = None,
guild_id: str = None) -> Dict[str, str]:
"""
识别文本中的子频道转跳(准确)
:param api: BotAPI
:param message: 消息对象
:param text: 文本,为空则message.content
:param guild_id: 频道id,为空则message.guild_id
:return: {子频道名称(不带#):子频道id} (去重)
"""
channels = await api.get_channels(guild_id or message.guild_id)
text = message.content if text is None else text
jumps = {}

for channel in channels:
if "#%s " % channel["name"] in text:
jumps[channel["name"]] = channel["id"]

return jumps


async def escape_channel_jump(api: BotAPI, message: Message = None, text: str = None, guild_id: str = None) -> str:
"""
转义子频道转跳 (#name -> <#id>)
:param api: BotAPI
:param message: 消息对象
:param text: 文本,为空则message.content
:param guild_id: 频道id,为空则message.guild_id
:return: 转义后的文本
"""
channels = await api.get_channels(guild_id or message.guild_id)
text = message.content if text is None else text

for channel in channels:
text = text.replace("#%s " % channel["name"], "<#%s> " % channel["id"])

return text

0 comments on commit 3f03aae

Please sign in to comment.