discord.py机器人命令仅获取上下文

时间:2019-12-27 03:51:32

标签: python discord.py

我刚刚测试了discord.py的语法,发现有问题。

以下三个代码段的

clientcommands.Bot()

@client.command(pass_context=True)
@commands.has_permissions(manage_roles=True)
async def add_role(mes: discord.Message, member: discord.Member, role: discord.Role):
    print(mes)
    # input: ?add_role someone Tester
    # output: <discord.ext.commands.context.Context object at 0x03B2EF30>
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def exit(mes: discord.ext.commands.Context):
    print(mes)
    # input: ?exit
    # output: <discord.ext.commands.context.Context object at 0x03B2EF50>
@client.command(pass_context=True)
async def ask(mes: discord.message.Message):
    print(mes)
    # input: ?ask
    # output: <discord.ext.commands.context.Context object at 0x03B2ED10>

为什么所有结果都相同<discord.ext.commands.context.Context object>而又不同mes

2 个答案:

答案 0 :(得分:1)

Context对象始终是传递给命令的第一个参数。
参见documentation for the commands extension

现在不管pass_context都是正确的,Context是discord.py v0.16版本的语法,现在不再支持。请参阅the guide for migrating to v1,尤其是Context Changes section

注意,SERVER_IP=$(shell python ./scripts/script.py get-server-ip); \ SERVER_CMD="iperf3 -s -p ${PORT} -4 --logfile s.out"; \ CLIENT_CMD="iperf3 -c $${SERVER_IP} -p ${PORT} -t 1000 -4 --logfile c.out"; \ echo "Server Command: " $${SERVER_CMD}; \ echo "Client Command: " $${CLIENT_CMD}; \ docker exec -d server $${SERVER_CMD}; \ docker exec -d client $${CLIENT_CMD}; 转换器也没有多大意义,也不由discord.py定义。

答案 1 :(得分:0)

on_command_error()的参数是上下文和异常,按顺序

@bot.event
async def on_command_error(ctx: commands.Context, exception: Exception):
    print(f"Error occured - {type(exception).__name__} : {exception}")