-
Notifications
You must be signed in to change notification settings - Fork 1
/
roll.py
82 lines (73 loc) · 2.82 KB
/
roll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import random
import re
from datetime import datetime
from random import randint as ri
from .utils import *
logger = get_logger('kusa')
DEFAULT_PLANE = 6
MAX_DICE = 10
MAX_PLANE = 1024
class Roll:
def __init__(self, **kwargs):
logger.info('初始化Roll')
self.api = kwargs['bot_api']
self.plane = DEFAULT_PLANE
async def execute_async(self, message):
msg = message['raw_message']
group = str(message.get("group_id", ''))
user = str(message.get("user_id", 0))
result = self.get_roll_result(msg)
if result:
return '[CQ:reply,id={}]{}'.format(message['message_id'], result)
def get_roll_result(self, msg):
replys = []
prm = re.match('([/!\.]?roll|[/!\.]r)(.*)', msg)
if prm:
try:
msg = prm[2].strip()
if not msg:
return '{}'.format(ri(1, 100))
prm = re.findall('(\d+)?d(\d+)?([\+\-]\d+)?', msg)
for p in prm:
dice = int(p[0]) if p[0] else 1
plane = int(p[1]) if p[1] else self.plane
delta = int(p[2]) if p[2] else 0
if plane <= 0:
return '¿'
if plane > MAX_PLANE:
return '这么多面,建议玩球'
if dice > MAX_DICE:
return '唔得,骰子不够叻'
for i in range(dice):
replys.append('{}'.format(ri(1, plane) + delta))
if replys:
if len(replys) > MAX_DICE:
return '唔得,骰子不够叻'
return ' '.join(replys)
prm = re.match('([\+\-]?\d+) +([\+\-]?\d+)', msg)
if prm:
l, r = min(int(prm[1]), int(prm[2])), max(int(prm[1]), int(prm[2]))
return '{}'.format(ri(l, r))
prm = re.match('([\+\-]?\d+)', msg)
if prm:
n = int(prm[1])
if n <= 0:
return '{}'.format(ri(n, 0))
return '{}'.format(ri(1, n))
except Exception as e:
return 'Roll error: {}'.format(e)
prm = re.match('([/!\.]set)(.*)', msg)
if prm:
msg = prm[2].strip()
if not msg:
self.plane = DEFAULT_PLANE
return '默认面数现在是{}'.format(self.plane)
prm = re.match('([\+\-]?\d+)', msg)
if prm:
p = int(prm[1])
if p <= 0:
return '¿'
if p > MAX_PLANE:
return '这么多面,建议玩球'
self.plane = p
return '默认面数现在是{}'.format(self.plane)