Pavlov的命令处理程序
pvlv-commando的Python项目详细描述
巴甫洛夫突击队
命令框架以一种简单的方式处理文本命令。在
示例
frompvlv_commando.pvlv_commandoimportCommandofrompvlv_commandoimport(CommandNotFound,CommandExecutionFail,ArgVoidNotAllowed,ParamNotFound,MaxHourlyUses,MaxDailyUses,)defcommand_handler():com=Commando()# An example of incoming text from the chattext='.command this is a nice argument -d a text parameter -f -g'""" permission level of the user in the chat, it will be compared with the permissions needed to run the command, specified in the declaration file """permissions=10iftext.startswith('.'):try:# text without the command invocation word, and the language of the commandcom.find_command(text[1:],'eng',permissions)""" Optional! You can specify in the command declaration file a max time use of the command cause of computational weight of the command. Build a db where you save the number of executions by the user """com.hourly_executions=12com.daily_executions=39""" to run the command you have to pass the bot object, that will be used inside commands. max_chunk_len: specify the max len of the out, some chats have a limit in length """out=com.run_command(None,max_chunk_len=1500)ifout:""" Send to the chat with parse mode enabled ** mean bold - if your chat dont support parse mode use com.run_manual().replace('**', '') - if your chat have a different parse mode use com.run_manual().replace('**', 'your_format') """print(out)# DO NOT EXPOSE FULL EXCEPTIONS IN CHAT, ONLY THIS ARE READY FOR CHATexceptCommandNotFoundasexc:print(exc)exceptArgVoidNotAllowedasexc:print(exc)exceptParamNotFoundasexc:print(exc)exceptMaxHourlyUsesasexc:print(exc)exceptMaxDailyUsesasexc:print(exc)exceptCommandExecutionFailasexc:print(exc)# the exception to send in chat# the full report of the exception to send to a log chat or for internal log.print(exc.error_report)
##软件包的配置文件: 必须放在项目根目录下的“configs”文件夹中
配置/命令.cfg在
^{pr2}$自动创建命令
使用此工具,您可以使用默认文件夹和文件自动创建新命令
frompvlv_commandoimportStartCommanddefmain():# define the module name and the command name (use underscores only)nc=StartCommand('new_module','new_command')nc.create()""" The full command_declaration json file will be created Check the command_declaration json file to learn how to set up it """if__name__=='__main__':main()
####命令声明文件 它是一个与命令.py文件和命令文件夹。在
旁注中带有“OPTIONAL”标签的所有内容都可以省略
示例:
{
"management_command": 0, # OPTIONAL, the commad is owner only
"beta_command": false, # OPTIONAL, the command in still in development
"pro_command": 0, # OPTIONAL, the command can be run only by pro users (pro level il arbitrary int)
"dm_enabled": true, # OPTIONAL, enabled in direct char, 1 to 1 with the bot
"enabled_by_default": true, # OPTIONAL, the command must be activare manually by the user
"permissions": 0, # OPTIONAL, the permission level to run the command
"cost": 20, # OPTIONAL, cost of the command, user might a value to run this command
"hourly_max_uses": 10, # OPTIONAL, man uses per hour by user
"daily_max_uses": 90, # OPTIONAL, man uses per day by user
"invocation_words": ["command", "com"], # the command invocation words
"description": {
"eng": "Short description of the command",
"ita": "Breve descrizione del comando"
},
"handled_args": { # must always contain at least one element, "" rappresent the void one
"": {
"eng": "Description of command without args",
"ita": "Descrizione del comando senza argomenti"
},
"arg": {
"eng": "Description of executions with this argument",
"ita": "Descrizione dell' esecuzione con questo argomento"
}
},
"handled_params": { # OPTIONAL, the parameters
"-param1": {
"eng": "Description of executions with this parameter",
"ita": "Descrizione dell'esecizione con questo parametro"
},
"-param2": {
"eng": "Description of executions with this parameter",
"ita": "Descrizione dell'esecizione con questo parametro"
}
}
}
命令类示例
这是调用命令时将运行的命令文件
classCommandName(object):def__init__(self,bot,language,command,arg,params):self.bot=bot# bot entity to send messages, the one that you pass on run commandself.language=language# the language detected in the guild, to personalize responsesself.command=command# the command descriptor of the commandself.arg=arg# the detected arg""" parameters will be initialized here You have to reserve the vars here that you need to use in _vars must be the same name as handled_params in the config json but without dash (-) """self._param1=None# the detected parametersself._param2=Noneforparaminparams:# read the parameter from the params dict and save them in vars over herename='_{}'.format(param[0])setattr(self,name,param[1])defrun(self):print('Command has been run arg: {}'.format(self.arg))
- 项目
标签: