【问题标题】:Making say command bot owner only只说命令机器人所有者
【发布时间】:2018-11-03 16:47:49
【问题描述】:

我正在尝试让我的 !say 命令仅适用于机器人所有者。这是我目前拥有的

@bot.command(pass_context = True)
async def say(ctx, *args):
    mesg = ' '.join(args)
    await bot.delete_message(ctx.message)
    return await bot.say(mesg)

代码有效,但我想让它只有我(机器人所有者)才能运行命令。

【问题讨论】:

  • 我也没有使用类。如果有帮助

标签: discord.py


【解决方案1】:

你也可以使用装饰器@is_owner()

@bot.command(pass_context = True)
@commands.is_owner()
async def say(ctx):
    your code...

【讨论】:

  • 使用 commands.is_owner() 应该是公认的答案,而不是在代码中添加您自己的 ID
【解决方案2】:

通过添加 if 语句尝试这样做

@bot.command(pass_context=True)
async def say(ctx):
      if ctx.message.author.id =='bot owner id':
      then execute the following code

【讨论】:

    【解决方案3】:

    我就是这样做的 - 如果这不能按预期工作,请发表评论。

    @client.command()
    @commands.is_owner() 
    async def say(ctx, *, message):
       await ctx.send(f"{message}")
    

    玩得开心!抱歉回复晚了,我正在阅读旧问题并寻求未解决的问题

    (重制)

    【讨论】:

    • 我已经更改了代码并使其更简单
    【解决方案4】:

    1.0 文档中check 的文档有以下示例(稍作修改。)

    def user_is_me(ctx):
        return ctx.message.author.id == "Your ID" 
    
    @bot.command(pass_context = True)
    @commands.check(user_is_me)
    async def say(ctx, *args):
        mesg = ' '.join(args)
        await bot.delete_message(ctx.message)
        return await bot.say(mesg)
    

    How to find your ID

    【讨论】:

    • ` 文件 "bot.py",第 162 行返回 ctx.message.author.id = "206696404347781120" ^ SyntaxError: invalid syntax ` 这就是我现在得到的。因为我才刚开始,所以我在 Python 方面并不是最好的。
    • @damian 我使用了= 而不是==。只是一个错字。再试一次?
    猜你喜欢
    • 2021-08-09
    • 2020-10-26
    • 2021-04-05
    • 2021-02-14
    • 1970-01-01
    • 2021-02-02
    • 2018-02-11
    • 2021-11-01
    • 1970-01-01
    相关资源
    最近更新 更多