Skip to content

Adding autocodeblocking#15

Merged
lemonsaurus merged 49 commits into
python-discord:masterfrom
hargoniX:master
Feb 26, 2018
Merged

Adding autocodeblocking#15
lemonsaurus merged 49 commits into
python-discord:masterfrom
hargoniX:master

Conversation

@hargoniX
Copy link
Copy Markdown
Contributor

Autodetecting python code via checking if

  • it has at least three lines
  • is not throwing syntax errors when parsed with ast.parse
  • is not only ast.Expr

Comment thread bot/cogs/bot.py Outdated
"""
Auto codeblock python code
"""
if msg.content.count("\n") >= 3: # more than three lines
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Didn't we agree that it was over 3?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Three or more counts of \n should mean over 3 lines, unless the user includes trailing newlines.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ahh yeah, my bad

Comment thread bot/cogs/bot.py Outdated

async def on_message(self, msg: Message):
"""
Auto codeblock python code
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this docstring necessary? In future it may not just be used for parsing Python code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, one could, of course, delete it but just for current usage. Depends on you del, not del?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you look at the other files where event listeners are defined they don't have docstrings, to stay compliant, this should be removed.

Comment thread bot/cogs/bot.py Outdated
try:
tree = ast.parse(msg.content) # no syntax errors
if not all(isinstance(node, ast.Expr) for node in tree.body): # we dont want hi\nthere\nguys\nD
await self.bot.send_message(msg.author, "```Python\n{}\n```".format(msg.content))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should technically be a lower case p, as well as this we should probably either delete the message, or, have it send the current text in the codeblock tag.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Regarding previous comment, the lowercase p was referring to the syntax block.

"```python\n..."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We use f-strings all over the bot, this should also be an f-string.

Comment thread bot/cogs/bot.py Outdated
if msg.content.count("\n") >= 3: # more than three lines
try:
tree = ast.parse(msg.content) # no syntax errors
if not all(isinstance(node, ast.Expr) for node in tree.body): # we dont want hi\nthere\nguys\nD
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This inline comment seems unnecessary, maybe have a comment underneath describing the check more clearly?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Inline comments never really look nice, and a couple here are probably un-needed, especially after the if statement and the tree definition. Moving them above or underneath a line, and adding newlines around that is a better idea.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

alright

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your comments should follow PEP8 - they should start with a capital letter, and need to be grammatically correct. That means you're gonna need to use some punctuation. This comment, for example, deseperately needs a period and a comma.

Comment thread bot/cogs/bot.py Outdated
if msg.content.count("\n") >= 3: # more than three lines
try:
tree = ast.parse(msg.content)
# tries to parse the code into a valid ast if it throws a SyntaxError it is no valid python
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

May I suggest # Attempts to parse the message into an AST node.
# Invalid Python code will raise a SyntaxError.

Comment thread bot/cogs/bot.py Outdated
tree = ast.parse(msg.content)
# Attempts to parse the message into an AST node.
# Invalid Python code will raise a SyntaxError.
if not all(isinstance(node, ast.Expr) for node in tree.body): # we dont want hi\nthere\nguys\nD
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This inline comment still bugs me. Try moving it to a new line and changing the comment to be more descriptive?

Comment thread bot/cogs/bot.py Outdated
# Attempts to parse the message into an AST node.
# Invalid Python code will raise a SyntaxError.
# => text which is not Python code will be filtered.
if not all(isinstance(node, ast.Expr) for node in tree.body): # we dont want hi\nthere\nguys\nD
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

# we dont want hi\nthere\nguys\nD is what I meant. Try removing it and adding a new comment underneath

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Something like:
# Single-line words are interpreted as expressions.
# This checks if all nodes were parsed as expressions.

Copy link
Copy Markdown
Contributor

@swedgwood swedgwood left a comment

Choose a reason for hiding this comment

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

I don't know much about AST so I commented on what I can, but that send_message() needs to change

Comment thread bot/cogs/bot.py

await ctx.invoke(self.info)

async def on_message(self, msg: Message):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is having this directly in bot.py a good idea? It might be better to have it elsewhere and be imported, for readability. ( And have that docstring moved there )
But thats open to discussion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's fine for now.

Comment thread bot/cogs/bot.py Outdated
# Attempts to parse the message into an AST node.
# Invalid Python code will raise a SyntaxError.
if not all(isinstance(node, ast.Expr) for node in tree.body): # we dont want hi\nthere\nguys\nD
await self.bot.send_message(msg.author, "```python\n{}\n```".format(msg.content))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

send_message() is pre-rewrite, and I'm pretty sure you want to send it to the channel, so the rewrite version would be msg.channel.send(<message here>)
Also if the message (with the unformatted python code) were >1986 characters this line would throw an error, maybe add a check against this?

Copy link
Copy Markdown
Contributor

@lemonsaurus lemonsaurus left a comment

Choose a reason for hiding this comment

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

minor readability and consistency changes required.

Comment thread bot/cogs/bot.py Outdated
# Attempts to parse the message into an AST node.
# Invalid Python code will raise a SyntaxError.
# => text which is not Python code will be filtered.
if not all(isinstance(node, ast.Expr) for node in tree.body): # we dont want hi\nthere\nguys\nD
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this inline comment still necessary?

Comment thread bot/cogs/bot.py Outdated
# Invalid Python code will raise a SyntaxError.
# => text which is not Python code will be filtered.
if not all(isinstance(node, ast.Expr) for node in tree.body): # we dont want hi\nthere\nguys\nD
await self.bot.send_message(msg.author, "```python\n{}\n```".format(msg.content))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this should be an f-string to be consistent with the rest of the repo.

Comment thread bot/cogs/bot.py Outdated
await ctx.invoke(self.info)

async def on_message(self, msg: Message):
if msg.content.count("\n") >= 3: # more than three lines
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this inline comment states the obvious, which PEP8 specifically says not to do.

Comment thread bot/cogs/bot.py Outdated
# Invalid Python code will raise a SyntaxError.
if not all(isinstance(node, ast.Expr) for node in tree.body):
# we dont want multiple lines of single words like:
'''
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is a docstring really necessary here?

Copy link
Copy Markdown
Contributor Author

@hargoniX hargoniX Feb 25, 2018

Choose a reason for hiding this comment

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

How else would one do multiline comments without
#
#
#
or should i use that?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You don't really need an example. I think just explaining what the check does should suffice.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yeah don't use docstrings like this. if you did need a comment of that many lines, you should have used multiple # comments, but I agree with Ap that you don't really need that in this case. Just say it with words.

@swedgwood swedgwood dismissed their stale review February 25, 2018 22:28

marked as outdated, reposting so it isnt marked as outdated

Comment thread bot/cogs/bot.py Outdated
# that would be syntactically valid Python but in this case
# just some random multiline text someone is sending.

await msg.channel.send("```python\n{}\n```".format(msg.content))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This line still needs an f-string...

Comment thread bot/cogs/bot.py Outdated
# that would be syntactically valid Python but in this case
# just some random multiline text someone is sending.

await msg.channel.send("```python\n{}\n```".format(msg.content))
Copy link
Copy Markdown
Contributor

@swedgwood swedgwood Feb 25, 2018

Choose a reason for hiding this comment

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

If msg.content were >1986 characters long, discord will get angry at you as the message exceeds 2000 characters, to avoid unnecessary errors I suggest some sort of check against this? Maybe format the string, then check if it is over 2000 characters before sending and deleting (Oh yeah also use f-strings, consistency ftw)

Comment thread bot/cogs/bot.py Outdated
await msg.channel.send(f"```python\n{msg.content}\n```")
await self.bot.delete_message(msg)
formatted = f"```python\n{msg.content}\n```"
if len(formatted) > 2000:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Less than or equal to 2000, not more

Comment thread bot/cogs/bot.py
def __init__(self, bot: AutoShardedBot):
self.bot = bot
self.code_block_channels = {303906576991780866: 0, 303906556754395136: 0, 303906514266226689: 0, 267624335836053506: 0}
# Stores allowed channels plus unix timestamp from last call
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment is still in the wrong place. Comments should not be placed after the line they document.

Comment thread bot/cogs/bot.py Outdated

def __init__(self, bot: AutoShardedBot):
self.bot = bot
self.code_block_channels = {303906576991780866: 0, 303906556754395136: 0, 303906514266226689: 0, 267624335836053506: 0}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I still want this line multilined.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree. This line is way too long.

Comment thread bot/cogs/bot.py Outdated
# Invalid Python code will raise a SyntaxError.
if not all(isinstance(node, ast.Expr) for node in tree.body):

# We don't want multiple lines of single words,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can I suggest having:
# Multiple lines of single words could be interpreted as expressions.
# This check is to avoid all nodes being parsed as expressions.
# (e.g. words over multiple lines)

Comment thread bot/cogs/bot.py Outdated
# Multiple lines of single words could be interpreted as expressions.
# This check is to avoid all nodes being parsed as expressions.
# (e.g. words over multiple lines)
howto = ("Please use syntax highlighted blocks, as it makes it more legible for other users.\n"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This string should be split over two lines. It exceeds 120 characters. Also made a minor edit to the sentence :D

"Please use syntax highlighted blocks, as it makes"
"your code more legible for other users.\n"

Comment thread bot/cogs/bot.py Outdated
303906556754395136: 0,
303906514266226689: 0,
267624335836053506: 0
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Try moving this closing parenthesis to right after the 0.
Reason: Travis

Comment thread bot/cogs/bot.py Outdated
from dulwich.repo import Repo

from bot.constants import PYTHON_GUILD, VERIFIED_ROLE
from bot.constants import HELP1_CHANNEL, HELP2_CHANNEL, HELP3_CHANNEL, PYTHON_CHANNEL, PYTHON_GUILD, VERIFIED_ROLE
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You know what to do.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Either have two imports on each line, or have three on each line. It'll make things look nicer, promise!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmmmmmmmmmmmmmmmmmmmmmmmmm

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we have an equal number of imports spread out over either 2 or 3 lines.
Currently, the line looks too long for use in a production setting.

Comment thread bot/constants.py
HELP1_CHANNEL = 303906576991780866
HELP2_CHANNEL = 303906556754395136
HELP3_CHANNEL = 303906514266226689
PYTHON_CHANNEL = 267624335836053506
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we change this order to:

BOT_CHANNEL
HELP1_CHANNEL
HELP2_CHANNEL
HELP3_CHANNEL
PYTHON_CHANNEL
DEVLOG_CHANNEL
VERIFICATION_CHANNEL

To keep line lengths ascending, thanks

Comment thread bot/cogs/bot.py Outdated
from dulwich.repo import Repo

from bot.constants import PYTHON_GUILD, VERIFIED_ROLE
from bot.constants import HELP1_CHANNEL, HELP2_CHANNEL, HELP3_CHANNEL, PYTHON_CHANNEL, PYTHON_GUILD, VERIFIED_ROLE
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we have an equal number of imports spread out over either 2 or 3 lines.
Currently, the line looks too long for use in a production setting.

Comment thread bot/cogs/bot.py Outdated
"```\n"
"print(\"Hello world!\")"
"```"
) # noqa. E124
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

consistent end bracket indentation please. this should be on howto's level.

Comment thread bot/cogs/bot.py Outdated

from bot.constants import PYTHON_GUILD, VERIFIED_ROLE
from bot.constants import HELP1_CHANNEL, HELP2_CHANNEL, HELP3_CHANNEL
from bot.constants import PYTHON_CHANNEL, PYTHON_GUILD, VERIFIED_ROLE
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's no need to have two import statements. You can spread the imports over 2 or 3 lines without repeating import statements, by using surrounding parentheses.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this I don't agree with. multiple imports are fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

understandable, have a nice day

Copy link
Copy Markdown
Contributor

@lemonsaurus lemonsaurus left a comment

Choose a reason for hiding this comment

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

this will not do, sir.

Comment thread bot/constants.py Outdated
DEVLOG_CHANNEL = 409308876241108992
VERIFICATION_CHANNEL = 352442727016693763


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why is this here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This line is unacceptable. Requested changes.

Comment thread bot/constants.py Outdated
DEVLOG_CHANNEL = 409308876241108992
VERIFICATION_CHANNEL = 352442727016693763


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This line is unacceptable. Requested changes.

Copy link
Copy Markdown
Contributor

@jerbob jerbob left a comment

Choose a reason for hiding this comment

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

Nicely done. This is a great first contribution.

@lemonsaurus
Copy link
Copy Markdown
Contributor

We've murdered this PR and you put up with it. Well done. The code looks great now.

@lemonsaurus lemonsaurus merged commit 0538bca into python-discord:master Feb 26, 2018
@hargoniX
Copy link
Copy Markdown
Contributor Author

hurray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants