KannaX Callback Decorators
In addition to available Pyrogram Decorators
, kannax comes with its own custom decorators to handle callback functions.
@kannax.on_cmd
@kannax.on_cmd
Decorator for handling messages containing command text and custom flags or command prefixes.
Parameters:
~ $ ~ [Mandatory] ~ $ ~
command: str
Command name to be executed (without a command trigger prefix)about: str | dict
Describe your function, this will be used to parse help strings. This can be a single string or a dictionary containing keys and values as mentioned below.Options Available for about (dict):
header: str
Title of your command[Example]
description: str
Describe the working of your command[Example]
flags: str | dict
Mention available flags in your command[Example]
options: str | dict
Mention available options that can be used with your command[Example]
types: str | list
Specific types with which your command can be triggered[Example]
usage: str
Syntax with which user should trigger command[Example]
others: str
Additional note that you would like to give[Example]
wild card: str | dict | list
This can be used to give customised title and description other than above defaults. Syntax: The title you want to declare should be key of dictionary and value should be a string, or another dictionary or a list.[Example]
{tr}
: This option can be used in usage or examples to replace default.
prefix of command trigger with custom user definedConfig.CMD_TRIGGER
~ $ ~ [Optional] ~ $ ~
group: int
The group identifier of callable function.[default: 0]
name: str
A name for your command.[default: '']
trigger: str
A custom trigger for your command. Useful when you want commands to be triggered only by some specific triggers only.[default: Config.CMD_TRIGGER]
filter_me: bool
Specify who can access this command. IfFalse
everyone can access this command else ifTrue
only userbot owner and sudo-users* can access this command.[default: True]
allow_private: bool
Specify if your command should work in private chats or not.[default: True]
allow_bots: bool
Specify if your command is intended to work for bots chats.[default: True]
allow_groups: bool
Specify if your command works in group chats.[default: True]
allow_channels: bool
Specify if your command works in channels.[default: True]
All above
allow_*
keyword arguments are simply provided to prohibit specific chat types to ensure the proper functioning of your callable function.
only_admins: bool
Specify if your command (i.e. callable function) needs admin privileges in current chat (groups or channel) to be used.[default: False]
allow_via_bot: bool
kannax supports bot account session too. And since due to some Telegram Restrictions bots can’t trigger some API calls, and in order to avoid BotMethodInvaild error this argument can be passed asTrue
to prohibit command execution if the client is set to bot.[default: True]
check_client: bool
By passingTrue
kannax will check if current client is bot or not.[default: False]
check_downpath: bool
IfTrue
, kannax will make sure thatConfig.DOWN_PATH
exists.[default: False]
check_change_info_perm: bool
PassTrue
to check if the current kannax client has Change Info Privileges in current chat before further execution of the program.[default: False]
check_edit_perm: bool
PassTrue
to check if current client has Edit Permission in current Channel before further execution.[default: False]
check_delete_perm: bool
PassTrue
to check if current client has Delete Permissions in current chat.[default: False]
check_restrict_perm: bool
PassTrue
to check if current client can Restrict Users in current chat.[default: False]
check_invite_perm: bool
PassTrue
to check if the current client has Privileges to Add Users to export a private invite link, etc…[default: False]
check_pin_perm: bool
PassTrue
to check if the current client has Privileges to Pin Messages in the current chat.[default: False]
prefix: str
Set a custom prefix to detect flags in a message.[default: '-']
del_pre: bool
PassTrue
to get adict
of flags without prefix.[default: False]
[*]
: Works only if the command is allowed to be triggered by sudo users viaaddscmd
Examples:
Here, we will be explaining how to use on_cmd
briefly with some example which will try to cover almost all above mentioned parameters.
Firstly import kannax
client to your program as on_cmd
can be accessed as an attribute of kannax
client.
Now we have imported kannax client. For example let’s make an echo command that will take an input and send it back to the current chat.
Point to Remember:
Unlike pyrogram decorators,
kannax
decorators return only one positional argument i.eMessage
. Just in case you want to use client methods you can either directly access them via importedkannax
(as shown in above example) OR useclient
attribute of modifiedMessage
class ofkannax
(as shown below)
Let’s say, now we need our command to take input from the replied message when a specific flag is passed then,
However, now we want the following modification in echo
command:
It should work only in groups.
Can only be triggered by
>
prefix.Change
-
flag prefix to*
.Anyone can use this command.
To do this all we need to do is take the same code and alter some parameters,
That’s all for kannax.on_cmd
for more examples check out our Plugins in main repo or unofficial plugins repository
@kannax.on_filters
@kannax.on_filters
Decorator for handling filters.
Parameters:
~ $ ~ [Mandatory] ~ $ ~
filters: pyrogram.filters
One or more pyrogram filters.
~ $ ~ [Optional] ~ $ ~
Same as in on_cmd
on_cmd
group: int
allow_private: bool
allow_bots: bool
allow_groups: bool
allow_channels: bool
only_admins: bool
allow_via_bot: bool
check_client: bool
[default: True]
check_downpath: bool
check_change_info_perm: bool
check_edit_perm: bool
check_delete_perm: bool
check_restrict_perm: bool
check_invite_perm: bool
check_pin_perm: bool
Examples:
Let’s make a simple program to detect incoming audio
and video
files in any chat but not in channels. So …
Now let’s optimize it for a specific chat. Let say I want to filter from @kannaxsup
. So we only need to add an extra filter to filter that chat.
That’s all for kannax.on_filters
for more examples check out our Plugins in main repo or unofficial plugins repository
Last updated