8000 added eval plugin by Furoin · Pull Request #1 · pyrogram/plugins · 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.

added eval plugin #1

Merged
merged 4 commits into from
Nov 8, 2018
Merged

added eval plugin #1

merged 4 commits into from
Nov 8, 2018

Conversation

Furoin
Copy link
Contributor
@Furoin Furoin commented Nov 6, 2018

No description provided.

@delivrance
Copy link
Member

Hi. Add yourself in the readme

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}```"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these to be UPPER_CASE

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use replace, use format instead.
Also, you could just use message.reply here

else:
client.edit_message_text(message.chat.id, m.message_id,
eval_success_text.replace('{code}', code),
parse_mode="MARKDOWN")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Markdown is enabled by default, there's no need for it.

Copy link
Member
@delivrance delivrance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are quite a few fixes needed to make this plugin nicer. Here's how I suggest it to be instead (there's still some redundant code because of message.edit() not being available (yet), but we can add it later):

from pyrogram import Client, Filters

RUNNING = "**Eval Code:**\n```{}```\n**Running...**"
ERROR = "**Eval Code:**\n```{}```\n**Error:**\n```{}```"
SUCCESS = "**Eval Code:**\n```{}```\n**Success**"
RESULT = "**Eval Code:**\n```{}```\n**Result:**\n```{}```"


@Client.on_message(Filters.command("eval", prefix="!"))
def eval_code(client, message):
    code = " ".join(message.command[1:])

    if code:
        m = message.reply(RUNNING.format(code))

        try:
            result = eval(code)
        except Exception as error:
            client.edit_message_text(
                m.chat.id,
                m.message_id,
                ERROR.format(code, error)
            )
        else:
            if result is None:
                client.edit_message_text(
                    m.chat.id,
                    m.message_id,
                    SUCCESS.format(code)
                )
            else:
                client.edit_message_text(
                    m.chat.id,
                    m.message_id,
                    RESULT.format(code, result)
                )

@delivrance
Copy link
Member
delivrance commented Nov 7, 2018

And here's how the plugin will look like as soon as Message.edit() is added. Nice and clean:

from pyrogram import Client, Filters

RUNNING = "**Eval Code:**\n```{}```\n**Running...**"
ERROR = "**Eval Code:**\n```{}```\n**Error:**\n```{}```"
SUCCESS = "**Eval Code:**\n```{}```\n**Success**"
RESULT = "**Eval Code:**\n```{}```\n**Result:**\n```{}```"


@Client.on_message(Filters.command("eval", prefix="!"))
def eval_code(client, message):
    code = " ".join(message.command[1:])

    if code:
        reply = message.reply(RUNNING.format(code))

        try:
            result = eval(code)
        except Exception as error:
            reply.edit(ERROR.format(code, error))
        else:
            if result is None:
                reply.edit(SUCCESS.format(code))
            else:
                reply.edit(RESULT.format(code, result))

@Sunda001
Copy link
Sunda001 commented Nov 8, 2018

Wow

@delivrance delivrance merged commit bd64d1f into pyrogram:master Nov 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0