子命令的问题:ctx.invoked_subcommands 总是 None
Issue with subcommands: ctx.invoked_subcommands is always None
编辑:已找到解决方案
Try changing the names of the subcommands to _enabled
and _disabled
and pass name="enabled"
name="disabled"
to the command decorators. This is a guess, but sometimes names that the Bot uses internally don't work properly when used as command names. - Patrick Haugh
我试图清理一些代码并试图将子命令实现到一个模块中,但我遇到了一个我似乎无法解决的问题。
首先,这是代码:
@commands.group()
@commands.has_permissions(administrator=True)
async def autorole(self, ctx):
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
if ctx.invoked_subcommand is None:
em = None
if (exists == None):
em = discord.Embed(title="Autorole is disabled for this guild.", color=discord.Color(0xff0000))
else:
em = discord.Embed(title="Autorole is enabled for this guild.", color = discord.Color(0x32ff00))
rol = discord.utils.get(ctx.guild.roles, id=exists)
em.add_field(name="Current role:", value=rol.mention)
await ctx.send(embed=em)
@autorole.command()
@commands.has_permissions(administrator=True)
async def enabled(self, ctx, role: discord.Role=None):
"""Defines a role that will be applied to all new members, format: autorole (enabled/disabled) [role]"""
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
print('status, role : {} {}'.format("enabled", role.id))
try:
if role==None:
await ctx.send("No role provided")
else:
if (exists!=None):
dbinteraction.dbexec("UPDATE autorole SET role = {}".format(role.id))
else:
dbinteraction.dbexec("INSERT INTO autorole VALUES({},{})".format(ctx.guild.id, role.id))
em = discord.Embed(title="", color= discord.Color(0x32ff00))
em.add_field(name="Autorole enabled", value="Current role: {}".format(role.mention))
await ctx.send(embed=em)
except (Exception) as e:
print(e)
@autorole.command()
@commands.has_permissions(administrator=True)
async def disabled(self, ctx, role: discord.Role=None):
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
print('disab')
if(exists!=None):
dbinteraction.dbexec("DELETE FROM autorole WHERE server_id = {}".format(ctx.server.id))
await ctx.send("Autorole disabled")
无论我尝试什么,下面的代码总是执行默认命令(if ctx.invoked_subcommand is None:
之后的那个),我正在使用重写
您可能对 invoke_without_command
感兴趣。这是一个你传递给你的组的参数,它告诉它如果没有子命令只 运行 回调。所以你的小组是
@commands.group(invoke_without_command=True)
@commands.has_permissions(administrator=True)
async def autorole(self, ctx):
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
if (exists == None):
em = discord.Embed(title="Autorole is disabled for this guild.", color=discord.Color(0xff0000))
else:
em = discord.Embed(title="Autorole is enabled for this guild.", color = discord.Color(0x32ff00))
rol = discord.utils.get(ctx.guild.roles, id=exists)
em.add_field(name="Current role:", value=rol.mention)
await ctx.send(embed=em)
编辑:
来自我下面的评论:
Try changing the names of the subcommands to _enabled
and _disabled
and pass name="enabled"
and name="disabled"
to the command
decorators. This is a guess, but sometimes names that the Bot
class uses internally don't work properly when used as command names.
编辑:已找到解决方案
Try changing the names of the subcommands to
_enabled
and_disabled
and passname="enabled"
name="disabled"
to the command decorators. This is a guess, but sometimes names that the Bot uses internally don't work properly when used as command names. - Patrick Haugh
我试图清理一些代码并试图将子命令实现到一个模块中,但我遇到了一个我似乎无法解决的问题。 首先,这是代码:
@commands.group()
@commands.has_permissions(administrator=True)
async def autorole(self, ctx):
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
if ctx.invoked_subcommand is None:
em = None
if (exists == None):
em = discord.Embed(title="Autorole is disabled for this guild.", color=discord.Color(0xff0000))
else:
em = discord.Embed(title="Autorole is enabled for this guild.", color = discord.Color(0x32ff00))
rol = discord.utils.get(ctx.guild.roles, id=exists)
em.add_field(name="Current role:", value=rol.mention)
await ctx.send(embed=em)
@autorole.command()
@commands.has_permissions(administrator=True)
async def enabled(self, ctx, role: discord.Role=None):
"""Defines a role that will be applied to all new members, format: autorole (enabled/disabled) [role]"""
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
print('status, role : {} {}'.format("enabled", role.id))
try:
if role==None:
await ctx.send("No role provided")
else:
if (exists!=None):
dbinteraction.dbexec("UPDATE autorole SET role = {}".format(role.id))
else:
dbinteraction.dbexec("INSERT INTO autorole VALUES({},{})".format(ctx.guild.id, role.id))
em = discord.Embed(title="", color= discord.Color(0x32ff00))
em.add_field(name="Autorole enabled", value="Current role: {}".format(role.mention))
await ctx.send(embed=em)
except (Exception) as e:
print(e)
@autorole.command()
@commands.has_permissions(administrator=True)
async def disabled(self, ctx, role: discord.Role=None):
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
print('disab')
if(exists!=None):
dbinteraction.dbexec("DELETE FROM autorole WHERE server_id = {}".format(ctx.server.id))
await ctx.send("Autorole disabled")
无论我尝试什么,下面的代码总是执行默认命令(if ctx.invoked_subcommand is None:
之后的那个),我正在使用重写
您可能对 invoke_without_command
感兴趣。这是一个你传递给你的组的参数,它告诉它如果没有子命令只 运行 回调。所以你的小组是
@commands.group(invoke_without_command=True)
@commands.has_permissions(administrator=True)
async def autorole(self, ctx):
exists = dbinteraction.dbexec("SELECT role from autorole WHERE server_id = {}".format(ctx.guild.id))
if (exists == None):
em = discord.Embed(title="Autorole is disabled for this guild.", color=discord.Color(0xff0000))
else:
em = discord.Embed(title="Autorole is enabled for this guild.", color = discord.Color(0x32ff00))
rol = discord.utils.get(ctx.guild.roles, id=exists)
em.add_field(name="Current role:", value=rol.mention)
await ctx.send(embed=em)
编辑:
来自我下面的评论:
Try changing the names of the subcommands to
_enabled
and_disabled
and passname="enabled"
andname="disabled"
to thecommand
decorators. This is a guess, but sometimes names that theBot
class uses internally don't work properly when used as command names.