diff --git a/README.md b/README.md index 62b7d0a..99a4dd0 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,9 @@ You found a bug on a plugin or want to extend one? Or maybe you have ideas on ho ## Plugins Collection -Name | Description | Usage -:--- | :--- | :--- +Name | Description | Usage | License +:--- | :--- | :--- | :--- [**haste**](plugins/haste), by [delivrance](//github.com/delivrance) | Upload text to hastebin.com and send its link | Reply to a group chat text message with `!haste` [**welcome**](plugins/welcome), by [delivrance](//github.com/delivrance) | Greet new members with a welcome message | Run and wait for new members to join your groups -[**replace**](plugins/replace), by [brightside](//github.com/bright5ide) | Search and Replace a part of a message to suggest user if he meant something else | Reply to a group chat text message with `!r /` \ No newline at end of file +[**eval**](plugins/eval), by [Furoin](//github.com/Furoin) | Evaluate a Python expression and send the result | Example: `!eval 1+2+3` +[**replace**](plugins/replace), by [brightside](//github.com/bright5ide) | Search and Replace a part of a message to suggest user if he meant something else | Reply to a group chat text message with `!r /` diff --git a/plugins/eval/eval.py b/plugins/eval/eval.py new file mode 100644 index 0000000..e96d108 --- /dev/null +++ b/plugins/eval/eval.py @@ -0,0 +1,58 @@ +# MIT License +# +# Copyright (c) 2018 Furoin +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from pyrogram import Client, Filters + +RUNNING = "**Eval Expression:**\n```{}```\n**Running...**" +ERROR = "**Eval Expression:**\n```{}```\n**Error:**\n```{}```" +SUCCESS = "**Eval Expression:**\n```{}```\n**Success**" +RESULT = "**Eval Expression:**\n```{}```\n**Result:**\n```{}```" + + +@Client.on_message(Filters.command("eval", prefix="!")) +def eval_expression(client, message): + expression = " ".join(message.command[1:]) + + if expression: + m = message.reply(RUNNING.format(expression)) + + try: + result = eval(expression) + except Exception as error: + client.edit_message_text( + m.chat.id, + m.message_id, + ERROR.format(expression, error) + ) + else: + if result is None: + client.edit_message_text( + m.chat.id, + m.message_id, + SUCCESS.format(expression) + ) + else: + client.edit_message_text( + m.chat.id, + m.message_id, + RESULT.format(expression, result) + )