|
1 | | -import logging |
| 1 | +import logging, random |
2 | 2 |
|
3 | 3 | from sirbot import SirBot |
4 | 4 | from sirbot.plugins.slack import SlackPlugin |
@@ -26,6 +26,7 @@ def create_endpoints(plugin: SlackPlugin): |
26 | 26 | plugin.on_command('/repeat', slash_repeat, wait=False) |
27 | 27 | plugin.on_command('/report', slash_report, wait=False) |
28 | 28 | plugin.on_command('/ticket', slash_ticket, wait=False) |
| 29 | + plugin.on_command('/roll', slash_roll, wait=False) |
29 | 30 | # plugin.on_command('/mentor', slash_mentor, wait=False) |
30 | 31 |
|
31 | 32 |
|
@@ -140,3 +141,37 @@ async def slash_repeat(command: Command, app: SirBot): |
140 | 141 |
|
141 | 142 | method_type, message = get_slash_repeat_messages(slack_id, channel_id, command['text']) |
142 | 143 | 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