|
1 | 1 | import asyncio
|
2 |
| -import logging |
3 | 2 | import re
|
| 3 | +import typing |
4 | 4 |
|
5 | 5 | import velum
|
6 | 6 |
|
7 | 7 | import sail
|
8 | 8 |
|
9 |
| -logging.basicConfig() |
10 |
| -logging.getLogger().setLevel(logging.DEBUG) |
11 |
| - |
12 |
| - |
13 | 9 | # Occasionally, the default string parser gets in your way.
|
14 | 10 | # Say you want to do something with codeblocks, where you do not want the
|
15 | 11 | # quotes inside to be consumed by the default parser. In situations like these,
|
@@ -57,6 +53,30 @@ async def codeblock(ctx: sail.Context, codeblock: str) -> None:
|
57 | 53 | ... # Actual code eval implementation...
|
58 | 54 |
|
59 | 55 |
|
| 56 | +# However, we can make this a bit more powerful if we want. |
| 57 | +# If we do the regex parsing in the parser, we can actually provide separate |
| 58 | +# args/kwargs to the function: |
| 59 | + |
| 60 | + |
| 61 | +def improved_parser( |
| 62 | + user_input: str, |
| 63 | +) -> typing.Tuple[typing.Sequence[str], typing.Dict[str, typing.Sequence[str]]]: |
| 64 | + match = CODEBLOCK_RE.fullmatch(user_input) |
| 65 | + |
| 66 | + if not match: |
| 67 | + # This can be handled externally... |
| 68 | + raise RuntimeError("Not a codeblock.") |
| 69 | + |
| 70 | + return [match.group("lang"), match.group("body")], {} |
| 71 | + |
| 72 | + |
| 73 | +@manager.command(string_parser=improved_parser) |
| 74 | +async def cooler_codeblock(ctx: sail.Context, lang: str, body: str) -> None: |
| 75 | + await client.rest.send_message("Sail", f"Evaluating your `{lang}` code...") |
| 76 | + |
| 77 | + ... # Actual code eval implementation... |
| 78 | + |
| 79 | + |
60 | 80 | # Finally, we can run the bot as per usual.
|
61 | 81 |
|
62 | 82 | asyncio.run(client.start())
|
0 commit comments