[V3 commands] Introduction to Red's converters + TimeConverter #2126
No reviewers
Labels
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
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!2126
Loading…
Reference in a new issue
No description provided.
Delete branch "timed-mute"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
Description of the changes
This PR adds a
converters.pyfile, following d.py's style. This comes with theTimeConverterconverter, which converts a string to adatetime.timedeltaunit. 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
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 reThis should probably be
TimedeltaConverterThis is a compiled constant, it belongs outside the class at the top of the file
This disallows a zero second timedelta, which could be allowed by checking against
if not paramsearlier in the code instead.@ -0,0 +1,66 @@import reI 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
commandspackage, import it from the sibling-module, not from the top level. So in this example, it would beContext, you want to dofrom .context import Context.Also, whenever you're doing a relative import which is only for a type-hint, please use
typing.TYPE_CHECKINGand forward references to avoid any circular imports.@ -0,0 +1,66 @@import reWhat do you mean? Put arguments outside of the class and keep what's related to time inside the class?
@ -0,0 +1,66 @@import reBut 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 raisesBadArgument(instead of returning None or something like that).@ -0,0 +1,66 @@import reokay for
Context, but what aboutConverterandBadArgument? Should I importdiscord.ext.commandsinstead?@ -0,0 +1,66 @@import reYour handling of this places
0 secondsas invalid, when the intent is to not handle2 weeks. Meanwhile, ifparamsis an empty dict here, you can raise the bad argument there (having matched nothing expected)@ -0,0 +1,66 @@import reYou shouldn't be calling
re.compileeach time a conversion happens. Make it a module level constant, but don't place it in__all__'s definition.@ -0,0 +1,66 @@import reCouple issues with these code blocks.
@ -0,0 +1,66 @@import reI'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?@ -0,0 +1,66 @@import reCan we change this to
Manually convert a string ...instead ofConvert manually a string...?@ -0,0 +1,66 @@import re'~' character not needed.
@ -0,0 +1,66 @@import reNot 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.
@ -0,0 +1,66 @@import reThis 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)I added some comments to this PR as well. I think this could be a helpful addition, but it needs some cleaning up.
@ -0,0 +1,66 @@import rectx 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.
@ -0,0 +1,66 @@import retime.total_seconds()might be better so it picks up on days and up, which this converter converts.@ -0,0 +1,66 @@import reI tried to follow what the
discord.pydocs 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
@ -0,0 +1,66 @@import reIsn't that character supposed to only show
BadArgumentin the docs instead of showingdiscord.ext.commands.BadArgument? 🤔@ -0,0 +1,66 @@import reI followed
converters.pyas an example for this PR, and tried to match with their style, but if it's not needed I'll remove it 👍@ -0,0 +1,66 @@import reTrue.
total_secondswould be better.@ -0,0 +1,66 @@import reIf that's the case, I'll resolve this then.
@ -0,0 +1,66 @@import rethe 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
@ -0,0 +1,66 @@import reI 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:
Using the basic converters would give this:
Using a different style is quite a mess, especially since
redbot.core.commandsinherits fromdiscord.ext.commandsand include their converters. We should just keep the same style as the other converters. Just imagine:commands.MemberConverter.convert()commands.TextChannelConverter.convert()commands.RoleConverter.convert()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!
Travis is failing because of a Python 3.6.6 problem.
@ -0,0 +1,66 @@import rediscord.py includes basic converters as well, ex
clean_content. More than that, basic converters are all over the bot, just look at anyintorstrtypehints. Those are basic converters in d.py's eyes as well.The advanced converters are advanced because they require
ctx. The advanced converters requirectxbecause they are getting data about the context to return Discord objects. Your converter does not requirectxbecause 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)@ -0,0 +1,66 @@import reActually, 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.
@ -0,0 +1,66 @@import reI'd prefer it if it wasn't an advanced
Converterin the first place, which would remove the coro requirement (as well as thectxrequirement).Did you mean to ping Laggron with that? Your phrasing seems to sound like you're addressing the person coding this PR.
uhh, Travis isn't failing on this PR atm @retke
@ -0,0 +1,66 @@import reActually, you're right. There's nothing used here which would need a coro or ctx, so This can easily just be a basic converter.
I restarted the job that failed, was just Travis having its periodic connection issues when downloading python
The 10 reviews are closed, is there something else to add?
@ -0,0 +1,66 @@import reThis and line 42 need updating with the new syntax.
@ -0,0 +1,66 @@import retimedelta_converteris not awaitable.This was superseded by #2140
Pull request closed