def subcommands(self):
if self._on_subcommand_page:
return None
ctx, command = self.context, self.command
assert isinstance(command, commands.GroupMixin), "command has no subcommands"
self._on_subcommand_page = True
subs = sorted(map(str, set(command.walk_commands())))
note = (
f'Type `{ctx.clean_prefix}{ctx.invoked_with} {command} subcommand`'
f' for more info on a subcommand.\n'
f'(e.g. type `{ctx.clean_prefix}{ctx.invoked_with} {random.choice(subs)}`)'
)
return (discord.Embed(colour=self.colour, description='\n'.join(map('`{}`'.format, subs)))
.set_author(name=f'Child Commands for {command}')
.add_field(name='\u200b', value=note, inline=False)
)
python类GroupMixin()的实例源码
def _has_subcommands(command):
return isinstance(command, commands.GroupMixin)
def has_subcommands(self, command):
"""bool : Specifies if the command has subcommands."""
return isinstance(command, commands.GroupMixin)
def on_command_error(self, error, ctx: commands.Context):
log = lib.get_author_name(ctx)
send = None
if isinstance(error, commands.CheckFailure):
if str(error) == "timer not found":
send = "No timer found for this channel."
log += " tried to interact with a nonexistent timer."
elif str(error) == "timer locked":
send = "You are not allowed to modify this timer."
log += " tried to modify a locked timer without permissions."
elif str(error) == "no permissions" or str(error) == "not admin":
send = "You do not have permission to do this!"
log += (" tried to execute a command and failed, " +
"lacked permissions.")
else:
send = "Timers are not allowed in this channel."
log += " tried to start a timer in a non-whitelisted channel."
elif isinstance(error, commands.CommandNotFound):
send = "Command not found: `" + ctx.invoked_with + "`."
log += " tried to execute a nonexistent command: `{}`."\
.format(ctx.invoked_with)
alt = None
for name, command in self.bot.commands.items():
if ctx.invoked_with == name:
alt = name
elif isinstance(command, commands.GroupMixin):
for sub_name, sub_command in command.commands.items():
if ctx.invoked_with == sub_name:
alt = name + " " + sub_name
if alt is not None:
send += " Did you mean `" + alt + "`?"
elif isinstance(error, commands.CommandInvokeError):
lib.log_cmd_stacktrace(error)
return
else:
log = str(error)
lib.log(log, channel_id=lib.get_channel_id(ctx), level=logging.WARN)
await self.bot.safe_send(lib.get_channel(ctx), send,
delete_after=self.bot.ans_lifespan)
def unload_extension(self, extension_name): # horrible hacks to make cogs work with single process sharding
for shard in self.shards.values():
if not extension_name in shard.extensions.keys():
continue
ext_lib = shard.extensions[extension_name]
# print("removing cogs")
for cog_name, cog in shard.cogs.copy().items():
if get_module(cog) is ext_lib:
shard.remove_cog(cog_name)
# print("removing commands")
for command in shard.commands.copy().values():
if command.module is ext_lib:
command.module = None
if isinstance(command, commands.GroupMixin):
command.recursively_remove_all_commands()
shard.remove_command(command.name)
# print("removing events")
for event_list in shard.extra_events.copy().values():
remove = []
for idx, event in enumerate(event_list):
if get_module(event) is ext_lib:
remove.append(idx)
for idx in reversed(remove):
del event_list[idx]
# print("tearing down")
try:
teardown_func = getattr(ext_lib, "teardown")
except AttributeError:
pass
else:
try:
func(shard)
except:
pass
finally:
del shard.extensions[extension_name]
del self.extensions[extension_name]
del sys.modules[extension_name]
# print("finished")