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
Source Command #485
Conversation
… the cog in __main__.py
There was a problem hiding this 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.
Thanks for the suggestion! will work on improving it. |
btw by subcommands, do u mean commands within a group? |
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? |
bot/cogs/source.py
Outdated
@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: |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
.
bot/cogs/source.py
Outdated
|
||
command = self.bot.get_command(command_name) |
There was a problem hiding this comment.
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).
There was a problem hiding this 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.
bot/cogs/source.py
Outdated
"""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:] |
There was a problem hiding this comment.
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.
bot/cogs/source.py
Outdated
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}" |
There was a problem hiding this comment.
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?
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" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which lines
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
All in all, @SebastiaanZ I think we are ready to merge (though, not sure if we should send the lines in explicit |
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
There was a problem hiding this 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.
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. |
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. |
okay, Thanks for the chance, I am not able to get the correct line numbers from help file :( . Marking this invalid. |
closes #331
Source command implemented as said in the issue.
The following is a screenshot of it being implemented: