[Commands] Added optional default_unit to the TimedeltaConverter #2753
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 project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot!2753
Loading…
Reference in a new issue
No description provided.
Delete branch "timedelta_default"
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
default_unitto the TimedeltaConverter, that let's you specify the default unit to use in cases where user would provide an integer input only. Let's consider this example:async def mute(self, ctx, user: discord.Member, minutes: int = 10)The command above, made using a standard base and discord converters, presumably lets you mute server members for the amount of time specified by
minutes. Now, if one would like to mute for couple of hours or days, and generally have more granular and easy to use input, they could use the new TimedeltaConverter to achieve this:async def mute(self, ctx, user: discord.Member, time: TimedeltaConverter = timedelta(minutes=10))Now, because of this change, doing something like
[p]mute Member 20will no longer work due to the converter expecting a unit specified right after. With this PR, adefault_unitcan be passed into the converter like so:async def mute(self, ctx, user: discord.Member, time: TimedeltaConverter(default_unit="minutes") = timedelta(minutes=10))..., making it parse integer-only input as a time duration as well, using the unit specified. This provides backwards compatibility with every command that used seconds, minutes, hours, days or weeks as the input unit, while also allowing to specify the time more precisely than it was possible.
I'm not that good with docs, so any improvement suggestions are welcome.
Note on the implementation:
.isdigit()specification ensures thatint()should never fail to convert the string provided, so should be safe to use here.Looks mostly fine, but needs a small adjustment before merge. Thinking on it, we may have the mixup I'm mentioning here in other places as well, and it may be a good idea to check for it.
@ -224,18 +224,29 @@ class TimedeltaConverter(dpy_commands.Converter):allowed_units : Optional[List[str]]If provided, you can constrain a user to expressing the amount of timeisdigitisn't safe for this.for why, consider
³. (In case of font display issues, the equivalent escaped string is\N{SUPERSCRIPT THREE})This passes both
isnumericandisdigitbut will raise a ValueError if you attempt to make it an int.Right, didn't spot this one. I think I also may have this mixup in other places too :P
Updated per request, should be ready to go now =)
I should have noticed this before, but didn't. Sorry about tossing on something extra here again.
get_timedelta_converteralso needs to be updated for the extra optional keyword arg.