对于python来说还很新,所以我认为一个好的学习项目是为个人服务器创建一个不和谐的bot,我有一些命令,只有作为bot的所有者才能访问,但我希望能够使用命令切换访问权限,以便我的朋友也可以使用它们,但是我有一个问题,这是一段代码,给我一个错误:
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})
###########################################################################
我在突出显示的代码段中收到无效的语法错误。如果有人知道切换访问权限的更简单方法或我可以纠正错误的方法,我将不胜感激。
提前谢谢。
答案 0 :(得分:1)
您可以为此使用LIST,在其中可以存储USER ID和Status ID。
注意:这只是一个片段,可以让您了解一下,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
答案 1 :(得分:1)
那么您可以在外部添加检查方法。这是一个例子。
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
答案 2 :(得分:1)
您可以指定一个bot_check_once
方法,该方法将用作检查来自该机器人的所有命令的方法。然后,我们可以使用齿轮属性来控制我们处于哪种模式:
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