Skip to content

Commit bffc1cf

Browse files
LordOfPollspre-commit-ci[bot]i0bs
authored
feat: implement console ext (#1287)
* feat: implement console ext * style: linter pass * ci: correct from checks. * Update pyproject.toml Co-authored-by: Sophia <41456914+i0bs@users.noreply.github.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sophia <41456914+i0bs@users.noreply.github.com>
1 parent a97d8dc commit bffc1cf

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

docs/src/Guides/98 2.x Migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
!!! Placeholder for 2.x migration guide
1+
!!! Placeholder for 2.x migration guide

docs/src/extra.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,4 @@ div.doc-contents:not(.first) {
197197
.md-header__button.md-logo{
198198
margin-right: 0;
199199
filter: brightness(0) invert(1); /* makes the logo white */
200-
}
200+
}

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ async def on_start():
7676
print("Bot is ready!")
7777

7878
bot.start("token")
79-
```
79+
```

interactions/ext/console.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
3+
import aiomonitor as aiomonitor
4+
5+
import interactions
6+
from interactions import Extension
7+
8+
__all__ = ("Console",)
9+
10+
11+
class Console(Extension):
12+
"""
13+
Extension that starts the bot with the aiomonitor console active - notably giving you REPL for the bot
14+
15+
To access the console, you need to connect to the port specified in the constructor, by default 501.
16+
On linux, you can do this with `nc localhost 501`, on windows you can use `telnet localhost 501`.
17+
18+
For both platforms you can also use "python -m aiomonitor.cli -p 501" as a replacement for the above commands.
19+
20+
Args:
21+
port: The port to start the aiomonitor on
22+
console_port: The port to start the aiomonitor console on
23+
**kwargs: The locals to make available in the console, by default this includes `client`, `bot` and `interactions`
24+
25+
"""
26+
27+
def __init__(self, client: interactions.Client, port: int = 501, console_port: int = 502, **kwargs) -> None:
28+
self.client.astart = self.async_start_bot
29+
self.port = port # 501 was chosen as windows throws a massive fit if you try to use the default port
30+
self.console_port = console_port
31+
self.locals = kwargs
32+
33+
async def async_start_bot(self, token: str | None = None) -> None:
34+
"""Starts the bot with the console active"""
35+
old_start = interactions.Client.astart
36+
37+
_locals = self.locals | {
38+
"client": self.client,
39+
"bot": self.client,
40+
"interactions": interactions,
41+
}
42+
43+
with aiomonitor.start_monitor(
44+
loop=asyncio.get_event_loop(), port=self.port, console_port=self.console_port, locals=_locals
45+
) as monitor:
46+
self.client.logger.info(f"Started aiomonitor on {monitor._host}:{monitor._port}") # noqa
47+
48+
await old_start(self.client, token)

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ emoji = "^2.1.0"
1818
# Optional dependencies
1919
orjson = {version = "^3.6.8", optional = true}
2020
jurigged = {version = "^0.5.3", optional = true}
21+
aioconsole = {version = "^0.6.0", optional = true}
2122

2223
[tool.poetry.extras]
2324
orjson = ["orjson"]
2425
jurigged = ["jurigged"]
26+
aioconsole = ["aioconsole"]
2527

2628
[tool.poetry.group.dev.dependencies]
2729
black = "^22.3.0"

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"speedup": ["aiodns", "orjson", "Brotli"],
1313
"sentry": ["sentry-sdk"],
1414
"jurigged": ["jurigged"],
15+
"console": ["aioconsole>=0.6.0"],
1516
}
1617
extras_require["all"] = list(itertools.chain.from_iterable(extras_require.values()))
1718
extras_require["docs"] = extras_require["all"] + [

0 commit comments

Comments
 (0)