mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-02-22 02:25:56 +08:00
Check nickname before parsing command, allowing users call commands with nickname instead of explicit "at"
This commit is contained in:
parent
fdc7327a0c
commit
838213d438
@ -29,36 +29,10 @@ async def handle_message(bot: NoneBot, ctx: Context_T) -> None:
|
|||||||
if coros:
|
if coros:
|
||||||
await asyncio.wait(coros)
|
await asyncio.wait(coros)
|
||||||
|
|
||||||
if 'to_me' not in ctx:
|
raw_to_me = ctx.get('to_me', False)
|
||||||
if ctx['message_type'] == 'private':
|
_check_at_me(bot, ctx)
|
||||||
ctx['to_me'] = True
|
_check_calling_me_nickname(bot, ctx)
|
||||||
else:
|
ctx['to_me'] = raw_to_me or ctx['to_me']
|
||||||
# group or discuss
|
|
||||||
ctx['to_me'] = False
|
|
||||||
at_me_seg = MessageSegment.at(ctx['self_id'])
|
|
||||||
|
|
||||||
# check the first segment
|
|
||||||
first_msg_seg = ctx['message'][0]
|
|
||||||
if first_msg_seg == at_me_seg:
|
|
||||||
ctx['to_me'] = True
|
|
||||||
del ctx['message'][0]
|
|
||||||
|
|
||||||
if not ctx['to_me']:
|
|
||||||
# check the last segment
|
|
||||||
i = -1
|
|
||||||
last_msg_seg = ctx['message'][i]
|
|
||||||
if last_msg_seg.type == 'text' and \
|
|
||||||
not last_msg_seg.data['text'].strip() and \
|
|
||||||
len(ctx['message']) >= 2:
|
|
||||||
i -= 1
|
|
||||||
last_msg_seg = ctx['message'][i]
|
|
||||||
|
|
||||||
if last_msg_seg == at_me_seg:
|
|
||||||
ctx['to_me'] = True
|
|
||||||
del ctx['message'][i:]
|
|
||||||
|
|
||||||
if not ctx['message']:
|
|
||||||
ctx['message'].append(MessageSegment.text(''))
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -79,6 +53,62 @@ async def handle_message(bot: NoneBot, ctx: Context_T) -> None:
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def _check_at_me(bot: NoneBot, ctx: Context_T) -> None:
|
||||||
|
if ctx['message_type'] == 'private':
|
||||||
|
ctx['to_me'] = True
|
||||||
|
else:
|
||||||
|
# group or discuss
|
||||||
|
ctx['to_me'] = False
|
||||||
|
at_me_seg = MessageSegment.at(ctx['self_id'])
|
||||||
|
|
||||||
|
# check the first segment
|
||||||
|
first_msg_seg = ctx['message'][0]
|
||||||
|
if first_msg_seg == at_me_seg:
|
||||||
|
ctx['to_me'] = True
|
||||||
|
del ctx['message'][0]
|
||||||
|
|
||||||
|
if not ctx['to_me']:
|
||||||
|
# check the last segment
|
||||||
|
i = -1
|
||||||
|
last_msg_seg = ctx['message'][i]
|
||||||
|
if last_msg_seg.type == 'text' and \
|
||||||
|
not last_msg_seg.data['text'].strip() and \
|
||||||
|
len(ctx['message']) >= 2:
|
||||||
|
i -= 1
|
||||||
|
last_msg_seg = ctx['message'][i]
|
||||||
|
|
||||||
|
if last_msg_seg == at_me_seg:
|
||||||
|
ctx['to_me'] = True
|
||||||
|
del ctx['message'][i:]
|
||||||
|
|
||||||
|
if not ctx['message']:
|
||||||
|
ctx['message'].append(MessageSegment.text(''))
|
||||||
|
|
||||||
|
|
||||||
|
def _check_calling_me_nickname(bot: NoneBot, ctx: Context_T) -> None:
|
||||||
|
first_msg_seg = ctx['message'][0]
|
||||||
|
if first_msg_seg.type != 'text':
|
||||||
|
return
|
||||||
|
|
||||||
|
first_text = first_msg_seg.data['text']
|
||||||
|
|
||||||
|
if bot.config.NICKNAME:
|
||||||
|
# check if the user is calling me with my nickname
|
||||||
|
if isinstance(bot.config.NICKNAME, str) or \
|
||||||
|
not isinstance(bot.config.NICKNAME, Iterable):
|
||||||
|
nicknames = (bot.config.NICKNAME,)
|
||||||
|
else:
|
||||||
|
nicknames = filter(lambda n: n, bot.config.NICKNAME)
|
||||||
|
nickname_regex = '|'.join(nicknames)
|
||||||
|
m = re.search(rf'^({nickname_regex})([\s,,]*|$)',
|
||||||
|
first_text, re.IGNORECASE)
|
||||||
|
if m:
|
||||||
|
nickname = m.group(1)
|
||||||
|
logger.debug(f'User is calling me {nickname}')
|
||||||
|
ctx['to_me'] = True
|
||||||
|
first_msg_seg.data['text'] = first_text[m.end():]
|
||||||
|
|
||||||
|
|
||||||
def _log_message(ctx: Context_T) -> None:
|
def _log_message(ctx: Context_T) -> None:
|
||||||
msg_from = str(ctx['user_id'])
|
msg_from = str(ctx['user_id'])
|
||||||
if ctx['message_type'] == 'group':
|
if ctx['message_type'] == 'group':
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import re
|
|
||||||
from typing import Iterable, Optional, Callable, Union, NamedTuple
|
from typing import Iterable, Optional, Callable, Union, NamedTuple
|
||||||
|
|
||||||
from . import NoneBot, permission as perm
|
from . import NoneBot, permission as perm
|
||||||
@ -106,23 +105,7 @@ async def handle_natural_language(bot: NoneBot, ctx: Context_T) -> bool:
|
|||||||
:param ctx: message context
|
:param ctx: message context
|
||||||
:return: the message is handled as natural language
|
:return: the message is handled as natural language
|
||||||
"""
|
"""
|
||||||
msg = str(ctx['message'])
|
session = NLPSession(bot, ctx, str(ctx['message']))
|
||||||
if bot.config.NICKNAME:
|
|
||||||
# check if the user is calling me with my nickname
|
|
||||||
if isinstance(bot.config.NICKNAME, str) or \
|
|
||||||
not isinstance(bot.config.NICKNAME, Iterable):
|
|
||||||
nicknames = (bot.config.NICKNAME,)
|
|
||||||
else:
|
|
||||||
nicknames = filter(lambda n: n, bot.config.NICKNAME)
|
|
||||||
nickname_regex = '|'.join(nicknames)
|
|
||||||
m = re.search(rf'^({nickname_regex})([\s,,]*|$)', msg, re.IGNORECASE)
|
|
||||||
if m:
|
|
||||||
nickname = m.group(1)
|
|
||||||
logger.debug(f'User is calling me {nickname}')
|
|
||||||
ctx['to_me'] = True
|
|
||||||
msg = msg[m.end():]
|
|
||||||
|
|
||||||
session = NLPSession(bot, ctx, msg)
|
|
||||||
|
|
||||||
# use msg_text here because CQ code "share" may be very long,
|
# use msg_text here because CQ code "share" may be very long,
|
||||||
# at the same time some plugins may want to handle it
|
# at the same time some plugins may want to handle it
|
||||||
|
Loading…
x
Reference in New Issue
Block a user