Adding autocodeblocking#15
Conversation
| """ | ||
| Auto codeblock python code | ||
| """ | ||
| if msg.content.count("\n") >= 3: # more than three lines |
There was a problem hiding this comment.
Didn't we agree that it was over 3?
There was a problem hiding this comment.
Three or more counts of \n should mean over 3 lines, unless the user includes trailing newlines.
|
|
||
| async def on_message(self, msg: Message): | ||
| """ | ||
| Auto codeblock python code |
There was a problem hiding this comment.
Is this docstring necessary? In future it may not just be used for parsing Python code.
There was a problem hiding this comment.
Yes, one could, of course, delete it but just for current usage. Depends on you del, not del?
There was a problem hiding this comment.
If you look at the other files where event listeners are defined they don't have docstrings, to stay compliant, this should be removed.
| 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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Regarding previous comment, the lowercase p was referring to the syntax block.
"```python\n..."There was a problem hiding this comment.
We use f-strings all over the bot, this should also be an f-string.
| 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 |
There was a problem hiding this comment.
This inline comment seems unnecessary, maybe have a comment underneath describing the check more clearly?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
May I suggest # Attempts to parse the message into an AST node.
# Invalid Python code will raise a SyntaxError.
| 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 |
There was a problem hiding this comment.
This inline comment still bugs me. Try moving it to a new line and changing the comment to be more descriptive?
| # 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 |
There was a problem hiding this comment.
# we dont want hi\nthere\nguys\nD is what I meant. Try removing it and adding a new comment underneath
There was a problem hiding this comment.
Something like:
# Single-line words are interpreted as expressions.
# This checks if all nodes were parsed as expressions.
swedgwood
left a comment
There was a problem hiding this comment.
I don't know much about AST so I commented on what I can, but that send_message() needs to change
|
|
||
| await ctx.invoke(self.info) | ||
|
|
||
| async def on_message(self, msg: Message): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I think it's fine for now.
| # 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)) |
There was a problem hiding this comment.
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?
lemonsaurus
left a comment
There was a problem hiding this comment.
minor readability and consistency changes required.
| # 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 |
There was a problem hiding this comment.
is this inline comment still necessary?
| # 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)) |
There was a problem hiding this comment.
this should be an f-string to be consistent with the rest of the repo.
| await ctx.invoke(self.info) | ||
|
|
||
| async def on_message(self, msg: Message): | ||
| if msg.content.count("\n") >= 3: # more than three lines |
There was a problem hiding this comment.
this inline comment states the obvious, which PEP8 specifically says not to do.
| # 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: | ||
| ''' |
There was a problem hiding this comment.
Is a docstring really necessary here?
There was a problem hiding this comment.
How else would one do multiline comments without
#
#
#
or should i use that?
There was a problem hiding this comment.
You don't really need an example. I think just explaining what the check does should suffice.
There was a problem hiding this comment.
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.
marked as outdated, reposting so it isnt marked as 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)) |
There was a problem hiding this comment.
This line still needs an f-string...
| # 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)) |
There was a problem hiding this comment.
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)
| 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: |
There was a problem hiding this comment.
Less than or equal to 2000, not more
| 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 |
There was a problem hiding this comment.
This comment is still in the wrong place. Comments should not be placed after the line they document.
|
|
||
| def __init__(self, bot: AutoShardedBot): | ||
| self.bot = bot | ||
| self.code_block_channels = {303906576991780866: 0, 303906556754395136: 0, 303906514266226689: 0, 267624335836053506: 0} |
There was a problem hiding this comment.
I still want this line multilined.
There was a problem hiding this comment.
I agree. This line is way too long.
| # 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, |
There was a problem hiding this comment.
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)
| # 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" |
There was a problem hiding this comment.
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"
| 303906556754395136: 0, | ||
| 303906514266226689: 0, | ||
| 267624335836053506: 0 | ||
| } |
There was a problem hiding this comment.
Try moving this closing parenthesis to right after the 0.
Reason: Travis
| 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 |
There was a problem hiding this comment.
Either have two imports on each line, or have three on each line. It'll make things look nicer, promise!
There was a problem hiding this comment.
hmmmmmmmmmmmmmmmmmmmmmmmmm
There was a problem hiding this comment.
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.
| HELP1_CHANNEL = 303906576991780866 | ||
| HELP2_CHANNEL = 303906556754395136 | ||
| HELP3_CHANNEL = 303906514266226689 | ||
| PYTHON_CHANNEL = 267624335836053506 |
There was a problem hiding this comment.
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
| 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 |
There was a problem hiding this comment.
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.
| "```\n" | ||
| "print(\"Hello world!\")" | ||
| "```" | ||
| ) # noqa. E124 |
There was a problem hiding this comment.
consistent end bracket indentation please. this should be on howto's level.
|
|
||
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
this I don't agree with. multiple imports are fine.
There was a problem hiding this comment.
understandable, have a nice day
lemonsaurus
left a comment
There was a problem hiding this comment.
this will not do, sir.
| DEVLOG_CHANNEL = 409308876241108992 | ||
| VERIFICATION_CHANNEL = 352442727016693763 | ||
|
|
||
|
|
There was a problem hiding this comment.
This line is unacceptable. Requested changes.
| DEVLOG_CHANNEL = 409308876241108992 | ||
| VERIFICATION_CHANNEL = 352442727016693763 | ||
|
|
||
|
|
There was a problem hiding this comment.
This line is unacceptable. Requested changes.
jerbob
left a comment
There was a problem hiding this comment.
Nicely done. This is a great first contribution.
|
We've murdered this PR and you put up with it. Well done. The code looks great now. |
|
hurray |
Autodetecting python code via checking if