Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions botpy/ext/command_util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import re
from functools import wraps
from botpy.message import BaseMessage

from typing import Tuple

class Commands:
"""
Expand All @@ -13,18 +13,19 @@ class Commands:

def __init__(self, *args):
self.commands = args
# 构建正则表达式模式
self.pattern = re.compile(rf"({'|'.join(self.commands)})(?:\s+(.*))?")

def __call__(self, func):
@wraps(func)
async def decorated(*args, **kwargs):
message: BaseMessage = kwargs["message"]
for command in self.commands:
if command in message.content:
# 分割指令后面的指令参数
params = message.content.split(command)[1].strip()
kwargs["params"] = params
return await func(*args, **kwargs)
match = self.pattern.match(message.content)
if match:
command, params = match.groups()
if params:
kwargs["params"] = params.strip()
return await func(*args, **kwargs)
return False

return decorated