Repository navigation
Update commands examples #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d2d2417
removed empty test file
chrisdedman f0a5524
refactor examples on shutdown
chrisdedman 370c098
added example of error handling of shutdown command
chrisdedman 37708b3
ran black and mypy on examples functionalities
chrisdedman 2c639d2
Added room to matrix.py import
chrisdedman 0891a2a
rename file
chrisdedman 763d0f8
refactor cooldown example and address PR comments
chrisdedman 529d868
added REAMDE documentation to explain how to run the examples
chrisdedman 8231d5e
Updated config file path for examples
chrisdedman c314cf9
removed dead code
chrisdedman 5223e57
refactor example check to add error handling if user not allowed to r…
chrisdedman e4943a9
remove debug print statements from cooldown error handlers
chrisdedman ad32b50
remove debug print statements from error handling and ping examples
chrisdedman 2877c0b
remove debug print statements from scheduler example
chrisdedman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| ## Functionality Examples | ||
| This directory contains multiple examples demonstrating how to use different | ||
| features of the **[matrix.py]** library. Each example is self-contained and can be | ||
| run independently. | ||
|
|
||
| ### How to Run the Examples | ||
| - You will first need to update the environment variables in the [config.yaml](config.yaml) | ||
| file with your Matrix credentials and desired settings. | ||
|
|
||
| - To run any of the examples, navigate to the `examples` directory from your | ||
| command line and execute the desired Python file using the command line. | ||
| For instance: | ||
|
|
||
| ```bash | ||
| python3 error_handling.py | ||
| ``` | ||
|
|
||
| Make sure you have all the necessary dependencies installed and configured | ||
| before running the examples as instructed in the root [README file](../README.md). | ||
|
|
||
| ### Available Examples | ||
| - [error_handling.py](error_handling.py): Demonstrates how to handle errors in commands. | ||
| - [cooldown.py](cooldown.py): Shows how to implement cooldowns for commands. | ||
| - [ping.py](ping.py): A simple ping command example. | ||
| - [reaction.py](reaction.py): Example of handling reactions to messages. | ||
| - [checks.py](checks.py): Demonstrates how to use checks for command execution. | ||
| - [scheduler.py](scheduler.py): Shows how to schedule tasks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,26 @@ | ||
| from matrix import Bot, Context | ||
| from matrix.errors import CheckError | ||
|
|
||
| bot = Bot("examples/config.yaml") | ||
| bot = Bot(config="config.yaml") | ||
|
|
||
| allowed_users = {"@alice:matrix.org", "@bob:matrix.org"} | ||
|
|
||
|
|
||
| @bot.command("secret") | ||
| async def secret_command(ctx: Context): | ||
| @bot.command(name="secret") | ||
| async def secret_command(ctx: Context) -> None: | ||
| await ctx.reply("🎉 Welcome to the secret club!") | ||
|
|
||
|
|
||
| @secret_command.check | ||
| async def is_allowed_user(ctx: Context) -> bool: | ||
| if ctx.sender in allowed_users: | ||
| return True | ||
| await ctx.reply("You do not have permission to use this command.") | ||
| return False | ||
|
|
||
|
|
||
| @secret_command.error(CheckError) | ||
| async def permission_error_handler(ctx: Context, error: CheckError) -> None: | ||
| await ctx.reply(f"Access denied: {error}") | ||
|
|
||
|
|
||
| bot.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
|
chrisdedman marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from matrix import Bot, Context | ||
| from matrix.errors import CommandNotFoundError, MissingArgumentError | ||
|
|
||
| bot = Bot(config="config.yaml") | ||
|
|
||
|
|
||
| @bot.error(CommandNotFoundError) | ||
| async def global_error(error: CommandNotFoundError) -> None: | ||
| print(f"Global error handler {error}.") | ||
|
|
||
|
|
||
| @bot.command("div") | ||
| async def division(ctx: Context, a: int, b: int) -> None: | ||
| c = a / b | ||
| await ctx.reply(f"{a} / {b} = {c}") | ||
|
|
||
|
|
||
| @division.error(ZeroDivisionError) | ||
| async def div_error(ctx: Context, error: ZeroDivisionError) -> None: | ||
| await ctx.reply(f"Operation not allowed: {error}") | ||
|
|
||
|
|
||
| @division.error(ValueError) | ||
| async def val_error(ctx: Context, error: ValueError) -> None: | ||
| await ctx.reply(f"ValueError: {error}") | ||
|
|
||
|
|
||
| @division.error(MissingArgumentError) | ||
| async def command_error(ctx: Context, error: MissingArgumentError) -> None: | ||
| await ctx.reply(f"{error}") | ||
|
|
||
|
|
||
| bot.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.