Skip to content

Commit 1c06b99

Browse files
author
Jeremy Mill
committed
added roll command
1 parent 80cb10f commit 1c06b99

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

3031

3132
async def slash_ticket(command: Command, app: SirBot):
@@ -123,3 +124,37 @@ async def slash_repeat(command: Command, app: SirBot):
123124

124125
method_type, message = get_slash_repeat_messages(slack_id, channel_id, command['text'])
125126
await slack.query(method_type, message)
127+
128+
async def slash_roll(command: Command, app: SirBot):
129+
"""
130+
Sends text supplied with the /report command to the moderators channel along
131+
with a button to claim the issue
132+
"""
133+
slack_id = command['user_id']
134+
channel_id = command['channel_id']
135+
text = command['text']
136+
137+
slack = app["plugins"]["slack"].api
138+
139+
#parse the type of die and number to roll
140+
try:
141+
text = text.lower()
142+
numdice, typedice = text.split('d')
143+
numdice = int(numdice)
144+
typedice = int(typedice)
145+
if numdice <= 0 or numdice > 10:
146+
raise ValueError
147+
if typedice <= 0 or typedice > 20:
148+
raise ValueError
149+
except ValueError:
150+
logger.debug("invalid input to roll: %s", text)
151+
await slack.query(methods.CHAT_POST_EPHEMERAL,
152+
{'user': slack_id, 'channel': channel_id,
153+
'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"})
154+
return
155+
dice = []
156+
for _ in range(0,numdice):
157+
dice.append(random.randint(1,typedice+1))
158+
159+
message = f'<@{slack_id}> Rolled {numdice} D{typedice}: {dice}'
160+
await slack.query(methods.CHAT_POST_MESSAGE, {'channel': channel_id, 'text': message})

0 commit comments

Comments
 (0)