From 98cc90a501832289bcecdeab34bd673b5a37d203 Mon Sep 17 00:00:00 2001 From: Furoin Date: Tue, 6 Nov 2018 17:56:56 +0300 Subject: [PATCH 1/3] added eval plugin --- plugins/eval/eval.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 plugins/eval/eval.py diff --git a/plugins/eval/eval.py b/plugins/eval/eval.py new file mode 100644 index 0000000..8f7f7e2 --- /dev/null +++ b/plugins/eval/eval.py @@ -0,0 +1,52 @@ +# 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 + +eval_running_text = "**Eval Code:**\n```{code}```\n**Running...**" +eval_success_text = "**Eval Code:**\n```{code}```\n**Success**" +eval_error_text = "**Eval Code:**\n```{code}```\n**Error:**\n```{error}```" +eval_result_text = "**Eval Code:**\n```{code}```\n**Result:**\n```{result}```" + + +@Client.on_message(Filters.command("eval", prefix="!")) +def evalcode(client, message): + code = " ".join(message.command[1:]) + if code: + m = client.send_message(message.chat.id, eval_running_text.replace('{code}', code), parse_mode="MARKDOWN") + try: + result = eval(code) + + except Exception as e: + client.edit_message_text(message.chat.id, m.message_id, + eval_error_text.replace('{code}', code).replace('{error}', str(e)), + parse_mode="MARKDOWN") + else: + if result: + client.edit_message_text(message.chat.id, m.message_id, + eval_result_text.replace('{code}', code).replace('{result}', str(result)), + parse_mode="MARKDOWN") + + else: + client.edit_message_text(message.chat.id, m.message_id, + eval_success_text.replace('{code}', code), + parse_mode="MARKDOWN") From e83c4d327cef9ff8c3fb25be7e8e7cdefbc2b917 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 8 Nov 2018 21:37:04 +0100 Subject: [PATCH 2/3] Update eval.py --- plugins/eval/eval.py | 50 +++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/plugins/eval/eval.py b/plugins/eval/eval.py index 8f7f7e2..e96d108 100644 --- a/plugins/eval/eval.py +++ b/plugins/eval/eval.py @@ -22,31 +22,37 @@ from pyrogram import Client, Filters -eval_running_text = "**Eval Code:**\n```{code}```\n**Running...**" -eval_success_text = "**Eval Code:**\n```{code}```\n**Success**" -eval_error_text = "**Eval Code:**\n```{code}```\n**Error:**\n```{error}```" -eval_result_text = "**Eval Code:**\n```{code}```\n**Result:**\n```{result}```" +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 evalcode(client, message): - code = " ".join(message.command[1:]) - if code: - m = client.send_message(message.chat.id, eval_running_text.replace('{code}', code), parse_mode="MARKDOWN") - try: - result = eval(code) +def eval_expression(client, message): + expression = " ".join(message.command[1:]) - except Exception as e: - client.edit_message_text(message.chat.id, m.message_id, - eval_error_text.replace('{code}', code).replace('{error}', str(e)), - parse_mode="MARKDOWN") - else: - if result: - client.edit_message_text(message.chat.id, m.message_id, - eval_result_text.replace('{code}', code).replace('{result}', str(result)), - parse_mode="MARKDOWN") + 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(message.chat.id, m.message_id, - eval_success_text.replace('{code}', code), - parse_mode="MARKDOWN") + client.edit_message_text( + m.chat.id, + m.message_id, + RESULT.format(expression, result) + ) From f11879e7cd6b3ab0947f59ca766d0b2f25847a22 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 8 Nov 2018 21:50:55 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2e831c9..7d1c5ae 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,4 @@ You found a bug on a plugin or want to extend one? Or maybe you have ideas on ho 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` | MIT +[**eval**](plugins/eval), by [Furoin](//github.com/Furoin) | Evaluate a Python expression and send the result | Example: `!eval 1+2+3` | MIT