-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_dice.py
More file actions
220 lines (177 loc) · 6.67 KB
/
command_dice.py
File metadata and controls
220 lines (177 loc) · 6.67 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import commands
import random
import re
mr_re = re.compile(r'^(\d+)d(?:(\d+)|(\[.*\]))\s*(?:(\+|-)\s*(\d+))?\s*(.*)$')
drop_re = re.compile(r'^dl(\d*)(?:dh(\d*))?|dh(\d*)(?:dl(\d*))?$')
mod_re = re.compile(r'^(\+|-)\s*(\d+)$')
@commands.command(condition=lambda line : commands.first_arg_match(line, 'dice', 'd'))
async def command_dice(line, message, meta, reng):
args = line.split()
if len(args) == 2:
try:
i = int(args[1])
if i > 0:
return f'Rolled **{random.randint(1, i)}**.'
else:
return f'**[Error]** Arg 1 ({args[1]}) must be a positive integer.'
except ValueError:
return f'**[Error]** Arg 1 ({args[1]}) must be a positive integer.'
elif len(args) == 3:
try:
i1 = int(args[1])
except ValueError:
return f'**[Error]** Arg 1 ({args[1]}) must be an integer.'
try:
i2 = int(args[2])
except ValueError:
return f'**[Error]** Arg 2 ({args[2]}) must be an integer.'
return f'Rolled **{random.randint(min(i1, i2), max(i1, i2))}**.'
return '**[Usage]** !dice <range> [range]'
@commands.command(condition=lambda line : commands.first_arg_match(line, 'multiroll', 'multi'))
async def command_multiroll(line, message, meta, reng):
args = line.split()
if len(args) == 3:
try:
i1 = int(args[1])
if i1 <= 0 or i1 >= 500:
return f'**[Error]** Arg 1 ({args[1]}) must be a positive integer less than 500.'
except ValueError:
return f'**[Error]** Arg 1 ({args[1]}) must be a positive integer less than 500.'
try:
i2 = int(args[2])
if i2 <= 0:
return f'**[Error]** Arg 2 ({args[2]}) must be a positive integer less than 500.'
except ValueError:
return f'**[Error]** Arg 2 ({args[2]}) must be a positive integer.'
res = [random.randint(1, i2) for _ in range(i1)]
return f"Rolled {', '.join(('**' + str(i) + '**') for i in res)} Total: **{sum(res)}**."
elif len(args) == 4:
try:
i1 = int(args[1])
if i1 <= 0 or i1 > 100:
return f'**[Error]** Arg 1 ({args[1]}) must be a positive integer 100 or less.'
except ValueError:
return f'**[Error]** Arg 1 ({args[1]}) must be a positive integer 100 or less.'
try:
i2 = int(args[2])
except ValueError:
return f'**[Error]** Arg 2 ({args[2]}) must be an integer.'
try:
i3 = int(args[3])
except ValueError:
return f'**[Error]** Arg 3 ({args[3]}) must be an integer.'
res = [random.randint(i2, i3) for _ in range(i1)]
return f"Rolled {', '.join(('**' + str(i) + '**') for i in res)} Total: **{sum(res)}**."
return '**[Usage]** !multiroll <amount> <range> [range]'
@commands.command(condition=lambda line : True)
async def command_xdy(line, message, meta, reng):
match = mr_re.match(line)
if match == None:
raise commands.SkipCommand
try:
dice_count = int(match.group(1))
if dice_count <= 0 or dice_count > 1000:
return f'**[Error]** Dice count: {dice_count} must be a positive integer 1000 or less.'
values = None
if (match.group(2) == None):
values = []
weights = []
val_len = 0
roll = match.group(3)[1:-1]
try:
for lst in roll.split(','):
comp = lst.split(':')
if len(comp) == 1:
val = int(comp[0])
values.append(range(val, val + 1))
weights.append(1)
val_len += 1
elif len(comp) == 2:
i1 = int(comp[0])
i2 = int(comp[1])
rn = range(min(i1, i2), max(i1, i2) + 1)
rn_len = rn.stop - rn.start
values.append(rn)
weights.append(rn_len)
val_len += rn_len
if len(values) == 0:
return f'**[Error]** Invalid roll: d[{roll}].'
except ValueError:
return f'**[Error]** Invalid roll: d[{roll}].'
else:
roll = int(match.group(2))
val_len = roll
if roll <= 0:
return f'**[Error]** Invalid roll: d{roll}.'
mod = 0
if match.group(4) and match.group(5):
mod = int(match.group(4) + match.group(5))
dl = 0
dh = 0
sort = False
nosum = dice_count == 1 and mod == 0
sumonly = False
unique = False
for option in match.group(6).split():
drop_match = drop_re.match(option.lower())
if drop_match != None:
if drop_match.group(1) != None:
dl = 1 if drop_match.group(1) == '' else int(drop_match.group(1))
if drop_match.group(2) != None:
dh = 1 if drop_match.group(2) == '' else int(drop_match.group(2))
if drop_match.group(3) != None:
dh = 1 if drop_match.group(3) == '' else int(drop_match.group(3))
if drop_match.group(4) != None:
dl = 1 if drop_match.group(4) == '' else int(drop_match.group(4))
if dl + dh > dice_count:
return '**[Error]** Cannot drop more dice than amount rolled.'
elif option.lower() == 'sorted':
sort = True
elif option.lower() == 'nosum':
if sumonly:
return '**[Error]** Cannot use sumonly option with nosum option or when there is only one die.'
nosum = True
elif option.lower() == 'sumonly':
if nosum:
return '**[Error]** Cannot use sumonly option with nosum option or when there is only one die.'
sumonly = True
elif option.lower() == 'unique':
unique = True
if dice_count > val_len:
return '**[Error]** Not enough possible values for unique option.'
if val_len > 1000000000:
return '**[Error]** Overflow: Cannot use more than 1000000000 die faces when using unique option.'
else:
return f'**[Error]** Unknown option: {option}.'
if unique:
if values == None:
res = random.sample(range(1, roll + 1), dice_count)
else:
res = random.sample(sum((list(rn) for rn in values), []), dice_count)
else:
if values == None:
res = [random.randint(1, roll) for _ in range(dice_count)]
else:
res = [random.randrange(rn.start, rn.stop) for rn in random.choices(values, weights, k=dice_count)]
if sort:
res.sort()
dropped = set()
if dl > 0:
dropped.update(sorted(range(dice_count), key=lambda i: res[i])[:dl])
if dh > 0:
dropped.update(sorted(range(dice_count), key=lambda i: res[i], reverse=True)[:dh])
if nosum:
statement = f"Rolled {', '.join((('~~' if i in dropped else '**') + str(res[i]) + ('~~' if i in dropped else '**')) for i in range(dice_count))}."
elif sumonly:
statement = f"Total: **{sum(res[i] for i in range(dice_count) if i not in dropped) + mod}**."
else:
statement = f"Rolled {', '.join((('~~' if i in dropped else '**') + str(res[i]) + ('~~' if i in dropped else '**')) for i in range(dice_count))}"
if mod != 0:
statement += f', **(+{mod})**' if mod > 0 else f', **(-{-mod})**'
statement += f" Total: **{sum(res[i] for i in range(dice_count) if i not in dropped) + mod}**."
return statement
except ValueError as e:
raise commands.SkipCommand
@commands.command(condition=lambda line : commands.first_arg_match(line, 'percent', '%'))
async def command_percent(line, message, meta, reng):
return f'Rolled **{random.randint(1, 100)}**.'