|
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 |
|
30 | 31 |
|
31 | 32 | async def slash_ticket(command: Command, app: SirBot): |
@@ -123,3 +124,37 @@ async def slash_repeat(command: Command, app: SirBot): |
123 | 124 |
|
124 | 125 | method_type, message = get_slash_repeat_messages(slack_id, channel_id, command['text']) |
125 | 126 | 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