Skip to content

Commit

Permalink
fix math func split bug
Browse files Browse the repository at this point in the history
  • Loading branch information
DoroWolf authored Mar 25, 2024
1 parent 4545a02 commit 9171375
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions modules/dice/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def process_expression(msg, expr: str, dc, use_markdown = False):
use_markdown = True
if use_markdown:
expr = expr.replace('*', '\*')
expr = expr.replace('\\*', '\*')
expr = expr.replace(r'\\*', '\*')

dice_list, count, times, err = parse_dice_expression(msg, expr)
if err:
Expand All @@ -57,6 +57,7 @@ async def process_expression(msg, expr: str, dc, use_markdown = False):

def parse_dice_expression(msg, dices):
dice_item_list = []
math_func_pattern = '(' + '|'.join(re.escape(func) for func in math_funcs.keys()) + ')' # 数学函数
patterns = [
r'((?:B|P)(?:\d+)?)', # 奖惩骰子
r'((?:\d+)?D?F)', # 命运骰子
Expand All @@ -75,12 +76,12 @@ def parse_dice_expression(msg, dices):
if not times.isdigit():
return None, None, None, DiceValueError(msg, msg.locale.t('dice.message.N.invalid'), times).message

dice_expr_list = re.split('|'.join(patterns), dices, flags=re.I)
dice_expr_list = re.split(f'{math_func_pattern}|' + '|'.join(patterns), dices, flags=re.I)
dice_expr_list = [item for item in dice_expr_list if item] # 清除空白元素
for item in range(len(dice_expr_list)):
if dice_expr_list[item][-1].upper() == 'D' and msg.data.options.get('dice_default_face'):
if dice_expr_list[item][-1].upper() == 'D' and dice_expr_list[item] not in math_funcs.keys()\
and msg.data.options.get('dice_default_face'):
dice_expr_list[item] += str(msg.data.options.get('dice_default_face'))
Logger.debug(dice_expr_list)

for i, item in enumerate(dice_expr_list):
for pattern in patterns:
Expand All @@ -89,14 +90,21 @@ def parse_dice_expression(msg, dices):
dice_expr_list[i] = item.upper()
dice_item_list.append(item)
break
func_match = re.match(math_func_pattern, item, flags=re.I)
if func_match:
dice_expr_list[i] = item.lower()

Logger.debug(dice_expr_list)
if len(dice_item_list) > MAX_ITEM_COUNT:
return None, None, None, DiceValueError(msg, msg.locale.t('dice.message.error.value.too_long')).message

dice_count = 0
# 初始化骰子序列
for j, item in enumerate(dice_expr_list):
try:
if 'B' in item or 'P' in item:
if any(item.lower() == func for func in math_funcs.keys()):
continue
elif 'B' in item or 'P' in item:
dice_count += 1
dice_expr_list[j] = BonusPunishDice(msg, item)
elif 'F' in item:
Expand All @@ -107,8 +115,6 @@ def parse_dice_expression(msg, dices):
dice_expr_list[j] = Dice(msg, item)
elif item.isdigit():
dice_count += 1
else:
continue
except (DiceSyntaxError, DiceValueError) as ex:
errmsg = msg.locale.t('dice.message.error.prompt', i=dice_count) + ex.message
if errmsg:
Expand Down Expand Up @@ -175,7 +181,7 @@ def generate_dice_message(msg, expr, dice_expr_list, dice_count, times, dc, use_
result = int(se.eval(dice_res))
else:
raise SyntaxError
except (FunctionNotDefined, NameNotDefined, SyntaxError):
except (FunctionNotDefined, NameNotDefined, SyntaxError, TypeError):
return DiceSyntaxError(msg, msg.locale.t('dice.message.error.syntax')).message
except Exception as e:
return DiceValueError(msg, msg.locale.t('dice.message.error') + '\n' + str(e)).message
Expand Down

0 comments on commit 9171375

Please sign in to comment.