首页 > 解决方案 > 我如何退出或中断 discord.py 中的命令

问题描述

我想知道是否有办法在匹配条件时中断命令

例如,当用户不是管理员时,我想打破这个命令

@client.command
async def test_me(ctx):
    user = ctx.author.id
    if user not in admins:
       await ctx.send("you're not admin")
       break command 
    else:
       None
    await ctx.send('Hi admin')

所以我不想在 if 语句中添加整个内容,这就是我问的原因

标签: pythonpython-3.xdiscorddiscord.pydiscord.py-rewrite

解决方案


你不能break一个函数。else您需要在循环中使用break,如果它为空,您也不需要该语句。

如果您只想退出该功能,您可以使用return

像这样的东西应该工作

@client.command
async def test_me(ctx):
    user = ctx.author.id
    if user not in admins:
       await ctx.send("you're not admin")
       return 
    await ctx.send('Hi admin')

推荐阅读