Skip to content

Commit 9eaaaf8

Browse files
authored
Merge pull request #65 from LivingInSyn/master
added roll command
2 parents a0ea5ba + cb35f3d commit 9eaaaf8

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

pybot/endpoints/slack/commands.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import logging
1+
import logging, random
22

33
from sirbot import SirBot
44
from sirbot.plugins.slack import SlackPlugin
@@ -26,6 +26,7 @@ def create_endpoints(plugin: SlackPlugin):
2626
plugin.on_command('/repeat', slash_repeat, wait=False)
2727
plugin.on_command('/report', slash_report, wait=False)
2828
plugin.on_command('/ticket', slash_ticket, wait=False)
29+
plugin.on_command('/roll', slash_roll, wait=False)
2930
# plugin.on_command('/mentor', slash_mentor, wait=False)
3031

3132

@@ -140,3 +141,37 @@ async def slash_repeat(command: Command, app: SirBot):
140141

141142
method_type, message = get_slash_repeat_messages(slack_id, channel_id, command['text'])
142143
await slack.query(method_type, message)
144+
145+
async def slash_roll(command: Command, app: SirBot):
146+
"""
147+
Sends text supplied with the /report command to the moderators channel along
148+
with a button to claim the issue
149+
"""
150+
slack_id = command['user_id']
151+
channel_id = command['channel_id']
152+
text = command['text']
153+
154+
slack = app["plugins"]["slack"].api
155+
156+
#parse the type of die and number to roll
157+
try:
158+
text = text.lower()
159+
numdice, typedice = text.split('d')
160+
numdice = int(numdice)
161+
typedice = int(typedice)
162+
if numdice <= 0 or numdice > 10:
163+
raise ValueError
164+
if typedice <= 0 or typedice > 20:
165+
raise ValueError
166+
except ValueError:
167+
logger.debug("invalid input to roll: %s", text)
168+
await slack.query(methods.CHAT_POST_EPHEMERAL,
169+
{'user': slack_id, 'channel': channel_id,
170+
'text': "Sorry, I didn't understand your input. Should be XDYY where X is the number of dice, and YY is the number of sides"})
171+
return
172+
dice = []
173+
for _ in range(0,numdice):
174+
dice.append(random.randint(1,typedice+1))
175+
176+
message = f'<@{slack_id}> Rolled {numdice} D{typedice}: {dice}'
177+
await slack.query(methods.CHAT_POST_MESSAGE, {'channel': channel_id, 'text': message})

0 commit comments

Comments
 (0)