aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar vcokltfre <[email protected]>2021-04-07 03:28:05 +0100
committerGravatar vcokltfre <[email protected]>2021-04-07 03:28:05 +0100
commitd14f83a4bc174a9c706552ba9b674cc1d9895efb (patch)
treed4dbdd45747ef33f138dca935b32fc0a7cf99cb3
parentMerge pull request #1504 from python-discord/reduce-default-stream-duration (diff)
add custom command checks tag
-rw-r--r--bot/resources/tags/customchecks.md21
1 files changed, 21 insertions, 0 deletions
diff --git a/bot/resources/tags/customchecks.md b/bot/resources/tags/customchecks.md
new file mode 100644
index 000000000..4f0d62c8d
--- /dev/null
+++ b/bot/resources/tags/customchecks.md
@@ -0,0 +1,21 @@
+**Custom Command Checks in discord.py**
+
+You may find yourself in need of a decorator to do something that doesn't exist in discord.py by default, but fear not, you can make your own! Using discord.py you can use `discord.ext.commands.check` to create you own decorators like this:
+```py
+from discord.ext.commands import check, Context
+
+def in_channel(*channels):
+ async def predicate(ctx: Context):
+ return ctx.channel.id in channels
+ return check(predicate)
+```
+There's a fair bit to break down here, so let's start with what we're trying to achieve with this decorator. As you can probably guess from the name it's locking a command to a list of channels. The inner function named `predicate` is used to perform the actual check on the command context. Here you can do anything that requires a `Context` object. This inner function should return `True` if the check is **successful** or `False` if the check **fails**.
+
+Here's how we might use our new decorator:
+```py
[email protected](name="ping")
+@in_channel(728343273562701984)
+async def ping(ctx: Context):
+ ...
+```
+This would lock the `ping` command to only be used in the channel `728343273562701984`.