【问题标题】:Only allow two people to use a discord bot command只允许两个人使用不和谐的机器人命令
【发布时间】:2021-11-01 00:22:37
【问题描述】:

我最近一直在研究一个不和谐的机器人,并遇到了一个问题,我希望只有我和另一个用户能够使用命令,但我就是不知道怎么做,我读了关于例外和列表,但我似乎无法理解如何使用它们,我基本上是在学习的过程中,有些东西比其他东西更容易。

我知道我将不得不添加和删除我只是不知道什么、在哪里以及如何的东西。

好吧,老实说,我试着做个疯子,结果复制了第一个返回并将其粘贴到自身下方。我知道那可能行不通。我还尝试在第一个 id 后面加一个逗号,然后放第二个 id。

def is_me():
    def predicate(ctx):
        return ctx.message.author.id == 413956481197670401
    return commands.check(predicate)

@client.command()
@is_me()
async def Secret(ctx):
    await ctx.send(f'{ctx.author.mention} Only you!')

@Secret.error
async def Secret_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        await ctx.send('Nothing here buckaroo.')

好吧,我的最终目标是拥有它,这样每当我们中的一个人触发命令时,它就会吐出我设置的内容,而当其他人使用它时,它就会吐出错误。

但是,每当我尝试使用逗号尝试时,它只允许任何人使用该命令。没有错误消息。

我很抱歉这个文字墙。

【问题讨论】:

  • 你问ctx.author.id in (id1, id2)怎么表达? (注意 id 周围的括号)
  • @PatrickHaugh 嗯,是的,这很可能就是我想要的,但我已经自学了如何做到这一点,我似乎无法理解它。跨度>
  • 我认为this answer 涵盖了您正在尝试做的事情。
  • @PatrickHaugh 是的,我只需要更新其中的一些以匹配当前的 discord.py。非常感谢您的帮助。

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

好的,所以你必须先定义命令。一旦你在 THAT 中发出该命令,就有一个 IF 语句来检查 ID 是否等于 or 其他任何人(使用他们的 ID),如果不是,则返回 null,或者更用户友好地返回没有权限的消息.

或者你可以创建一个变量来存储两个 ID,也许是一个数组?然后不使用 ID,只使用数组及其索引。

【讨论】:

    【解决方案2】:
    def is_Mod(user_id): 
        ## This function will check if the given id is already in the file. True if in file, False if not ##
            with open('Mod.txt', 'r') as f:
                if str(user_id) in f.read():
                    return True
                else:
                    return False
    

    现在定义我们将其用作

    @client.command()
    async def Secret(ctx):
        if is_Mod(ctx.author.id) == True:
            await ctx.send("Your response")
        else:
           #error
    

    【讨论】:

      【解决方案3】:

      Discord.py 提供 @is_owner 装饰器,因此只有所有者才能使用特定命令。您可以在初始化列表中显式提供 owner_ids,也可以将 id 存储在 db 中,命令名称为 dict 或其他内容,并编写自己的装饰器函数,将作者 id 与 db 中存在的 id 进行比较,如果 id 在列表中则返回 true否则为假

      【讨论】:

        【解决方案4】:

        允许 2 人或更多人使用命令可以通过多种方式完成,下面是我用于允许某些人/ID 使用命令的方法:

        IDCARD = [ID1, ID2] <---Make a list of IDs
        
        @client.command()
        async def hello(ctx):
        if ctx.author.id in IDCARD:
            #your code here (Hello World)
        else:
            await ctx.send('Access Denied!')
        

        【讨论】:

          【解决方案5】:

          如果你想成为自己的装饰师,你必须这样做:

          def is_me(func):
              @functools.wraps(func) # Don't forget to import functools
              async def wrapper(ctx, *param, **kwargs):
                  if ctx.message.author.id == 413956481197670401:
                      await func(ctx, *param, **kwargs)
                  else:
                      await ctx.send("Nothing here buckaroo.")
              return wrapper
          
          @client.command()
          @is_me
          async def Secret(ctx):
              await ctx.send(f'{ctx.author.mention} Only you!')
          

          但是你也应该知道你可以使用 discord.py 提供的装饰器来做你想做的检查。例如:

          【讨论】:

            猜你喜欢
            • 2021-11-04
            • 2018-11-10
            • 2018-07-21
            • 2017-05-31
            • 2021-10-21
            • 2022-01-15
            • 2021-06-17
            • 2020-09-25
            • 2022-10-24
            相关资源
            最近更新 更多