10000 Merge pull request #1 from Furoin/dev · pyrogram/plugins@bd64d1f · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Dec 22, 2021. It is now read-only.

Commit bd64d1f

Browse files
authored
Merge pull request #1 from Furoin/dev
Add Eval plugin
2 parents a6c4c78 + f989195 commit bd64d1f

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ You found a bug on a plugin or want to extend one? Or maybe you have ideas on ho
3737

3838
## Plugins Collection
3939

40-
Name | Description | Usage
41-
:--- | :--- | :---
40+
Name | Description | Usage | License
41+
:--- | :--- | :--- | :---
4242
[**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`
4343
[**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
44-
[**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 <old>/<new>`
44+
[**eval**](plugins/eval), by [Furoin](//github.com/Furoin) | Evaluate a Python expression and send the result | Example: `!eval 1+2+3`
45+
[**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 <old>/<new>`

plugins/eval/eval.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2018 Furoin <https://github.com/furoin>
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
from pyrogram import Client, Filters
24+
25+
RUNNING = "**Eval Expression:**\n```{}```\n**Running...**"
26+
ERROR = "**Eval Expression:**\n```{}```\n**Error:**\n```{}```"
27+
SUCCESS = "**Eval Expression:**\n```{}```\n**Success**"
28+
RESULT = "**Eval Expression:**\n```{}```\n**Result:**\n```{}```"
29+
30+
31+
@Client.on_message(Filters.command("eval", prefix="!"))
32+
def eval_expression(client, message):
33+
expression = " ".join(message.command[1:])
34+
35+
if expression:
36+
m = message.reply(RUNNING.format(expression))
37+
38+
try:
39+
result = eval(expression)
40+
except Exception as error:
41+
client.edit_message_text(
42+
m.chat.id,
43+
m.message_id,
44+
ERROR.format(expression, error)
45+
)
46+
else:
47+
if result is None:
48+
client.edit_message_text(
49+
m.chat.id,
50+
m.message_id,
51+
SUCCESS.format(expression)
52+
)
53+
else:
54+
client.edit_message_text(
55+
m.chat.id,
56+
m.message_id,
57+
RESULT.format(expression, result)
58+
)

0 commit comments

Comments
 (0)
0