[V3 commands] Introduction to Red's converters + TimeConverter #2126

Closed
laggron42 wants to merge 17 commits from timed-mute into V3/develop
laggron42 commented 2018-09-13 20:09:11 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

This PR adds a converters.py file, following d.py's style. This comes with the TimeConverter converter, which converts a string to a datetime.timedelta unit. It can be called manually or be used as a function annotation, like other converters.

You can provide many types of strings and this will be converted to a valid class, thanks @mikeshardmind for the regex thing.

This PR will be needed for the future timed mute, that should be made by me if everything's alright.

Example

  • 5m > timedelta(seconds=300)
  • 10 minutes > timedelta(seconds=600)
  • 1 second > timedelta(seconds=1)
  • 5d > timedelta(days=5)

Documentation

### Type - [ ] Bugfix - [ ] Enhancement - [x] New feature ### Description of the changes This PR adds a `converters.py` file, following d.py's style. This comes with the `TimeConverter` converter, which converts a string to a `datetime.timedelta` unit. It can be called manually or be used as a function annotation, like other converters. You can provide many types of strings and this will be converted to a valid class, thanks @mikeshardmind for the regex thing. This PR will be needed for the future timed mute, that should be made by me if everything's alright. ### Example - `5m` > `timedelta(seconds=300)` - `10 minutes` > `timedelta(seconds=600)` - `1 second` > `timedelta(seconds=1)` - `5d` > `timedelta(days=5)` ### Documentation ![](https://screenshotscdn.firefoxusercontent.com/images/b26967b8-c820-4704-a30a-698bb16c8d89.png)
palmtree5 (Migrated from github.com) reviewed 2018-09-13 20:09:11 +02:00
tekulvw (Migrated from github.com) reviewed 2018-09-13 20:09:11 +02:00
Twentysix26 (Migrated from github.com) reviewed 2018-09-13 20:09:11 +02:00
mikeshardmind (Migrated from github.com) requested changes 2018-09-14 10:18:42 +02:00
mikeshardmind (Migrated from github.com) left a comment

I haven't tested the implementation yet, but I already noticed a few things with it. And next time, careful with those GitHub pings.

I haven't tested the implementation yet, but I already noticed a few things with it. And next time, careful with those GitHub pings.
@ -0,0 +1,66 @@
import re
mikeshardmind (Migrated from github.com) commented 2018-09-14 10:09:10 +02:00

This should probably be TimedeltaConverter

This should probably be `TimedeltaConverter`
mikeshardmind (Migrated from github.com) commented 2018-09-14 10:12:10 +02:00

This is a compiled constant, it belongs outside the class at the top of the file

This is a compiled constant, it belongs outside the class at the top of the file
mikeshardmind (Migrated from github.com) commented 2018-09-14 10:16:55 +02:00

This disallows a zero second timedelta, which could be allowed by checking against if not params earlier in the code instead.

This disallows a zero second timedelta, which could be allowed by checking against `if not params` earlier in the code instead.
Tobotimus (Migrated from github.com) reviewed 2018-09-14 13:30:12 +02:00
@ -0,0 +1,66 @@
import re
Tobotimus (Migrated from github.com) commented 2018-09-14 13:30:12 +02:00

I know this package is a bit confusing due to the way it imports all of discord.py's stuff to the top level, then writes our stuff afterwards.

When importing something we've overwritten from within the actual commands package, import it from the sibling-module, not from the top level. So in this example, it would be Context, you want to do from .context import Context.

Also, whenever you're doing a relative import which is only for a type-hint, please use typing.TYPE_CHECKING and forward references to avoid any circular imports.

I know this package is a bit confusing due to the way it imports all of discord.py's stuff to the top level, then writes our stuff afterwards. When importing something we've overwritten from within the actual `commands` package, import it from the sibling-module, not from the top level. So in this example, it would be `Context`, you want to do `from .context import Context`. Also, whenever you're doing a relative import which is only for a type-hint, please use [`typing.TYPE_CHECKING`](https://docs.python.org/3.7/library/typing.html#typing.TYPE_CHECKING) and forward references to avoid any circular imports.
laggron42 (Migrated from github.com) reviewed 2018-09-14 17:51:41 +02:00
@ -0,0 +1,66 @@
import re
laggron42 (Migrated from github.com) commented 2018-09-14 17:51:41 +02:00

What do you mean? Put arguments outside of the class and keep what's related to time inside the class?

What do you mean? Put arguments outside of the class and keep what's related to time inside the class?
laggron42 (Migrated from github.com) reviewed 2018-09-14 17:55:16 +02:00
@ -0,0 +1,66 @@
import re
laggron42 (Migrated from github.com) commented 2018-09-14 17:55:16 +02:00

But if the user sets, for example 2 weeks, Regex will return 0:00:00, while the user is actually expecting something else. For example, with timed mute, if the user input a wrong unit, what will happen is that the user will get muted and immediately unmuted.

I'm trying to follow discord.py's style for converters, and when it can't convert, it raises BadArgument (instead of returning None or something like that).

But if the user sets, for example `2 weeks`, Regex will return 0:00:00, while the user is actually expecting something else. For example, with timed mute, if the user input a wrong unit, what will happen is that the user will get muted and immediately unmuted. I'm trying to follow `discord.py`'s style for converters, and when it can't convert, it raises `BadArgument` (instead of returning None or something like that).
laggron42 (Migrated from github.com) reviewed 2018-09-14 17:56:48 +02:00
@ -0,0 +1,66 @@
import re
laggron42 (Migrated from github.com) commented 2018-09-14 17:56:48 +02:00

okay for Context, but what about Converter and BadArgument? Should I import discord.ext.commands instead?

okay for `Context`, but what about `Converter` and `BadArgument`? Should I import `discord.ext.commands` instead?
mikeshardmind (Migrated from github.com) reviewed 2018-09-14 19:19:12 +02:00
@ -0,0 +1,66 @@
import re
mikeshardmind (Migrated from github.com) commented 2018-09-14 19:19:11 +02:00

Your handling of this places 0 seconds as invalid, when the intent is to not handle 2 weeks. Meanwhile, if params is an empty dict here, you can raise the bad argument there (having matched nothing expected)

Your handling of this places `0 seconds` as invalid, when the intent is to not handle `2 weeks`. Meanwhile, if `params` is an empty dict here, you can raise the bad argument there (having matched nothing expected)
mikeshardmind (Migrated from github.com) reviewed 2018-09-14 19:22:49 +02:00
@ -0,0 +1,66 @@
import re
mikeshardmind (Migrated from github.com) commented 2018-09-14 19:22:49 +02:00

You shouldn't be calling re.compile each time a conversion happens. Make it a module level constant, but don't place it in __all__'s definition.

You shouldn't be calling `re.compile` each time a conversion happens. Make it a module level constant, but don't place it in `__all__`'s definition.
Zephyrkul (Migrated from github.com) reviewed 2018-09-16 06:56:57 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-16 06:56:57 +02:00

Couple issues with these code blocks.

  1. lines 39 and 47: it's TimedeltaConverter, not Timedelta.
  2. line 48: You're using the class to call an instance's method. You need to use an instance to call an instance method.
Couple issues with these code blocks. 1. lines 39 and 47: it's TimedeltaConverter, not Timedelta. 2. line 48: You're using the class to call an instance's method. You need to use an instance to call an instance method.
Redjumpman (Migrated from github.com) reviewed 2018-09-18 06:12:09 +02:00
@ -0,0 +1,66 @@
import re
Redjumpman (Migrated from github.com) commented 2018-09-18 06:12:09 +02:00

I'm not sure what ctx is doing here. It's not used for the rest of the code in this function, and the "type" checking is just "Context". Did you mean to check for commands.Context ?

I'm not sure what ctx is doing here. It's not used for the rest of the code in this function, and the "type" checking is just "Context". Did you mean to check for `commands.Context` ?
Redjumpman (Migrated from github.com) reviewed 2018-09-18 06:12:59 +02:00
@ -0,0 +1,66 @@
import re
Redjumpman (Migrated from github.com) commented 2018-09-18 06:12:58 +02:00

Can we change this toManually convert a string ... instead of Convert manually a string...?

Can we change this to` Manually convert a string ... ` instead of `Convert manually a string...`?
Redjumpman (Migrated from github.com) reviewed 2018-09-18 06:13:27 +02:00
@ -0,0 +1,66 @@
import re
Redjumpman (Migrated from github.com) commented 2018-09-18 06:13:27 +02:00

'~' character not needed.

'~' character not needed.
Redjumpman (Migrated from github.com) reviewed 2018-09-18 06:16:36 +02:00
@ -0,0 +1,66 @@
import re
Redjumpman (Migrated from github.com) commented 2018-09-18 06:16:36 +02:00

Not sure why dunder all is needed here. TimedeltaConverter is literally the only thing here. Typically you use all to "hide" some of the helper functions that build up the stuff you want developers to use.

Not sure why dunder all is needed here. TimedeltaConverter is literally the only thing here. Typically you use all to "hide" some of the helper functions that build up the stuff you want developers to use.
Redjumpman (Migrated from github.com) reviewed 2018-09-18 06:20:22 +02:00
@ -0,0 +1,66 @@
import re
Redjumpman (Migrated from github.com) commented 2018-09-18 06:20:22 +02:00

This is not a great example of how these deltas could be used. Perhaps change this very ambiguous classmethod to something like await asyncio.sleep(time.seconds)

This is not a great example of how these deltas could be used. Perhaps change this very ambiguous classmethod to something like `await asyncio.sleep(time.seconds)`
Redjumpman commented 2018-09-18 06:22:08 +02:00 (Migrated from github.com)

I added some comments to this PR as well. I think this could be a helpful addition, but it needs some cleaning up.

I added some comments to this PR as well. I think this could be a helpful addition, but it needs some cleaning up.
Zephyrkul (Migrated from github.com) reviewed 2018-09-18 06:25:19 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-18 06:25:19 +02:00

ctx is required for converters that inherit from d.py's Converter. That said, if there's no need for ctx, perhaps just a normal module function would be better. d.py takes any callable as a converter, after all.

ctx is required for converters that inherit from d.py's Converter. That said, if there's no need for ctx, perhaps just a normal module function would be better. d.py takes any callable as a converter, after all.
Zephyrkul (Migrated from github.com) reviewed 2018-09-18 06:26:55 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-18 06:26:55 +02:00

time.total_seconds() might be better so it picks up on days and up, which this converter converts.

`time.total_seconds()` might be better so it picks up on days and up, which this converter converts.
laggron42 (Migrated from github.com) reviewed 2018-09-18 12:27:02 +02:00
@ -0,0 +1,66 @@
import re
laggron42 (Migrated from github.com) commented 2018-09-18 12:27:01 +02:00

I tried to follow what the discord.py docs recommand. Please read this, and if you still want to remove that argument, tell me.
https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#advanced-converters

I tried to follow what the `discord.py` docs recommand. Please read this, and if you still want to remove that argument, tell me. https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#advanced-converters
laggron42 (Migrated from github.com) reviewed 2018-09-18 12:27:58 +02:00
@ -0,0 +1,66 @@
import re
laggron42 (Migrated from github.com) commented 2018-09-18 12:27:58 +02:00

Isn't that character supposed to only show BadArgument in the docs instead of showing discord.ext.commands.BadArgument? 🤔

Isn't that character supposed to only show `BadArgument` in the docs instead of showing `discord.ext.commands.BadArgument`? 🤔
laggron42 (Migrated from github.com) reviewed 2018-09-18 12:28:57 +02:00
@ -0,0 +1,66 @@
import re
laggron42 (Migrated from github.com) commented 2018-09-18 12:28:57 +02:00

I followed converters.py as an example for this PR, and tried to match with their style, but if it's not needed I'll remove it 👍

I followed `converters.py` as an example for this PR, and tried to match with their style, but if it's not needed I'll remove it 👍
Redjumpman (Migrated from github.com) reviewed 2018-09-18 13:36:49 +02:00
@ -0,0 +1,66 @@
import re
Redjumpman (Migrated from github.com) commented 2018-09-18 13:36:49 +02:00

True. total_seconds would be better.

True. `total_seconds` would be better.
Redjumpman (Migrated from github.com) reviewed 2018-09-18 13:37:33 +02:00
@ -0,0 +1,66 @@
import re
Redjumpman (Migrated from github.com) commented 2018-09-18 13:37:33 +02:00

If that's the case, I'll resolve this then.

If that's the case, I'll resolve this then.
Zephyrkul (Migrated from github.com) reviewed 2018-09-18 15:08:21 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-18 15:08:20 +02:00

the discord.py docs recommend using basic converters unless you require ctx. Since you don't require ctx, then a basic converter will likely be a better approach.
https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#basic-converters

the discord.py docs recommend using basic converters unless you require ctx. Since you don't require ctx, then a basic converter will likely be a better approach. https://discordpy.readthedocs.io/en/rewrite/ext/commands/commands.html#basic-converters
laggron42 (Migrated from github.com) reviewed 2018-09-18 23:52:28 +02:00
@ -0,0 +1,66 @@
import re
laggron42 (Migrated from github.com) commented 2018-09-18 23:52:28 +02:00

I find it easier for the users to use the advanced converters, because the core discord.py's converters are advanced. The users are used to call a function like that:

await commands.MemberConverter.convert(ctx, text)

Using the basic converters would give this:

commands.timedelta_converter(text)

Using a different style is quite a mess, especially since redbot.core.commands inherits from discord.ext.commands and include their converters. We should just keep the same style as the other converters. Just imagine:

  • For a member, I call commands.MemberConverter.convert()
  • For a channel, I call commands.TextChannelConverter.convert()
  • For a role, I call commands.RoleConverter.convert()
  • For time, I call commands.timedelta_converter()

That doesn't match at all with the other converters and will confuse the users. That's why I'm using advanced converters, so it works the same as the other ones, which are often used.

Basic converters may be useful for a personnal cog, but not for an API that already uses an other type of converters under the same module!

I find it easier for the users to use the advanced converters, because the core discord.py's converters are advanced. The users are used to call a function like that: ```py await commands.MemberConverter.convert(ctx, text) ``` Using the basic converters would give this: ```py commands.timedelta_converter(text) ``` Using a different style is quite a mess, especially since **`redbot.core.commands` inherits from `discord.ext.commands`** and include their converters. We should just keep the same style as the other converters. Just imagine: - For a member, I call `commands.MemberConverter.convert()` - For a channel, I call `commands.TextChannelConverter.convert()` - For a role, I call `commands.RoleConverter.convert()` - For time, I call `commands.timedelta_converter()` That doesn't match at all with the other converters and will confuse the users. That's why I'm using advanced converters, so it works the same as the other ones, which are often used. Basic converters may be useful for a personnal cog, but not for an API that already uses an other type of converters under the same module!
laggron42 commented 2018-09-19 00:03:18 +02:00 (Migrated from github.com)

Travis is failing because of a Python 3.6.6 problem.

Travis is failing because of a Python 3.6.6 problem.
Zephyrkul (Migrated from github.com) reviewed 2018-09-19 00:18:41 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-19 00:18:41 +02:00

discord.py includes basic converters as well, ex clean_content. More than that, basic converters are all over the bot, just look at any int or str typehints. Those are basic converters in d.py's eyes as well.

The advanced converters are advanced because they require ctx. The advanced converters require ctx because they are getting data about the context to return Discord objects. Your converter does not require ctx because it's neither returning any Discord objects nor getting any data about Discord's context.

Creating a convoluted method to convert to match Discord converters which require that convoluted method to function is quite simply absurd. You should match the functionality of what you require, keep it as simple as possible, and above all actually match how d.py functions with its typehint conversions rather than make nonsense excuses for what you coded.

Besides which, what happens if I want to convert an argument to a timedelta in a Discord event? Your method requires me to have a Context object when it's not needed, so I'd have to somehow find or create one or be SOL. I could pass None, but what looks better and is easier to remember in your eyes?

commands.timedelta_converter(my_string)
await commands.TimedeltaConverter().convert(None, my_string)

discord.py includes basic converters as well, ex `clean_content`. More than that, basic converters are all over the bot, just look at any `int` or `str` typehints. Those are basic converters in d.py's eyes as well. The advanced converters are advanced *because* they require `ctx`. The advanced converters require `ctx` because they are getting data about the context to return Discord objects. Your converter does not require `ctx` because it's neither returning any Discord objects nor getting any data about Discord's context. Creating a convoluted method to convert to match Discord converters which *require* that convoluted method to function is quite simply absurd. You should match the functionality of what you require, keep it as simple as possible, and above all actually match how d.py functions with its typehint conversions rather than make nonsense excuses for what you coded. Besides which, what happens if I want to convert an argument to a timedelta in a Discord event? Your method *requires* me to have a Context object when it's not needed, so I'd have to somehow find or create one or be SOL. I could pass `None`, but what looks better and is easier to remember in your eyes? `commands.timedelta_converter(my_string)` `await commands.TimedeltaConverter().convert(None, my_string)`
mikeshardmind (Migrated from github.com) reviewed 2018-09-19 01:22:11 +02:00
@ -0,0 +1,66 @@
import re
mikeshardmind (Migrated from github.com) commented 2018-09-19 01:22:11 +02:00

Actually, on that note @zephyrkul , I'd prefer if the actual conversion happened in a static method which was called from the convert to allow usages without the overhead of a creating a coroutine when being called manually. The only reason this needs to be a coroutine is it's use as a converter, so by offering manual usage as a feature, we should better support it as well.

Actually, on that note @zephyrkul , I'd prefer if the actual conversion happened in a static method which was called from the convert to allow usages without the overhead of a creating a coroutine when being called manually. The only reason this needs to be a coroutine is it's use as a converter, so by offering manual usage as a feature, we should better support it as well.
Zephyrkul (Migrated from github.com) reviewed 2018-09-19 01:25:57 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-19 01:25:57 +02:00

I'd prefer it if it wasn't an advanced Converter in the first place, which would remove the coro requirement (as well as the ctx requirement).
Did you mean to ping Laggron with that? Your phrasing seems to sound like you're addressing the person coding this PR.

I'd prefer it if it wasn't an advanced `Converter` in the first place, which would remove the coro requirement (as well as the `ctx` requirement). Did you mean to ping Laggron with that? Your phrasing seems to sound like you're addressing the person coding this PR.
palmtree5 commented 2018-09-19 03:48:48 +02:00 (Migrated from github.com)

uhh, Travis isn't failing on this PR atm @retke

uhh, Travis isn't failing on this PR atm @retke
mikeshardmind (Migrated from github.com) reviewed 2018-09-19 03:55:38 +02:00
@ -0,0 +1,66 @@
import re
mikeshardmind (Migrated from github.com) commented 2018-09-19 03:55:38 +02:00

Actually, you're right. There's nothing used here which would need a coro or ctx, so This can easily just be a basic converter.

Actually, you're right. There's nothing used here which would need a coro or ctx, so This can easily just be a basic converter.
Tobotimus commented 2018-09-19 04:04:40 +02:00 (Migrated from github.com)

I restarted the job that failed, was just Travis having its periodic connection issues when downloading python

I restarted the job that failed, was just Travis having its periodic connection issues when downloading python
laggron42 commented 2018-09-19 17:44:26 +02:00 (Migrated from github.com)

The 10 reviews are closed, is there something else to add?

The 10 reviews are closed, is there something else to add?
Zephyrkul (Migrated from github.com) reviewed 2018-09-19 17:47:11 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-19 17:47:11 +02:00

This and line 42 need updating with the new syntax.

This and line 42 need updating with the new syntax.
Zephyrkul (Migrated from github.com) reviewed 2018-09-19 23:38:35 +02:00
@ -0,0 +1,66 @@
import re
Zephyrkul (Migrated from github.com) commented 2018-09-19 23:38:35 +02:00

timedelta_converter is not awaitable.

`timedelta_converter` is not awaitable.
Tobotimus commented 2018-10-01 08:38:42 +02:00 (Migrated from github.com)

This was superseded by #2140

This was superseded by #2140

Pull request closed

Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!2126
No description provided.