Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
DoroWolf authored Mar 24, 2024
1 parent f4353d9 commit 8253686
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions modules/dice/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import secrets

import numpy as np
from simpleeval import simple_eval, NameNotDefined
from simpleeval import SimpleEval, FunctionNotDefined, NameNotDefined

from config import Config
from core.exceptions import ConfigValueError
Expand All @@ -17,6 +17,28 @@
MAX_DETAIL_CNT = Config('dice_detail_count', 5) # n次投掷的骰子的总量超过该值时将不再显示详细信息
MAX_ITEM_COUNT = Config('dice_count_limit', 10) # 骰子表达式最多的项数

math_funcs = {
'abs': abs,
'ceil': math.ceil,
'comb': math.comb,
'exp': math.exp,
'fabs': math.fabs,
'factorial': math.factorial,
'fmod': math.fmod,
'floor': math.floor,
'gcd': math.gcd,
'lcm': math.lcm,
'log': math.log,
'log2': math.log2,
'log10': math.log10,
'perm': math.perm,
'pow': math.pow,
'sqrt': math.sqrt,
}

se = SimpleEval()
se.functions.update(math_funcs)


# 异常类定义
class DiceSyntaxError(Exception):
Expand Down Expand Up @@ -336,8 +358,8 @@ def generate_dice_message(msg, expr, dice_expr_list, dice_count, times, dc, use_
try:
dice_res = ''.join(dice_res_list)
dice_res = dice_res.replace('\*', '*')
result = int(simple_eval(dice_res))
except (NameNotDefined, SyntaxError):
result = int(se.eval('a'))
except (FunctionNotDefined, NameNotDefined, SyntaxError):
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 8253686

Please sign in to comment.