【问题标题】:Toggling command access - Discord.py切换命令访问 - Discord.py
【发布时间】:2020-07-26 15:09:28
【问题描述】:

我对 python 很陌生,所以我认为一个好的学习项目是为个人服务器创建一个不和谐的机器人,我有一些命令只有我作为机器人的所有者才能访问,但我希望能够使用命令切换该访问权限,以便我的朋友也可以使用它们,但我有一个问题,这是给我一个错误的代码:

MyID = '<@xxxxxxxxxxxxxxxx>' 
FFA = False

class TestCommands(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
    def IDCheck(ctx):
        return ctx.message.author.id == xxxxxxxxxxxxxxxxxxxxxx

   @commands.command()
    async def ToggleFFA(ctx):
        if FFA == False:
            FFA = True
            print (FFA)
            await message.channel.send('All user can now use owner locked commands')
        if FFA == True:
            FFA = False
            print (FFA)
            await message.channel.send('All user can no longer use owner locked commands')
###########################################################################
    @commands.command()
    if FFA == False:
        @commands.check(IDCheck)
        async def FFATest(self, ctx, *, question):
            loopnumber = 0
            while spamnumber < int(question):
                    await ctx.send('Test' + MyID);
                    await asyncio.sleep(1)
                    loopnumber += 1
                    print ({loopnumber})
    if FFA == True:
        async def LoopTest(self, ctx, *, question):
            loopnumber = 0
            while loopnumber < int(question):
                    await ctx.send('Test' + MyID);
                    await asyncio.sleep(1)
                    loopnumber+= 1
                    print ({loopnumber})
###########################################################################

在突出显示的代码段中出现无效语法错误。如果有人知道切换访问的更简单方法或我可以纠正错误的方法,我将不胜感激。

提前致谢。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    您可以指定一个bot_check_once 方法,该方法将用作对来自机器人的所有命令的检查。然后我们可以有一个 cog 的属性来控制我们处于哪种模式:

    class MyCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
            self.owner_only = True
    
        async def bot_check_once(self, ctx):
            app = await self.bot.application_info()
            if self.owner_only:
                return ctx.author == app.owner
            else:
                return True
    
        @commands.command()
        async def all_users(self, ctx):
            self.owner_only = False
    
        @commands.command()
        async def just_owner(self, ctx):
            self.owner_only = True
    

    【讨论】:

      【解决方案2】:

      你可以在外面添加一个检查方法。这是一个例子。

      def FFA_Enabled(ctx):
           global FFA
           if commands.is_owner():
                return True
           else:
                return FFA
      
      @commands.check(FFA_enabled):
      async def SomeCommand(self, ctx:Context):
           ctx.send("Message")
      

      这应该可以 如果你不知道 ctx:Context 是什么意思 它从 Context 类型派生 ctx(我用它来自动填充)如果你想要的话,我建议你输入这个:

      from discord.ext.commands import Context
      

      【讨论】:

        【解决方案3】:

        您可以为此使用 LIST,在其中可以存储 USER ID 和 Status ID。
        注意:这只是一个 sn-p 给你一个想法,脚本重新启动时 ID 将重置,我建议你将它保存在一个文件中并从那里加载它。
        您还可以使用函数根据 USER ID 返回 True/False,而不是在每个命令中编写一堆代码。

        users = []
        status = 'Global'
        
        @commands.is_owner()
        @commands.command()
        async def add_user(self,ctx,user:discord.User):
            global users
            id = user.id
            await ctx.send(f'{user.name} has been added into the mod list.')
            return users.append(id)
        
        @commands.is_owner()
        @commands.command()
        async def change_status(self,ctx):
            global status
            if status == 'Global':
                status = 'Local'
            elif status == 'Local':
                status = 'Global'
            await ctx.send(f'Status has been changed to {status}')
            return status
        
        @commands.command()
        async def test_command(self,ctx):
            global status
            global users
            #IF Status is Local
            if status == 'Local':
                if ctx.user.id in users:
                    #ALLOW THE USERS' WHOS' ID IS IN THE LIST
                    pass
                else:
                    #EXIT THE FUNCTION IF USER NOT IN THE LIST
                    return
            #IF The status is Global
            else:
                #ALLOW THE COMMAND IF IT'S GLOBAL
                pass
        

        【讨论】:

        • 哇,我没想到这么详细的回复非常感谢,我试试看
        • 抱歉,最近没有太多空闲时间,我在你的解决方案上搞砸了一段时间,但老实说,我无法让它工作,也不想提出另一条评论,因为我知道初学者程序员对于知道自己在做什么的人来说已经很痛苦。我最终改用了帕特里克的解决方案
        猜你喜欢
        • 2018-06-05
        • 2020-05-26
        • 1970-01-01
        • 2020-07-09
        • 2021-03-26
        • 2021-07-11
        • 2021-08-28
        • 2021-01-22
        • 2021-03-30
        相关资源
        最近更新 更多