Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update command_util.py #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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