def is_cog(self, command):
"""bool : Specifies if the command being formatted is actually a cog."""
return not self.is_bot(command) and not isinstance(command, commands.Command)
python类command()的实例源码
def format(self):
"""
Formats the help page.
"""
# Adapted from discord.ext.commands.formatter.HelpFormatter.format
self._paginator = AryasPaginator()
description = self.command.description if not self.is_cog() else inspect.getdoc(self.command)
if description:
self._paginator.new_page(description=description)
if isinstance(self.command, Command):
# long help doc
if self.command.help:
self._paginator.add_line(self.command.help)
self._paginator.set_name(self.get_command_signature())
self._paginator.make_field(inline=False)
# if it's just a single command we're done here
if not self.has_subcommands():
return self._paginator.pages
# Helper method for sorting by category (cog)
def category(tup):
cog = tup[1].cog_name
# Unicode invisible space is there to set No Category last
return cog + ':' if cog is not None else '\u200bNo Category:'
# if command is a bot we need to process the entire command list
if self.is_bot():
data = sorted(self.filter_command_list(), key=category)
for category, commands in itertools.groupby(data, key=category):
commands = list(commands)
if len(commands) > 0:
self._add_entries(commands)
self._paginator.set_name(category)
self._paginator.make_field(inline=False)
else:
# if command is just a cog or Group we can print all the commands
# returned by filter_command_list
self._add_entries(self.filter_command_list())
self._paginator.set_name('Commands:')
self._paginator.make_field(inline=False)
# Get the ending message
self._paginator.set_name('More:')
self._paginator.add_line(self.get_ending_note())
self._paginator.make_field(inline=False)
return self._paginator.pages
def permissions(self, ctx, target_name, command_name, allow: bool=None):
"""Sets permissions for a user, role, or everyone.
You can mention a user or role, or use their name or ID, to set permissions for them.
Use "everyone" to affect everyone's permissions in this guild.
Allow can be true/false, yes/no, or on/off.
"""
target = await self.find_target(ctx, target_name)
if target is None:
await ctx.send('BAKA! Target must be a member, role, or "everyone"!')
command, command_name = self.find_command(ctx, command_name)
if command is None:
await ctx.send('BAKA! Command must be command name, category name, or "all"!')
if allow is None: # Get
allowed = config.allowed(ctx, user=target, command=command)
perm_str = 'allowed' if allowed else 'not allowed'
elif config.allowed(ctx, command=command): # Check the caller has permission to give/take this command
with db.Session() as session:
attrs = {'guild_id': ctx.guild.id, 'target_id': target.id, 'command_name': command_name}
record = session.get(config.Permission, **attrs).one_or_none()
if record is None:
record = session.add(config.Permission(**attrs, allowed=allow))
else:
record.allowed = allow
perm_str = 'now allowed' if allow else 'no longer allowed'
else:
return await ctx.send('BAKA! You can only control permissions for commands you can use!')
if isinstance(target, discord.Member):
target_str = target.name
elif isinstance(target, discord.Role):
target_str = 'The {} role'.format(target.name.capitalize())
else:
target_str = 'Everyone'
if command_name == '_all':
command_str = 'all commands'
elif command_name.startswith('_'):
command_str = 'the {} category'.format(command_name[1:].capitalize())
else:
command_str = 'the {} command'.format(command_name)
await ctx.send('{0} is {1} to use {2}.'.format(target_str, perm_str, command_str))
def _make_commands(self):
group = commands.Group(
name=self.service_name,
callback=self._group_command,
help=self._make_help_string(strings.group_command_help),
)
group.instance = self
cmd = commands.Command(
name='add',
aliases=['subscribe'],
callback=self._add_command,
help=self._make_help_string(strings.add_command_help),
)
cmd.instance = self
group.add_command(cmd)
cmd = commands.Command(
name='del',
aliases=['unsubscribe', 'remove', 'delete'],
callback=self._del_command,
help=self._make_help_string(strings.del_command_help),
)
cmd.instance = self
group.add_command(cmd)
cmd = commands.Command(
name='list',
callback=self._list_command,
help=self._make_help_string(strings.list_command_help),
)
cmd.instance = self
group.add_command(cmd)
cmd = commands.Command(
name='enable',
callback=self._enable_command,
help=self._make_help_string(strings.enable_command_help),
)
cmd.instance = self
group.add_command(cmd)
cmd = commands.Command(
name='disable',
callback=self._disable_command,
help=self._make_help_string(strings.disable_command_help),
)
cmd.instance = self
group.add_command(cmd)
return group