Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source Command #485

Closed
wants to merge 4 commits into from
Closed

Source Command #485

wants to merge 4 commits into from

Conversation

RohanJnr
Copy link
Member

@RohanJnr RohanJnr commented Oct 2, 2019

closes #331

Source command implemented as said in the issue.
The following is a screenshot of it being implemented:

sourcecmd

Copy link
Member

@SebastiaanZ SebastiaanZ left a comment

Choose a reason for hiding this comment

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

Hey @RohanJnr,

Like mentioned in the issue, the standard approach does not work consistently for our bot, as it does not always return the right starting line and/or code length. I have not looked into why it's happening, although it may be because of decorators passing the function around.

As an example, issuing !source help sends me to https://github.com/python-discord/bot/blob/master/bot/cogs/help.py#L108-L140, which is obviously not where we want to end up. Help may be the only one for which this happens, but I haven't extensively tested all commands yet.

One additional problem is way you reconstruct the path to the .py file: We've recently started using a subpackage approach (directory with __init__.py) to group related functionality together. The command currently does not take that into account and expects all of the extension files to live directly in bot/cogs/.

As an example, issuing !source superstarify yields the link https://github.com/python-discord/bot/tree/master/bot/cogs/superstarify.py#L148-L219. This file does not exist and the function is actually located here: https://github.com/python-discord/bot/blob/master/bot/cogs/superstarify/__init__.py#L148-L219. (Notice that the line numbers are correct in this case.)

I think that if we do add this to the bot, the approach should be robust and not suffer from these edge cases. Additionally, I think that you should consider adding support for subcommands (!source docs get now sends you to the source code for docs, not the get subcommand) and restricting the command to channels like #bot-commands.

I do think the code you wrote is clean and easy to read and I think the functionality would be cool to have.

@RohanJnr
Copy link
Member Author

RohanJnr commented Oct 2, 2019

Hey @RohanJnr,

Like mentioned in the issue, the standard approach does not work consistently for our bot, as it does not always return the right starting line and/or code length. I have not looked into why it's happening, although it may be because of decorators passing the function around.

As an example, issuing !source help sends me to https://github.com/python-discord/bot/blob/master/bot/cogs/help.py#L108-L140, which is obviously not where we want to end up. Help may be the only one for which this happens, but I haven't extensively tested all commands yet.

One additional problem is way you reconstruct the path to the .py file: We've recently started using a subpackage approach (directory with __init__.py) to group related functionality together. The command currently does not take that into account and expects all of the extension files to live directly in bot/cogs/.

As an example, issuing !source superstarify yields the link https://github.com/python-discord/bot/tree/master/bot/cogs/superstarify.py#L148-L219. This file does not exist and the function is actually located here: https://github.com/python-discord/bot/blob/master/bot/cogs/superstarify/__init__.py#L148-L219. (Notice that the line numbers are correct in this case.)

I think that if we do add this to the bot, the approach should be robust and not suffer from these edge cases. Additionally, I think that you should consider adding support for subcommands (!source docs get now sends you to the source code for docs, not the get subcommand) and restricting the command to channels like #bot-commands.

I do think the code you wrote is clean and easy to read and I think the functionality would be cool to have.

Thanks for the suggestion! will work on improving it.

@sco1 sco1 added area: cogs t: feature New feature or request labels Oct 2, 2019
@RohanJnr
Copy link
Member Author

RohanJnr commented Oct 3, 2019

btw by subcommands, do u mean commands within a group?

@RohanJnr
Copy link
Member Author

RohanJnr commented Oct 3, 2019

the only pending work is the pathfinding for the help command. I am not sure how to go about it. The subcommands URL generation works fine except for the tags group, some weared stuff happening. Maybe you can look through my code and find some fixes?

@commands.command(name="source")
async def command_source(self, ctx: commands.Context, command_name: str = None) -> None:
async def command_source(self, ctx: commands.Context, command_name: str = None, sub_command: str = None) -> None:
Copy link

@nekitdev nekitdev Oct 4, 2019

Choose a reason for hiding this comment

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

At the current state, only one command and one subcommand are supported. Why couldn't we do this? : async def command_source(self, ctx: commands.Context, *, command_name: str = None) -> None: This way, end-users can enter anything they wish.

"""View the source of a command."""
if command_name is None:
return await ctx.send("> https://github.com/python-discord/bot")
await ctx.send("> https://github.com/python-discord/bot")
Copy link

@nekitdev nekitdev Oct 4, 2019

Choose a reason for hiding this comment

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

Not sure why wouldn't we do return await ctx.send(...) in one line honestly, but ok if there is a reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we're annotating that our function returns None then it should return None, not maybe None

Copy link

Choose a reason for hiding this comment

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

Okay, I have just checked in discord.py. send returns a discord.Message instance, well then, we should return None.


command = self.bot.get_command(command_name)
Copy link

@nekitdev nekitdev Oct 4, 2019

Choose a reason for hiding this comment

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

Instead of that sub_command usage, we could do self.bot.get_command(command_name) (almost like before it was deleted, but with new function arguments).

Copy link

@nekitdev nekitdev left a comment

Choose a reason for hiding this comment

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

I have added some suggestions. Planning to do several tests now.

"""Make up the url for the source of the command."""
# Get the source code
src_code_object = command.callback.__code__
file_path = src_code_object.co_filename[5:]
Copy link

Choose a reason for hiding this comment

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

Rapptz's implementation of getting the location was like this: file_name = src_code_object.co_filename and file_path = os.path.relpath(file_name).replace('\\', '/'). I think, should work; will do some tests in a bit.

embed = discord.Embed(colour=discord.Colour.red())
embed.title = "Command Source"
embed.description = f"**{command.name.capitalize()}**\n"
embed.description += f"`{prefix}{command.name}`\n\n {url}"
Copy link

Choose a reason for hiding this comment

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

Shouldn't we send command.qualified_name in the embed?

@RohanJnr
Copy link
Member Author

RohanJnr commented Oct 4, 2019

Updated

if command is None:
await ctx.send("No such command found.")
return

url = self.get_command_url(command)
if command.name == "help":
url = "https://github.com/python-discord/bot/blob/master/bot/cogs/help.py#L475-L490"
Copy link

Choose a reason for hiding this comment

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

In my opinion, we would better send a link without the lines here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Which lines

Copy link

Choose a reason for hiding this comment

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

The #L475-L490 should not be hard-coded in my opinion. I think we should just return the link to the help.py file.

Copy link
Member

Choose a reason for hiding this comment

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

I think we should just return the correct lines, but without hardcoding them.

@RohanJnr we can't have any hardcoded edge cases in this solution. Please try to find a solution that solves this without hardcoding.

@nekitdev
Copy link

nekitdev commented Oct 4, 2019

All in all, @SebastiaanZ I think we are ready to merge (though, not sure if we should send the lines in explicit !source help implementation.

MarkKoz pushed a commit that referenced this pull request Oct 4, 2019
Related to #458

This commit adds a converter that automatically parses ISO-formatted
datetime strings and returns a `datetime.datetime` object. It uses
`dateutil.parser.isoparse` to do the heavy lifting, so it supports
the same formats as this method.

In addition, I have added tests that ensure that it accepts certain
formats and added a description of these 'guaranteed' formats to the
`ISODate.convert` docstring.

This commit should make it easy to implement #485
Copy link
Member

@SebastiaanZ SebastiaanZ left a comment

Choose a reason for hiding this comment

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

This PR is out of date with master and has some conflicts that need to be resolved. @lemonsaurus also rightly noted that we should rather have a solution that's robust instead of one that uses hard coded links for edge cases. Hard coded links will need to be maintained by hand and makes me wonder if more edge cases are going to come up in the future.

@jchristgit jchristgit assigned jchristgit and unassigned jchristgit Oct 11, 2019
@RohanJnr
Copy link
Member Author

what if I just return the file name for the help command? I will not hard code the file link tho, I can get it by the code.

@lemonsaurus
Copy link
Member

Returning only the file name for the help command seems like a cheap solution. I think that this is solvable without hardcoding any of the replies. Given how stale this pull request is and how many unaddressed reviews are in it, I'm tempted to just give this task to someone else if you can't come up with an agnostic solution.

@RohanJnr RohanJnr added the invalid This doesn't seem right label Oct 21, 2019
@RohanJnr
Copy link
Member Author

okay, Thanks for the chance, I am not able to get the correct line numbers from help file :( . Marking this invalid.

@sco1 sco1 closed this Oct 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right t: feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Source command.
6 participants