[V3] Predicate Utility #1985
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!1985
Loading…
Reference in a new issue
No description provided.
Delete branch "V3/develop"
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
I often find myself and other cog creators having to write a bunch of boiler plate checks either through functions or lambdas for multiple cogs that need waiting events. Specifically, for when you need to validate a response. Suppose the following scenario:
Because
ctxis often tightly coupled in these scenarios, you may find yourself writing simple predicates like the aboveconfirmfunction or an equivalent lambda over and over across multiple commands or cogs.Enter the new Predicates core utility module! This solves the problem with reusable predicates. Take the following example into consideration:
In the above code, we bypass having to write two separate checks by using Predicate. The Predicate class comes with the following methods:
yes,y,no, ornoNote: All methods require that the sender and the message author be the same.
I think
has_role,valid_role, andmembershould support names and IDs. Also, I want to see achannelspredicate, acanceloption, the ability to ignore case incontained, the ability to capture multiple items (consume-all, split on spaces or commas), and a way to capture the matched object(s).@calebj Those are some good points. I'll look into adding them to the module. Thank you for the feedback.
👍 This is awesome! This is just a suggestion of mine that could cut down on lines used and perhaps increase usability and extensibility:
ctxas an attribute to the predicate since we might add more checks which need more contextPredicateobject and return the requested function in one single expression.Take your example in the header of this PR. With above suggestions implemented, it could look like this:
The proposed classmethods could return the Predicate object itself but modify its
__call__to match the requested predicate function.Also I think most message predicates ought to check for
ctx.channel == m.channel, otherwise the bot could unexpectedly pick up responses in other channels.@calebj has some good suggestions too.
I really like this initiative! In fact I think (in another PR) we could add a
ReactionPredicateto compliment this one. Perhaps in the interest of future-proofing the naming scheme, this could be renamed toMessagePredicate? Let me know if you have any qualms or queries.@Tobotimus These are some awesome suggestions. I'll continue working on this PR and try to implement the things you and caleb brought up. I my original implementation used ctx, instead of requiring a member object, but was worried it might seem as too much. I'm happy to revert that change. I'll also change the naming convention to MessagePredicate or something similar so it's a little more extensible if someone wants to add a new predicate class.
Changed a few things based on the suggestions:
I have a concern about allowing for additional arguments, outside of instantiating the class, because the predicate is supposed to only accept on argument (or 2 if using it for reactions). I think that's why I initially off-loaded some of the extra args onto
__init__. Although I could be tired and missing something obvious. Anyone is free to take a whack at this if they want to.Awesome, this is a good start. I'm going to merge this into a feature branch now, since as we can see there are quite a few suggestions for additions and/or changes. Work can continue there :)