matrix-python 1.4.1a0__tar.gz → 1.4.3a0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- matrix_python-1.4.3a0/.github/workflows/tests.yml +35 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/PKG-INFO +18 -9
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/README.md +17 -8
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/checks.py +2 -2
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/cooldown.py +2 -2
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/error_handling.py +2 -2
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/extension.py +4 -2
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/ping.py +2 -2
- matrix_python-1.4.3a0/examples/reaction.py +39 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/scheduler.py +5 -5
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/__init__.py +2 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/_version.py +3 -3
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/bot.py +49 -21
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/message.py +61 -22
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/room.py +53 -19
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/types.py +6 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix_python.egg-info/PKG-INFO +18 -9
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_bot.py +30 -30
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_message.py +78 -9
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_room.py +115 -82
- matrix_python-1.4.1a0/.github/workflows/tests.yml +0 -35
- matrix_python-1.4.1a0/examples/reaction.py +0 -43
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/.github/dependabot.yml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/.github/workflows/CODEOWNERS +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/.github/workflows/codeql.yml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/.github/workflows/publish.yml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/.github/workflows/scorecard.yml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/.gitignore +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/CODE_OF_CONDUCT.md +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/CONTRIBUTING.md +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/LICENSE +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/README.md +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/examples/config.yaml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/checks.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/command.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/config.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/content.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/context.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/errors.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/extension.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/group.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/help/__init__.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/help/help_command.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/help/pagination.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/protocols.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/registry.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix/scheduler.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix_python.egg-info/SOURCES.txt +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix_python.egg-info/dependency_links.txt +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix_python.egg-info/requires.txt +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/matrix_python.egg-info/top_level.txt +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/mypy.ini +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/pyproject.toml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/setup.cfg +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/config_fixture.yaml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/config_fixture_token.yaml +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/help/test_default_help_command.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/help/test_help_command.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/help/test_pagination.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_command.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_config.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_context.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_extension.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_group.py +0 -0
- {matrix_python-1.4.1a0 → matrix_python-1.4.3a0}/tests/test_registry.py +0 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ "main" ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ "main" ]
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
- name: Set up Python 3.12
|
|
20
|
+
uses: actions/setup-python@v3
|
|
21
|
+
with:
|
|
22
|
+
python-version: "3.12"
|
|
23
|
+
- name: Install dependencies
|
|
24
|
+
run: |
|
|
25
|
+
python -m pip install --upgrade pip
|
|
26
|
+
pip install .[dev]
|
|
27
|
+
- name: Lint with Black
|
|
28
|
+
run: |
|
|
29
|
+
black --check matrix/ tests/ examples/
|
|
30
|
+
- name: Check typing with mypy
|
|
31
|
+
run: |
|
|
32
|
+
mypy matrix
|
|
33
|
+
- name: Test with pytest
|
|
34
|
+
run: |
|
|
35
|
+
pytest -v
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: matrix-python
|
|
3
|
-
Version: 1.4.
|
|
3
|
+
Version: 1.4.3a0
|
|
4
4
|
Summary: An easy-to-use Matrix bot framework designed for quick development and minimal setup
|
|
5
5
|
Author: Simon Roy, Chris Dedman Rollet
|
|
6
6
|
Maintainer-email: Code Society Lab <admin@codesociety.xyz>
|
|
@@ -718,6 +718,7 @@ decorator-based API similar to popular event-driven frameworks, allowing
|
|
|
718
718
|
developers to focus on behavior rather than boilerplate.
|
|
719
719
|
|
|
720
720
|
#### Key Features
|
|
721
|
+
|
|
721
722
|
- Minimal setup, easy to extend
|
|
722
723
|
- Event-driven API using async/await
|
|
723
724
|
- Clean command registration
|
|
@@ -727,6 +728,7 @@ developers to focus on behavior rather than boilerplate.
|
|
|
727
728
|
# Quickstart
|
|
728
729
|
|
|
729
730
|
**Requirements**
|
|
731
|
+
|
|
730
732
|
- Python 3.10+
|
|
731
733
|
|
|
732
734
|
```
|
|
@@ -734,38 +736,45 @@ pip install matrix-python
|
|
|
734
736
|
```
|
|
735
737
|
|
|
736
738
|
If you plan on contributing to matrix.py, we recommend to install the development libraries:
|
|
739
|
+
|
|
737
740
|
```
|
|
738
741
|
pip install -e .[dev]
|
|
739
742
|
```
|
|
740
743
|
|
|
741
|
-
*Note*: It is recommended to use
|
|
742
|
-
|
|
744
|
+
*Note*: It is recommended to use
|
|
745
|
+
a [virtual environment](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/)
|
|
746
|
+
when installing python packages.
|
|
743
747
|
|
|
744
748
|
```python
|
|
745
749
|
from matrix import Bot, Context
|
|
746
750
|
|
|
747
|
-
bot = Bot(
|
|
751
|
+
bot = Bot()
|
|
748
752
|
|
|
749
753
|
|
|
750
754
|
@bot.command("ping")
|
|
751
755
|
async def ping(ctx: Context):
|
|
752
|
-
await ctx.
|
|
756
|
+
await ctx.reply("Pong!")
|
|
753
757
|
|
|
754
758
|
|
|
755
|
-
bot.start()
|
|
759
|
+
bot.start(config="config.yml")
|
|
756
760
|
```
|
|
757
761
|
|
|
758
762
|
[Documentation](https://github.com/Code-Society-Lab/matrixpy/wiki) - [Examples](https://github.com/Code-Society-Lab/matrixpy/tree/main/examples)
|
|
759
763
|
|
|
760
764
|
# Contributing
|
|
761
|
-
|
|
765
|
+
|
|
766
|
+
We welcome everyone to contribute!
|
|
762
767
|
|
|
763
768
|
Whether it's fixing bugs, suggesting features, or improving the docs - every bit helps.
|
|
769
|
+
|
|
764
770
|
- Submit an issue
|
|
765
771
|
- Open a pull request
|
|
766
|
-
- Or just hop into our [Matrix](https://matrix.to/#/%23codesociety:matrix.org)
|
|
772
|
+
- Or just hop into our [Matrix](https://matrix.to/#/%23codesociety:matrix.org)
|
|
773
|
+
or [Discord](https://discord.gg/code-society-823178343943897088) server and say hi!
|
|
767
774
|
|
|
768
|
-
If you intend to contribute, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) first. Additionally, **every
|
|
775
|
+
If you intend to contribute, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) first. Additionally, **every
|
|
776
|
+
contributor** is expected to follow the [code of conduct](./CODE_OF_CONDUCT.md).
|
|
769
777
|
|
|
770
778
|
# License
|
|
779
|
+
|
|
771
780
|
matrix.py is released under [GPL-3.0](https://opensource.org/license/gpl-3-0)
|
|
@@ -19,6 +19,7 @@ decorator-based API similar to popular event-driven frameworks, allowing
|
|
|
19
19
|
developers to focus on behavior rather than boilerplate.
|
|
20
20
|
|
|
21
21
|
#### Key Features
|
|
22
|
+
|
|
22
23
|
- Minimal setup, easy to extend
|
|
23
24
|
- Event-driven API using async/await
|
|
24
25
|
- Clean command registration
|
|
@@ -28,6 +29,7 @@ developers to focus on behavior rather than boilerplate.
|
|
|
28
29
|
# Quickstart
|
|
29
30
|
|
|
30
31
|
**Requirements**
|
|
32
|
+
|
|
31
33
|
- Python 3.10+
|
|
32
34
|
|
|
33
35
|
```
|
|
@@ -35,38 +37,45 @@ pip install matrix-python
|
|
|
35
37
|
```
|
|
36
38
|
|
|
37
39
|
If you plan on contributing to matrix.py, we recommend to install the development libraries:
|
|
40
|
+
|
|
38
41
|
```
|
|
39
42
|
pip install -e .[dev]
|
|
40
43
|
```
|
|
41
44
|
|
|
42
|
-
*Note*: It is recommended to use
|
|
43
|
-
|
|
45
|
+
*Note*: It is recommended to use
|
|
46
|
+
a [virtual environment](https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/)
|
|
47
|
+
when installing python packages.
|
|
44
48
|
|
|
45
49
|
```python
|
|
46
50
|
from matrix import Bot, Context
|
|
47
51
|
|
|
48
|
-
bot = Bot(
|
|
52
|
+
bot = Bot()
|
|
49
53
|
|
|
50
54
|
|
|
51
55
|
@bot.command("ping")
|
|
52
56
|
async def ping(ctx: Context):
|
|
53
|
-
await ctx.
|
|
57
|
+
await ctx.reply("Pong!")
|
|
54
58
|
|
|
55
59
|
|
|
56
|
-
bot.start()
|
|
60
|
+
bot.start(config="config.yml")
|
|
57
61
|
```
|
|
58
62
|
|
|
59
63
|
[Documentation](https://github.com/Code-Society-Lab/matrixpy/wiki) - [Examples](https://github.com/Code-Society-Lab/matrixpy/tree/main/examples)
|
|
60
64
|
|
|
61
65
|
# Contributing
|
|
62
|
-
|
|
66
|
+
|
|
67
|
+
We welcome everyone to contribute!
|
|
63
68
|
|
|
64
69
|
Whether it's fixing bugs, suggesting features, or improving the docs - every bit helps.
|
|
70
|
+
|
|
65
71
|
- Submit an issue
|
|
66
72
|
- Open a pull request
|
|
67
|
-
- Or just hop into our [Matrix](https://matrix.to/#/%23codesociety:matrix.org)
|
|
73
|
+
- Or just hop into our [Matrix](https://matrix.to/#/%23codesociety:matrix.org)
|
|
74
|
+
or [Discord](https://discord.gg/code-society-823178343943897088) server and say hi!
|
|
68
75
|
|
|
69
|
-
If you intend to contribute, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) first. Additionally, **every
|
|
76
|
+
If you intend to contribute, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) first. Additionally, **every
|
|
77
|
+
contributor** is expected to follow the [code of conduct](./CODE_OF_CONDUCT.md).
|
|
70
78
|
|
|
71
79
|
# License
|
|
80
|
+
|
|
72
81
|
matrix.py is released under [GPL-3.0](https://opensource.org/license/gpl-3-0)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from matrix import Bot, Context
|
|
2
2
|
from matrix.errors import CheckError
|
|
3
3
|
|
|
4
|
-
bot = Bot(
|
|
4
|
+
bot = Bot()
|
|
5
5
|
|
|
6
6
|
allowed_users = {"@alice:matrix.org", "@bob:matrix.org"}
|
|
7
7
|
|
|
@@ -23,4 +23,4 @@ async def permission_error_handler(ctx: Context, error: CheckError) -> None:
|
|
|
23
23
|
await ctx.reply(f"Access denied: {error}")
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
bot.start()
|
|
26
|
+
bot.start(config="config.yaml")
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from matrix import Bot, Context, cooldown
|
|
2
2
|
from matrix.errors import CooldownError
|
|
3
3
|
|
|
4
|
-
bot = Bot(
|
|
4
|
+
bot = Bot()
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
# Invoke by using !hello
|
|
@@ -27,4 +27,4 @@ async def cooldown_function(ctx: Context, error: CooldownError) -> None:
|
|
|
27
27
|
await ctx.reply(f"⏳ Try again in {error.retry:.1f}s")
|
|
28
28
|
|
|
29
29
|
|
|
30
|
-
bot.start()
|
|
30
|
+
bot.start(config="config.yaml")
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from matrix import Bot, Context
|
|
2
2
|
from matrix.errors import CommandNotFoundError, MissingArgumentError
|
|
3
3
|
|
|
4
|
-
bot = Bot(
|
|
4
|
+
bot = Bot()
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
@bot.error(CommandNotFoundError)
|
|
@@ -30,4 +30,4 @@ async def command_error(ctx: Context, error: MissingArgumentError) -> None:
|
|
|
30
30
|
await ctx.reply(f"{error}")
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
bot.start()
|
|
33
|
+
bot.start(config="config.yaml")
|
|
@@ -34,12 +34,14 @@ async def divide_error(ctx: Context, error):
|
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
"""
|
|
37
|
+
# bot.py
|
|
38
|
+
|
|
37
39
|
from matrix import Bot
|
|
38
40
|
from math_extension import extension as math_extension
|
|
39
41
|
|
|
40
|
-
bot = Bot(
|
|
42
|
+
bot = Bot()
|
|
41
43
|
|
|
42
44
|
|
|
43
45
|
bot.load_extension(math_extension)
|
|
44
|
-
bot.start()
|
|
46
|
+
bot.start(config="config.yaml")
|
|
45
47
|
"""
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from asyncio import Event
|
|
2
|
+
from nio import MatrixRoom, RoomMessageText, ReactionEvent
|
|
3
|
+
from matrix import Bot, Room, Message
|
|
4
|
+
|
|
5
|
+
bot = Bot()
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@bot.event
|
|
9
|
+
async def on_message(room: Room, event: RoomMessageText) -> None:
|
|
10
|
+
"""
|
|
11
|
+
This function listens for new messages in a room and reacts based
|
|
12
|
+
on the message content.
|
|
13
|
+
"""
|
|
14
|
+
message = await room.fetch_message(event.event_id)
|
|
15
|
+
|
|
16
|
+
if message.body.lower().startswith("thanks"):
|
|
17
|
+
await message.react("🙏")
|
|
18
|
+
|
|
19
|
+
if message.body.lower().startswith("hello"):
|
|
20
|
+
# Can also react with a text message instead of emoji
|
|
21
|
+
await message.react("hi")
|
|
22
|
+
|
|
23
|
+
if message.body.lower().startswith("❤️"):
|
|
24
|
+
await message.react("❤️")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@bot.event
|
|
28
|
+
async def on_react(room: Room, event: ReactionEvent) -> None:
|
|
29
|
+
"""
|
|
30
|
+
This function listens for new member reaction to messages in a room,
|
|
31
|
+
and reacts based on the reaction emoji.
|
|
32
|
+
"""
|
|
33
|
+
message = await room.fetch_message(event.reacts_to)
|
|
34
|
+
|
|
35
|
+
if message.key == "🙏":
|
|
36
|
+
await message.react("❤️")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
bot.start(config="config.yaml")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from matrix import Bot, Context
|
|
2
2
|
|
|
3
|
-
bot = Bot(
|
|
3
|
+
bot = Bot()
|
|
4
4
|
|
|
5
5
|
room_id = "!your_room_id:matrix.org" # Replace with your room ID
|
|
6
6
|
|
|
@@ -15,19 +15,19 @@ async def ping(ctx: Context) -> None:
|
|
|
15
15
|
@bot.schedule("* * * * *")
|
|
16
16
|
async def scheduled_task() -> None:
|
|
17
17
|
# This task runs every minute.
|
|
18
|
-
await room.send(
|
|
18
|
+
await room.send("Scheduled ping!")
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
@bot.schedule("0 * * * *")
|
|
22
22
|
async def hourly_task() -> None:
|
|
23
23
|
# This task runs every hour.
|
|
24
|
-
await room.send(
|
|
24
|
+
await room.send("This is your hourly update!")
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
@bot.schedule("0 9 * * 1-5")
|
|
28
28
|
async def weekday_morning_task() -> None:
|
|
29
29
|
# This task runs every weekday at 9 AM.
|
|
30
|
-
await room.send(
|
|
30
|
+
await room.send("Good morning! Here's your weekday update!")
|
|
31
31
|
|
|
32
32
|
|
|
33
|
-
bot.start()
|
|
33
|
+
bot.start(config="config.yaml")
|
|
@@ -15,6 +15,7 @@ from .command import Command
|
|
|
15
15
|
from .help import HelpCommand
|
|
16
16
|
from .checks import cooldown
|
|
17
17
|
from .room import Room
|
|
18
|
+
from .message import Message
|
|
18
19
|
from .extension import Extension
|
|
19
20
|
|
|
20
21
|
__all__ = [
|
|
@@ -27,5 +28,6 @@ __all__ = [
|
|
|
27
28
|
"HelpCommand",
|
|
28
29
|
"cooldown",
|
|
29
30
|
"Room",
|
|
31
|
+
"Message",
|
|
30
32
|
"Extension",
|
|
31
33
|
]
|
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '1.4.
|
|
22
|
-
__version_tuple__ = version_tuple = (1, 4,
|
|
21
|
+
__version__ = version = '1.4.3a0'
|
|
22
|
+
__version_tuple__ = version_tuple = (1, 4, 3, 'a0')
|
|
23
23
|
|
|
24
|
-
__commit_id__ = commit_id = '
|
|
24
|
+
__commit_id__ = commit_id = 'gba1ba5d3c'
|
|
@@ -28,29 +28,34 @@ class Bot(Registry):
|
|
|
28
28
|
"""
|
|
29
29
|
|
|
30
30
|
def __init__(
|
|
31
|
-
self,
|
|
31
|
+
self,
|
|
32
|
+
*,
|
|
33
|
+
help_: Optional[HelpCommand] = None,
|
|
32
34
|
) -> None:
|
|
33
|
-
|
|
34
|
-
self.config = config
|
|
35
|
-
elif isinstance(config, str):
|
|
36
|
-
self.config = Config(config_path=config)
|
|
37
|
-
else:
|
|
38
|
-
raise TypeError("config must be a Config instance or a config file path")
|
|
35
|
+
super().__init__(self.__class__.__name__)
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
self.
|
|
37
|
+
self._config: Config | None = None
|
|
38
|
+
self._client: AsyncClient | None = None
|
|
39
|
+
self._help: HelpCommand | None = help_
|
|
43
40
|
self.extensions: dict[str, Extension] = {}
|
|
44
41
|
self.scheduler: Scheduler = Scheduler()
|
|
45
42
|
self.log: logging.Logger = logging.getLogger(__name__)
|
|
43
|
+
self.start_at: float | None = None
|
|
46
44
|
|
|
47
|
-
|
|
45
|
+
@property
|
|
46
|
+
def client(self) -> AsyncClient:
|
|
47
|
+
assert self._client is not None, "Bot has not been started."
|
|
48
|
+
return self._client
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
@property
|
|
51
|
+
def config(self) -> Config:
|
|
52
|
+
assert self._config is not None, "Bot has not been started."
|
|
53
|
+
return self._config
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
@property
|
|
56
|
+
def help(self) -> HelpCommand:
|
|
57
|
+
assert self._help is not None, "Bot has not been started."
|
|
58
|
+
return self._help
|
|
54
59
|
|
|
55
60
|
def _auto_register_events(self) -> None:
|
|
56
61
|
for attr in dir(self):
|
|
@@ -192,7 +197,26 @@ class Bot(Registry):
|
|
|
192
197
|
|
|
193
198
|
# ENTRYPOINT
|
|
194
199
|
|
|
195
|
-
def
|
|
200
|
+
def _load_config(self, config: Config | str) -> None:
|
|
201
|
+
if self._config is not None:
|
|
202
|
+
raise RuntimeError("Config is already loaded.")
|
|
203
|
+
|
|
204
|
+
if isinstance(config, str):
|
|
205
|
+
config = Config(config_path=config)
|
|
206
|
+
elif not isinstance(config, Config):
|
|
207
|
+
raise TypeError("config must be a Config instance or a config file path")
|
|
208
|
+
|
|
209
|
+
self._config = config
|
|
210
|
+
self._client = AsyncClient(config.homeserver)
|
|
211
|
+
self._help = self._help or DefaultHelpCommand()
|
|
212
|
+
|
|
213
|
+
self.prefix = config.prefix
|
|
214
|
+
self.register_command(self.help)
|
|
215
|
+
|
|
216
|
+
self.client.add_event_callback(self._on_matrix_event, Event)
|
|
217
|
+
self._auto_register_events()
|
|
218
|
+
|
|
219
|
+
def start(self, *, config: Config | str) -> None:
|
|
196
220
|
"""
|
|
197
221
|
Synchronous entry point for running the bot.
|
|
198
222
|
|
|
@@ -201,6 +225,9 @@ class Bot(Registry):
|
|
|
201
225
|
:func:`asyncio.run`, and ensures the client is closed gracefully
|
|
202
226
|
on interruption.
|
|
203
227
|
"""
|
|
228
|
+
if config is not None:
|
|
229
|
+
self._load_config(config)
|
|
230
|
+
|
|
204
231
|
try:
|
|
205
232
|
asyncio.run(self.run())
|
|
206
233
|
except KeyboardInterrupt:
|
|
@@ -235,10 +262,10 @@ class Bot(Registry):
|
|
|
235
262
|
|
|
236
263
|
# MATRIX EVENTS
|
|
237
264
|
|
|
238
|
-
async def on_message(self, room:
|
|
265
|
+
async def on_message(self, room: Room, event: Event) -> None:
|
|
239
266
|
await self._process_commands(room, event)
|
|
240
267
|
|
|
241
|
-
async def _on_matrix_event(self,
|
|
268
|
+
async def _on_matrix_event(self, matrix_room: MatrixRoom, event: Event) -> None:
|
|
242
269
|
# ignore bot events
|
|
243
270
|
if event.sender == self.client.user:
|
|
244
271
|
return
|
|
@@ -248,6 +275,7 @@ class Bot(Registry):
|
|
|
248
275
|
return
|
|
249
276
|
|
|
250
277
|
try:
|
|
278
|
+
room = self.get_room(matrix_room.room_id)
|
|
251
279
|
await self._dispatch_matrix_event(room, event)
|
|
252
280
|
except Exception as error:
|
|
253
281
|
await self._on_error(error)
|
|
@@ -257,14 +285,14 @@ class Bot(Registry):
|
|
|
257
285
|
for handler in self._hook_handlers.get(event_name, []):
|
|
258
286
|
await handler(*args, **kwargs)
|
|
259
287
|
|
|
260
|
-
async def _dispatch_matrix_event(self, room:
|
|
288
|
+
async def _dispatch_matrix_event(self, room: Room, event: Event) -> None:
|
|
261
289
|
"""Fire all listeners registered for a named matrix event."""
|
|
262
290
|
for event_type, funcs in self._event_handlers.items():
|
|
263
291
|
if isinstance(event, event_type):
|
|
264
292
|
for func in funcs:
|
|
265
293
|
await func(room, event)
|
|
266
294
|
|
|
267
|
-
async def _process_commands(self, room:
|
|
295
|
+
async def _process_commands(self, room: Room, event: Event) -> None:
|
|
268
296
|
"""Parse and execute commands"""
|
|
269
297
|
ctx = await self._build_context(room, event)
|
|
270
298
|
|
|
@@ -276,7 +304,7 @@ class Bot(Registry):
|
|
|
276
304
|
await self._on_command(ctx)
|
|
277
305
|
await ctx.command(ctx)
|
|
278
306
|
|
|
279
|
-
async def _build_context(self, matrix_room:
|
|
307
|
+
async def _build_context(self, matrix_room: Room, event: Event) -> Context:
|
|
280
308
|
room = self.get_room(matrix_room.room_id)
|
|
281
309
|
ctx = Context(bot=self, room=room, event=event)
|
|
282
310
|
prefix = self.prefix or self.config.prefix
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
from typing import TYPE_CHECKING
|
|
2
|
-
|
|
1
|
+
from typing import TYPE_CHECKING, Self
|
|
2
|
+
|
|
3
|
+
from nio import AsyncClient, Event
|
|
4
|
+
|
|
5
|
+
from matrix.types import Reaction
|
|
3
6
|
from matrix.content import ReactionContent, EditContent
|
|
4
7
|
from matrix.errors import MatrixError
|
|
5
8
|
|
|
@@ -10,28 +13,35 @@ if TYPE_CHECKING:
|
|
|
10
13
|
class Message:
|
|
11
14
|
"""Represents a Matrix message with methods to interact with it."""
|
|
12
15
|
|
|
13
|
-
def __init__(
|
|
14
|
-
self, *, room: "Room", event_id: str, body: str | None, client: AsyncClient
|
|
15
|
-
) -> None:
|
|
16
|
+
def __init__(self, *, room: "Room", event: Event, client: AsyncClient) -> None:
|
|
16
17
|
self._room = room
|
|
17
|
-
self.
|
|
18
|
-
self._body = body
|
|
18
|
+
self._matrix_event: Event = event
|
|
19
19
|
self._client = client
|
|
20
20
|
|
|
21
|
+
self._body = getattr(self._matrix_event, "body", None)
|
|
22
|
+
|
|
23
|
+
def __repr__(self) -> str:
|
|
24
|
+
return f"<Message id={self.event_id!r} body={self.body!r}>"
|
|
25
|
+
|
|
21
26
|
@property
|
|
22
27
|
def room(self) -> "Room":
|
|
23
28
|
"""The room this message was sent in."""
|
|
24
29
|
return self._room
|
|
25
30
|
|
|
26
31
|
@property
|
|
27
|
-
def
|
|
28
|
-
"""The event
|
|
29
|
-
return self.
|
|
32
|
+
def event(self) -> Event:
|
|
33
|
+
"""The matrix event of this message"""
|
|
34
|
+
return self._matrix_event
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def client(self) -> AsyncClient:
|
|
38
|
+
"""The Matrix client."""
|
|
39
|
+
return self._client
|
|
30
40
|
|
|
31
41
|
@property
|
|
32
42
|
def event_id(self) -> str:
|
|
33
|
-
"""The event ID of this message
|
|
34
|
-
return self.
|
|
43
|
+
"""The event ID of this message."""
|
|
44
|
+
return str(self._matrix_event.event_id)
|
|
35
45
|
|
|
36
46
|
@property
|
|
37
47
|
def body(self) -> str | None:
|
|
@@ -39,9 +49,41 @@ class Message:
|
|
|
39
49
|
return self._body
|
|
40
50
|
|
|
41
51
|
@property
|
|
42
|
-
def
|
|
43
|
-
"""The
|
|
44
|
-
return self.
|
|
52
|
+
def key(self) -> str | None:
|
|
53
|
+
"""The key of this message."""
|
|
54
|
+
return getattr(self._matrix_event, "key", None)
|
|
55
|
+
|
|
56
|
+
async def fetch_reactions(self) -> list[Reaction]:
|
|
57
|
+
"""Fetch all reactions for this message.
|
|
58
|
+
|
|
59
|
+
Returns a dict mapping emoji to a list of sender IDs who reacted with it.
|
|
60
|
+
|
|
61
|
+
## Example
|
|
62
|
+
```python
|
|
63
|
+
@bot.command()
|
|
64
|
+
async def reactions(ctx: Context):
|
|
65
|
+
reactions = await ctx.message.fetch_reactions()
|
|
66
|
+
|
|
67
|
+
for emoji, senders in reactions.items():
|
|
68
|
+
await ctx.reply(f"{emoji}: {len(senders)} reaction(s)")
|
|
69
|
+
```
|
|
70
|
+
"""
|
|
71
|
+
raw: dict[str, list[str]] = {}
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
async for event in self.client.room_get_event_relations(
|
|
75
|
+
room_id=self.room.room_id,
|
|
76
|
+
event_id=self.event_id,
|
|
77
|
+
):
|
|
78
|
+
emoji = getattr(event, "key", None)
|
|
79
|
+
sender = getattr(event, "sender", None)
|
|
80
|
+
|
|
81
|
+
if emoji and sender:
|
|
82
|
+
raw.setdefault(emoji, []).append(sender)
|
|
83
|
+
except Exception as e:
|
|
84
|
+
raise MatrixError(f"Failed to fetch reactions: {e}")
|
|
85
|
+
|
|
86
|
+
return [Reaction(key=emoji, senders=senders) for emoji, senders in raw.items()]
|
|
45
87
|
|
|
46
88
|
async def reply(self, body: str) -> "Message":
|
|
47
89
|
"""Reply to this message.
|
|
@@ -57,7 +99,7 @@ class Message:
|
|
|
57
99
|
```
|
|
58
100
|
"""
|
|
59
101
|
try:
|
|
60
|
-
return await self.room.send_text(content=body, reply_to=self.
|
|
102
|
+
return await self.room.send_text(content=body, reply_to=self.event_id)
|
|
61
103
|
except Exception as e:
|
|
62
104
|
raise MatrixError(f"Failed to send reply: {e}")
|
|
63
105
|
|
|
@@ -72,7 +114,7 @@ class Message:
|
|
|
72
114
|
await msg.react("👍")
|
|
73
115
|
```
|
|
74
116
|
"""
|
|
75
|
-
content = ReactionContent(event_id=self.
|
|
117
|
+
content = ReactionContent(event_id=self.event_id, emoji=emoji)
|
|
76
118
|
|
|
77
119
|
try:
|
|
78
120
|
await self.client.room_send(
|
|
@@ -95,7 +137,7 @@ class Message:
|
|
|
95
137
|
await msg.edit("Hello world!")
|
|
96
138
|
```
|
|
97
139
|
"""
|
|
98
|
-
content = EditContent(new_body, original_event_id=self.
|
|
140
|
+
content = EditContent(new_body, original_event_id=self.event_id)
|
|
99
141
|
|
|
100
142
|
try:
|
|
101
143
|
await self.client.room_send(
|
|
@@ -122,10 +164,7 @@ class Message:
|
|
|
122
164
|
try:
|
|
123
165
|
await self.client.room_redact(
|
|
124
166
|
room_id=self.room.room_id,
|
|
125
|
-
event_id=self.
|
|
167
|
+
event_id=self.event_id,
|
|
126
168
|
)
|
|
127
169
|
except Exception as e:
|
|
128
170
|
raise MatrixError(f"Failed to delete message: {e}")
|
|
129
|
-
|
|
130
|
-
def __repr__(self) -> str:
|
|
131
|
-
return f"<Message id={self.id!r} body={self.body!r}>"
|