Use f-strings in more places that were missed.

This commit is contained in:
Rapptz 2021-04-08 06:02:47 -04:00
parent c3e0b6e123
commit 99fc950510
34 changed files with 220 additions and 143 deletions

View File

@ -279,7 +279,7 @@ class GuildChannel(Protocol):
perms = [] perms = []
for target, perm in overwrites.items(): for target, perm in overwrites.items():
if not isinstance(perm, PermissionOverwrite): if not isinstance(perm, PermissionOverwrite):
raise InvalidArgument('Expected PermissionOverwrite received {0.__name__}'.format(type(perm))) raise InvalidArgument(f'Expected PermissionOverwrite received {perm.__class__.__name__}')
allow, deny = perm.pair() allow, deny = perm.pair()
payload = { payload = {

View File

@ -584,7 +584,7 @@ class Spotify:
return 'Spotify' return 'Spotify'
def __repr__(self): def __repr__(self):
return '<Spotify title={0.title!r} artist={0.artist!r} track_id={0.track_id!r}>'.format(self) return f'<Spotify title={self.title!r} artist={self.artist!r} track_id={self.track_id!r}>'
@property @property
def title(self): def title(self):
@ -738,7 +738,7 @@ class CustomActivity(BaseActivity):
return str(self.name) return str(self.name)
def __repr__(self): def __repr__(self):
return '<CustomActivity name={0.name!r} emoji={0.emoji!r}>'.format(self) return f'<CustomActivity name={self.name!r} emoji={self.emoji!r}>'
def create_activity(data): def create_activity(data):

View File

@ -31,6 +31,7 @@ __all__ = (
'AppInfo', 'AppInfo',
) )
class AppInfo: class AppInfo:
"""Represents the application info for the bot provided by Discord. """Represents the application info for the bot provided by Discord.
@ -95,10 +96,25 @@ class AppInfo:
.. versionadded:: 1.3 .. versionadded:: 1.3
""" """
__slots__ = ('_state', 'description', 'id', 'name', 'rpc_origins',
'bot_public', 'bot_require_code_grant', 'owner', 'icon', __slots__ = (
'summary', 'verify_key', 'team', 'guild_id', 'primary_sku_id', '_state',
'slug', 'cover_image') 'description',
'id',
'name',
'rpc_origins',
'bot_public',
'bot_require_code_grant',
'owner',
'icon',
'summary',
'verify_key',
'team',
'guild_id',
'primary_sku_id',
'slug',
'cover_image',
)
def __init__(self, state, data): def __init__(self, state, data):
self._state = state self._state = state
@ -125,8 +141,11 @@ class AppInfo:
self.cover_image = data.get('cover_image') self.cover_image = data.get('cover_image')
def __repr__(self): def __repr__(self):
return '<{0.__class__.__name__} id={0.id} name={0.name!r} description={0.description!r} public={0.bot_public} ' \ return (
'owner={0.owner!r}>'.format(self) f'<{self.__class__.__name__} id={self.id} name={self.name!r} '
f'description={self.description!r} public={self.bot_public} '
f'owner={self.owner!r}>'
)
@property @property
def icon_url(self): def icon_url(self):
@ -166,7 +185,6 @@ class AppInfo:
""" """
return Asset._from_icon(self._state, self, 'app', format=format, size=size) return Asset._from_icon(self._state, self, 'app', format=format, size=size)
@property @property
def cover_image_url(self): def cover_image_url(self):
""":class:`.Asset`: Retrieves the cover image on a store embed. """:class:`.Asset`: Retrieves the cover image on a store embed.

View File

@ -88,7 +88,7 @@ class Asset:
if format is None: if format is None:
format = 'gif' if user.is_avatar_animated() else static_format format = 'gif' if user.is_avatar_animated() else static_format
return cls(state, '/avatars/{0.id}/{0.avatar}.{1}?size={2}'.format(user, format, size)) return cls(state, f'/avatars/{user.id}/{user.avatar}.{format}?size={size}')
@classmethod @classmethod
def _from_icon(cls, state, object, path, *, format='webp', size=1024): def _from_icon(cls, state, object, path, *, format='webp', size=1024):
@ -100,7 +100,7 @@ class Asset:
if format not in VALID_STATIC_FORMATS: if format not in VALID_STATIC_FORMATS:
raise InvalidArgument(f"format must be None or one of {VALID_STATIC_FORMATS}") raise InvalidArgument(f"format must be None or one of {VALID_STATIC_FORMATS}")
url = '/{0}-icons/{1.id}/{1.icon}.{2}?size={3}'.format(path, object, format, size) url = f'/{path}-icons/{object.id}/{object.icon}.{format}?size={size}'
return cls(state, url) return cls(state, url)
@classmethod @classmethod
@ -113,7 +113,7 @@ class Asset:
if format not in VALID_STATIC_FORMATS: if format not in VALID_STATIC_FORMATS:
raise InvalidArgument(f"format must be None or one of {VALID_STATIC_FORMATS}") raise InvalidArgument(f"format must be None or one of {VALID_STATIC_FORMATS}")
url = '/app-assets/{0.id}/store/{0.cover_image}.{1}?size={2}'.format(obj, format, size) url = f'/app-assets/{obj.id}/store/{obj.cover_image}.{format}?size={size}'
return cls(state, url) return cls(state, url)
@classmethod @classmethod
@ -126,8 +126,7 @@ class Asset:
if hash is None: if hash is None:
return cls(state) return cls(state)
url = '/{key}/{0}/{1}.{2}?size={3}' return cls(state, f'/{key}/{id}/{hash}.{format}?size={size}')
return cls(state, url.format(id, hash, format, size, key=key))
@classmethod @classmethod
def _from_guild_icon(cls, state, guild, *, format=None, static_format='webp', size=1024): def _from_guild_icon(cls, state, guild, *, format=None, static_format='webp', size=1024):
@ -146,14 +145,14 @@ class Asset:
if format is None: if format is None:
format = 'gif' if guild.is_icon_animated() else static_format format = 'gif' if guild.is_icon_animated() else static_format
return cls(state, '/icons/{0.id}/{0.icon}.{1}?size={2}'.format(guild, format, size)) return cls(state, f'/icons/{guild.id}/{guild.icon}.{format}?size={size}')
@classmethod @classmethod
def _from_sticker_url(cls, state, sticker, *, size=1024): def _from_sticker_url(cls, state, sticker, *, size=1024):
if not utils.valid_icon_size(size): if not utils.valid_icon_size(size):
raise InvalidArgument("size must be a power of 2 between 16 and 4096") raise InvalidArgument("size must be a power of 2 between 16 and 4096")
return cls(state, '/stickers/{0.id}/{0.image}.png?size={2}'.format(sticker, format, size)) return cls(state, f'/stickers/{sticker.id}/{sticker.image}.png?size={size}')
@classmethod @classmethod
def _from_emoji(cls, state, emoji, *, format=None, static_format='png'): def _from_emoji(cls, state, emoji, *, format=None, static_format='png'):

View File

@ -301,7 +301,7 @@ class AuditLogEntry(Hashable):
return self.guild.get_member(user_id) or self._users.get(user_id) return self.guild.get_member(user_id) or self._users.get(user_id)
def __repr__(self): def __repr__(self):
return '<AuditLogEntry id={0.id} action={0.action} user={0.user!r}>'.format(self) return f'<AuditLogEntry id={self.id} action={self.action} user={self.user!r}>'
@utils.cached_property @utils.cached_property
def created_at(self): def created_at(self):

View File

@ -320,7 +320,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
return m.author == client.user return m.author == client.user
deleted = await channel.purge(limit=100, check=is_me) deleted = await channel.purge(limit=100, check=is_me)
await channel.send('Deleted {} message(s)'.format(len(deleted))) await channel.send(f'Deleted {len(deleted)} message(s)')
Parameters Parameters
----------- -----------
@ -504,7 +504,7 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
raise ClientException('The channel must be a news channel.') raise ClientException('The channel must be a news channel.')
if not isinstance(destination, TextChannel): if not isinstance(destination, TextChannel):
raise InvalidArgument('Expected TextChannel received {0.__name__}'.format(type(destination))) raise InvalidArgument(f'Expected TextChannel received {destination.__class__.__name__}')
from .webhook import Webhook from .webhook import Webhook
data = await self._state.http.follow_webhook(self.id, webhook_channel_id=destination.id, reason=reason) data = await self._state.http.follow_webhook(self.id, webhook_channel_id=destination.id, reason=reason)
@ -896,7 +896,7 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable):
self._update(guild, data) self._update(guild, data)
def __repr__(self): def __repr__(self):
return '<CategoryChannel id={0.id} name={0.name!r} position={0.position} nsfw={0.nsfw}>'.format(self) return f'<CategoryChannel id={self.id} name={self.name!r} position={self.position} nsfw={self.nsfw}>'
def _update(self, guild, data): def _update(self, guild, data):
self.guild = guild self.guild = guild
@ -1092,7 +1092,7 @@ class StoreChannel(discord.abc.GuildChannel, Hashable):
self._update(guild, data) self._update(guild, data)
def __repr__(self): def __repr__(self):
return '<StoreChannel id={0.id} name={0.name!r} position={0.position} nsfw={0.nsfw}>'.format(self) return f'<StoreChannel id={self.id} name={self.name!r} position={self.position} nsfw={self.nsfw}>'
def _update(self, guild, data): def _update(self, guild, data):
self.guild = guild self.guild = guild
@ -1218,7 +1218,7 @@ class DMChannel(discord.abc.Messageable, Hashable):
return f'Direct Message with {self.recipient}' return f'Direct Message with {self.recipient}'
def __repr__(self): def __repr__(self):
return '<DMChannel id={0.id} recipient={0.recipient!r}>'.format(self) return f'<DMChannel id={self.id} recipient={self.recipient!r}>'
@property @property
def type(self): def type(self):
@ -1354,7 +1354,7 @@ class GroupChannel(discord.abc.Messageable, Hashable):
return ', '.join(map(lambda x: x.name, self.recipients)) return ', '.join(map(lambda x: x.name, self.recipients))
def __repr__(self): def __repr__(self):
return '<GroupChannel id={0.id} name={0.name!r}>'.format(self) return f'<GroupChannel id={self.id} name={self.name!r}>'
@property @property
def type(self): def type(self):

View File

@ -849,7 +849,7 @@ class Client:
return m.content == 'hello' and m.channel == channel return m.content == 'hello' and m.channel == channel
msg = await client.wait_for('message', check=check) msg = await client.wait_for('message', check=check)
await channel.send('Hello {.author}!'.format(msg)) await channel.send(f'Hello {msg.author}!')
Waiting for a thumbs up reaction from the message author: :: Waiting for a thumbs up reaction from the message author: ::

View File

@ -111,11 +111,11 @@ class Emoji(_EmojiTag):
def __str__(self): def __str__(self):
if self.animated: if self.animated:
return '<a:{0.name}:{0.id}>'.format(self) return f'<a:{self.name}:{self.id}>'
return "<:{0.name}:{0.id}>".format(self) return f'<:{self.name}:{self.id}>'
def __repr__(self): def __repr__(self):
return '<Emoji id={0.id} name={0.name!r} animated={0.animated} managed={0.managed}>'.format(self) return f'<Emoji id={self.id} name={self.name!r} animated={self.animated} managed={self.managed}>'
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, _EmojiTag) and self.id == other.id return isinstance(other, _EmojiTag) and self.id == other.id

View File

@ -846,7 +846,7 @@ class BotBase(GroupMixin):
raise raise
raise TypeError("command_prefix must be plain string, iterable of strings, or callable " raise TypeError("command_prefix must be plain string, iterable of strings, or callable "
"returning either of these, not {}".format(ret.__class__.__name__)) f"returning either of these, not {ret.__class__.__name__}")
if not ret: if not ret:
raise ValueError("Iterable command_prefix must contain at least one prefix") raise ValueError("Iterable command_prefix must contain at least one prefix")
@ -907,13 +907,13 @@ class BotBase(GroupMixin):
except TypeError: except TypeError:
if not isinstance(prefix, list): if not isinstance(prefix, list):
raise TypeError("get_prefix must return either a string or a list of string, " raise TypeError("get_prefix must return either a string or a list of string, "
"not {}".format(prefix.__class__.__name__)) f"not {prefix.__class__.__name__}")
# It's possible a bad command_prefix got us here. # It's possible a bad command_prefix got us here.
for value in prefix: for value in prefix:
if not isinstance(value, str): if not isinstance(value, str):
raise TypeError("Iterable command_prefix or list returned from get_prefix must " raise TypeError("Iterable command_prefix or list returned from get_prefix must "
"contain only strings, not {}".format(value.__class__.__name__)) f"contain only strings, not {value.__class__.__name__}")
# Getting here shouldn't happen # Getting here shouldn't happen
raise raise

View File

@ -131,7 +131,7 @@ class Cooldown:
return Cooldown(self.rate, self.per, self.type) return Cooldown(self.rate, self.per, self.type)
def __repr__(self): def __repr__(self):
return '<Cooldown rate: {0.rate} per: {0.per} window: {0._window} tokens: {0._tokens}>'.format(self) return f'<Cooldown rate: {self.rate} per: {self.per} window: {self._window} tokens: {self._tokens}>'
class CooldownMapping: class CooldownMapping:
def __init__(self, original): def __init__(self, original):
@ -202,7 +202,7 @@ class _Semaphore:
self._waiters = deque() self._waiters = deque()
def __repr__(self): def __repr__(self):
return '<_Semaphore value={0.value} waiters={1}>'.format(self, len(self._waiters)) return f'<_Semaphore value={self.value} waiters={len(self._waiters)}>'
def locked(self): def locked(self):
return self.value == 0 return self.value == 0
@ -259,7 +259,7 @@ class MaxConcurrency:
return self.__class__(self.number, per=self.per, wait=self.wait) return self.__class__(self.number, per=self.per, wait=self.wait)
def __repr__(self): def __repr__(self):
return '<MaxConcurrency per={0.per!r} number={0.number} wait={0.wait}>'.format(self) return f'<MaxConcurrency per={self.per!r} number={self.number} wait={self.wait}>'
def get_key(self, message): def get_key(self, message):
return self.per.get_key(message) return self.per.get_key(message)

View File

@ -695,15 +695,13 @@ class Command(_BaseCommand):
try: try:
next(iterator) next(iterator)
except StopIteration: except StopIteration:
fmt = 'Callback for {0.name} command is missing "self" parameter.' raise discord.ClientException(f'Callback for {self.name} command is missing "self" parameter.')
raise discord.ClientException(fmt.format(self))
# next we have the 'ctx' as the next parameter # next we have the 'ctx' as the next parameter
try: try:
next(iterator) next(iterator)
except StopIteration: except StopIteration:
fmt = 'Callback for {0.name} command is missing "ctx" parameter.' raise discord.ClientException(f'Callback for {self.name} command is missing "ctx" parameter.')
raise discord.ClientException(fmt.format(self))
for name, param in iterator: for name, param in iterator:
if param.kind == param.POSITIONAL_OR_KEYWORD or param.kind == param.POSITIONAL_ONLY: if param.kind == param.POSITIONAL_OR_KEYWORD or param.kind == param.POSITIONAL_ONLY:
@ -2046,7 +2044,7 @@ def before_invoke(coro):
@commands.before_invoke(record_usage) @commands.before_invoke(record_usage)
@commands.command() @commands.command()
async def when(self, ctx): # Output: <User> used when at <Time> async def when(self, ctx): # Output: <User> used when at <Time>
await ctx.send('and i have existed since {}'.format(ctx.bot.user.created_at)) await ctx.send(f'and i have existed since {ctx.bot.user.created_at}')
@commands.command() @commands.command()
async def where(self, ctx): # Output: <Nothing> async def where(self, ctx): # Output: <Nothing>

View File

@ -424,7 +424,7 @@ class CommandInvokeError(CommandError):
""" """
def __init__(self, e): def __init__(self, e):
self.original = e self.original = e
super().__init__('Command raised an exception: {0.__class__.__name__}: {0}'.format(e)) super().__init__(f'Command raised an exception: {e.__class__.__name__}: {e}')
class CommandOnCooldown(CommandError): class CommandOnCooldown(CommandError):
"""Exception raised when the command being invoked is on cooldown. """Exception raised when the command being invoked is on cooldown.
@ -764,8 +764,8 @@ class ExtensionFailed(ExtensionError):
""" """
def __init__(self, name, original): def __init__(self, name, original):
self.original = original self.original = original
fmt = 'Extension {0!r} raised an error: {1.__class__.__name__}: {1}' msg = f'Extension {name!r} raised an error: {original.__class__.__name__}: {original}'
super().__init__(fmt.format(name, original), name=name) super().__init__(msg, name=name)
class ExtensionNotFound(ExtensionError): class ExtensionNotFound(ExtensionError):
"""An exception raised when an extension is not found. """An exception raised when an extension is not found.
@ -784,8 +784,8 @@ class ExtensionNotFound(ExtensionError):
""" """
def __init__(self, name, original=None): def __init__(self, name, original=None):
self.original = None self.original = None
fmt = 'Extension {0!r} could not be loaded.' msg = f'Extension {name!r} could not be loaded.'
super().__init__(fmt.format(name), name=name) super().__init__(msg, name=name)
class CommandRegistrationError(ClientException): class CommandRegistrationError(ClientException):
"""An exception raised when the command can't be added """An exception raised when the command can't be added

View File

@ -60,6 +60,7 @@ __all__ = (
# Type <prefix>help command for more info on a command. # Type <prefix>help command for more info on a command.
# You can also type <prefix>help category for more info on a category. # You can also type <prefix>help category for more info on a category.
class Paginator: class Paginator:
"""A class that aids in paginating code blocks for Discord messages. """A class that aids in paginating code blocks for Discord messages.
@ -81,6 +82,7 @@ class Paginator:
The character string inserted between lines. e.g. a newline character. The character string inserted between lines. e.g. a newline character.
.. versionadded:: 1.7 .. versionadded:: 1.7
""" """
def __init__(self, prefix='```', suffix='```', max_size=2000, linesep='\n'): def __init__(self, prefix='```', suffix='```', max_size=2000, linesep='\n'):
self.prefix = prefix self.prefix = prefix
self.suffix = suffix self.suffix = suffix
@ -171,10 +173,12 @@ class Paginator:
fmt = '<Paginator prefix: {0.prefix!r} suffix: {0.suffix!r} linesep: {0.linesep!r} max_size: {0.max_size} count: {0._count}>' fmt = '<Paginator prefix: {0.prefix!r} suffix: {0.suffix!r} linesep: {0.linesep!r} max_size: {0.max_size} count: {0._count}>'
return fmt.format(self) return fmt.format(self)
def _not_overriden(f): def _not_overriden(f):
f.__help_command_not_overriden__ = True f.__help_command_not_overriden__ = True
return f return f
class _HelpCommandImpl(Command): class _HelpCommandImpl(Command):
def __init__(self, inject, *args, **kwargs): def __init__(self, inject, *args, **kwargs):
super().__init__(inject.command_callback, *args, **kwargs) super().__init__(inject.command_callback, *args, **kwargs)
@ -250,6 +254,7 @@ class _HelpCommandImpl(Command):
cog.walk_commands = cog.walk_commands.__wrapped__ cog.walk_commands = cog.walk_commands.__wrapped__
self.cog = None self.cog = None
class HelpCommand: class HelpCommand:
r"""The base implementation for help command formatting. r"""The base implementation for help command formatting.
@ -288,7 +293,7 @@ class HelpCommand:
'@everyone': '@\u200beveryone', '@everyone': '@\u200beveryone',
'@here': '@\u200bhere', '@here': '@\u200bhere',
r'<@!?[0-9]{17,22}>': '@deleted-user', r'<@!?[0-9]{17,22}>': '@deleted-user',
r'<@&[0-9]{17,22}>': '@deleted-role' r'<@&[0-9]{17,22}>': '@deleted-role',
} }
MENTION_PATTERN = re.compile('|'.join(MENTION_TRANSFORMS.keys())) MENTION_PATTERN = re.compile('|'.join(MENTION_TRANSFORMS.keys()))
@ -305,10 +310,7 @@ class HelpCommand:
# The keys can be safely copied as-is since they're 99.99% certain of being # The keys can be safely copied as-is since they're 99.99% certain of being
# string keys # string keys
deepcopy = copy.deepcopy deepcopy = copy.deepcopy
self.__original_kwargs__ = { self.__original_kwargs__ = {k: deepcopy(v) for k, v in kwargs.items()}
k: deepcopy(v)
for k, v in kwargs.items()
}
self.__original_args__ = deepcopy(args) self.__original_args__ = deepcopy(args)
return self return self
@ -369,10 +371,7 @@ class HelpCommand:
def get_bot_mapping(self): def get_bot_mapping(self):
"""Retrieves the bot mapping passed to :meth:`send_bot_help`.""" """Retrieves the bot mapping passed to :meth:`send_bot_help`."""
bot = self.context.bot bot = self.context.bot
mapping = { mapping = {cog: cog.get_commands() for cog in bot.cogs.values()}
cog: cog.get_commands()
for cog in bot.cogs.values()
}
mapping[None] = [c for c in bot.commands if c.cog is None] mapping[None] = [c for c in bot.commands if c.cog is None]
return mapping return mapping
@ -607,10 +606,7 @@ class HelpCommand:
The maximum width of the commands. The maximum width of the commands.
""" """
as_lengths = ( as_lengths = (discord.utils._string_width(c.name) for c in commands)
discord.utils._string_width(c.name)
for c in commands
)
return max(as_lengths, default=0) return max(as_lengths, default=0)
def get_destination(self): def get_destination(self):
@ -880,6 +876,7 @@ class HelpCommand:
else: else:
return await self.send_command_help(cmd) return await self.send_command_help(cmd)
class DefaultHelpCommand(HelpCommand): class DefaultHelpCommand(HelpCommand):
"""The implementation of the default help command. """The implementation of the default help command.
@ -940,8 +937,10 @@ class DefaultHelpCommand(HelpCommand):
def get_ending_note(self): def get_ending_note(self):
""":class:`str`: Returns help command's ending note. This is mainly useful to override for i18n purposes.""" """:class:`str`: Returns help command's ending note. This is mainly useful to override for i18n purposes."""
command_name = self.invoked_with command_name = self.invoked_with
return f"Type {self.clean_prefix}{command_name} command for more info on a command.\n" \ return (
f"Type {self.clean_prefix}{command_name} command for more info on a command.\n"
f"You can also type {self.clean_prefix}{command_name} category for more info on a category." f"You can also type {self.clean_prefix}{command_name} category for more info on a category."
)
def add_indented_commands(self, commands, *, heading, max_size=None): def add_indented_commands(self, commands, *, heading, max_size=None):
"""Indents a list of commands after the specified heading. """Indents a list of commands after the specified heading.
@ -1030,6 +1029,7 @@ class DefaultHelpCommand(HelpCommand):
self.paginator.add_line(bot.description, empty=True) self.paginator.add_line(bot.description, empty=True)
no_category = f'\u200b{self.no_category}:' no_category = f'\u200b{self.no_category}:'
def get_category(command, *, no_category=no_category): def get_category(command, *, no_category=no_category):
cog = command.cog cog = command.cog
return cog.qualified_name + ':' if cog is not None else no_category return cog.qualified_name + ':' if cog is not None else no_category
@ -1083,6 +1083,7 @@ class DefaultHelpCommand(HelpCommand):
await self.send_pages() await self.send_pages()
class MinimalHelpCommand(HelpCommand): class MinimalHelpCommand(HelpCommand):
"""An implementation of a help command with minimal output. """An implementation of a help command with minimal output.
@ -1149,8 +1150,10 @@ class MinimalHelpCommand(HelpCommand):
The help command opening note. The help command opening note.
""" """
command_name = self.invoked_with command_name = self.invoked_with
return "Use `{0}{1} [command]` for more info on a command.\n" \ return (
"You can also use `{0}{1} [category]` for more info on a category.".format(self.clean_prefix, command_name) f"Use `{self.clean_prefix}{command_name} [command]` for more info on a command.\n"
f"You can also use `{self.clean_prefix}{command_name} [category]` for more info on a category."
)
def get_command_signature(self, command): def get_command_signature(self, command):
return f'{self.clean_prefix}{command.qualified_name} {command.signature}' return f'{self.clean_prefix}{command.qualified_name} {command.signature}'
@ -1273,6 +1276,7 @@ class MinimalHelpCommand(HelpCommand):
self.paginator.add_line(note, empty=True) self.paginator.add_line(note, empty=True)
no_category = f'\u200b{self.no_category}' no_category = f'\u200b{self.no_category}'
def get_category(command, *, no_category=no_category): def get_category(command, *, no_category=no_category):
cog = command.cog cog = command.cog
return cog.qualified_name if cog is not None else no_category return cog.qualified_name if cog is not None else no_category

View File

@ -189,4 +189,4 @@ class StringView:
def __repr__(self): def __repr__(self):
return '<StringView pos: {0.index} prev: {0.previous} end: {0.end} eof: {0.eof}>'.format(self) return f'<StringView pos: {self.index} prev: {self.previous} end: {self.end} eof: {self.eof}>'

View File

@ -75,7 +75,7 @@ class Loop:
self._next_iteration = None self._next_iteration = None
if not inspect.iscoroutinefunction(self.coro): if not inspect.iscoroutinefunction(self.coro):
raise TypeError('Expected coroutine function, not {0.__name__!r}.'.format(type(self.coro))) raise TypeError(f'Expected coroutine function, not {type(self.coro).__name__!r}.')
async def _call_loop_function(self, name, *args, **kwargs): async def _call_loop_function(self, name, *args, **kwargs):
coro = getattr(self, '_' + name) coro = getattr(self, '_' + name)
@ -370,7 +370,7 @@ class Loop:
""" """
if not inspect.iscoroutinefunction(coro): if not inspect.iscoroutinefunction(coro):
raise TypeError('Expected coroutine function, received {0.__name__!r}.'.format(type(coro))) raise TypeError(f'Expected coroutine function, received {coro.__class__.__name__!r}.')
self._before_loop = coro self._before_loop = coro
return coro return coro
@ -398,7 +398,7 @@ class Loop:
""" """
if not inspect.iscoroutinefunction(coro): if not inspect.iscoroutinefunction(coro):
raise TypeError('Expected coroutine function, received {0.__name__!r}.'.format(type(coro))) raise TypeError(f'Expected coroutine function, received {coro.__class__.__name__!r}.')
self._after_loop = coro self._after_loop = coro
return coro return coro
@ -424,7 +424,7 @@ class Loop:
The function was not a coroutine. The function was not a coroutine.
""" """
if not inspect.iscoroutinefunction(coro): if not inspect.iscoroutinefunction(coro):
raise TypeError('Expected coroutine function, received {0.__name__!r}.'.format(type(coro))) raise TypeError(f'Expected coroutine function, received {coro.__class__.__name__!r}.')
self._error = coro self._error = coro
return coro return coro

View File

@ -839,7 +839,7 @@ class Guild(Hashable):
perms = [] perms = []
for target, perm in overwrites.items(): for target, perm in overwrites.items():
if not isinstance(perm, PermissionOverwrite): if not isinstance(perm, PermissionOverwrite):
raise InvalidArgument('Expected PermissionOverwrite received {0.__name__}'.format(type(perm))) raise InvalidArgument(f'Expected PermissionOverwrite received {perm.__class__.__name__}')
allow, deny = perm.pair() allow, deny = perm.pair()
payload = { payload = {
@ -2105,17 +2105,17 @@ class Guild(Hashable):
Getting the first 100 entries: :: Getting the first 100 entries: ::
async for entry in guild.audit_logs(limit=100): async for entry in guild.audit_logs(limit=100):
print('{0.user} did {0.action} to {0.target}'.format(entry)) print(f'{entry.user} did {entry.action} to {entry.target}')
Getting entries for a specific action: :: Getting entries for a specific action: ::
async for entry in guild.audit_logs(action=discord.AuditLogAction.ban): async for entry in guild.audit_logs(action=discord.AuditLogAction.ban):
print('{0.user} banned {0.target}'.format(entry)) print(f'{entry.user} banned {entry.target}')
Getting entries made by a specific user: :: Getting entries made by a specific user: ::
entries = await guild.audit_logs(limit=None, user=guild.me).flatten() entries = await guild.audit_logs(limit=None, user=guild.me).flatten()
await channel.send('I made {} moderation actions.'.format(len(entries))) await channel.send(f'I made {len(entries)} moderation actions.')
Parameters Parameters
----------- -----------

View File

@ -587,7 +587,7 @@ class HTTPClient:
r = Route('DELETE', '/guilds/{guild_id}/members/{user_id}', guild_id=guild_id, user_id=user_id) r = Route('DELETE', '/guilds/{guild_id}/members/{user_id}', guild_id=guild_id, user_id=user_id)
if reason: if reason:
# thanks aiohttp # thanks aiohttp
r.url = '{0.url}?reason={1}'.format(r, _uriquote(reason)) r.url = f'{r.url}?reason={_uriquote(reason)}'
return self.request(r) return self.request(r)
@ -599,7 +599,7 @@ class HTTPClient:
if reason: if reason:
# thanks aiohttp # thanks aiohttp
r.url = '{0.url}?reason={1}'.format(r, _uriquote(reason)) r.url = f'{r.url}?reason={_uriquote(reason)}'
return self.request(r, params=params) return self.request(r, params=params)

View File

@ -53,7 +53,7 @@ class IntegrationAccount:
self.name = kwargs.pop('name') self.name = kwargs.pop('name')
def __repr__(self): def __repr__(self):
return '<IntegrationAccount id={0.id} name={0.name!r}>'.format(self) return f'<IntegrationAccount id={self.id} name={self.name!r}>'
class Integration: class Integration:
"""Represents a guild integration. """Represents a guild integration.
@ -101,7 +101,7 @@ class Integration:
self._from_data(data) self._from_data(data)
def __repr__(self): def __repr__(self):
return '<Integration id={0.id} name={0.name!r} type={0.type!r}>'.format(self) return f'<Integration id={self.id} name={self.name!r} type={self.type!r}>'
def _from_data(self, integ): def _from_data(self, integ):
self.id = _get_as_snowflake(integ, 'id') self.id = _get_as_snowflake(integ, 'id')

View File

@ -34,6 +34,7 @@ __all__ = (
'Invite', 'Invite',
) )
class PartialInviteChannel: class PartialInviteChannel:
"""Represents a "partial" invite channel. """Represents a "partial" invite channel.
@ -79,7 +80,7 @@ class PartialInviteChannel:
return self.name return self.name
def __repr__(self): def __repr__(self):
return '<PartialInviteChannel id={0.id} name={0.name} type={0.type!r}>'.format(self) return f'<PartialInviteChannel id={self.id} name={self.name} type={self.type!r}>'
@property @property
def mention(self): def mention(self):
@ -91,6 +92,7 @@ class PartialInviteChannel:
""":class:`datetime.datetime`: Returns the channel's creation time in UTC.""" """:class:`datetime.datetime`: Returns the channel's creation time in UTC."""
return snowflake_time(self.id) return snowflake_time(self.id)
class PartialInviteGuild: class PartialInviteGuild:
"""Represents a "partial" invite guild. """Represents a "partial" invite guild.
@ -135,8 +137,7 @@ class PartialInviteGuild:
The partial guild's description. The partial guild's description.
""" """
__slots__ = ('_state', 'features', 'icon', 'banner', 'id', 'name', 'splash', __slots__ = ('_state', 'features', 'icon', 'banner', 'id', 'name', 'splash', 'verification_level', 'description')
'verification_level', 'description')
def __init__(self, state, data, id): def __init__(self, state, data, id):
self._state = state self._state = state
@ -153,8 +154,10 @@ class PartialInviteGuild:
return self.name return self.name
def __repr__(self): def __repr__(self):
return '<{0.__class__.__name__} id={0.id} name={0.name!r} features={0.features} ' \ return (
'description={0.description!r}>'.format(self) f'<{self.__class__.__name__} id={self.id} name={self.name!r} features={self.features} '
f'description={self.description!r}>'
)
@property @property
def created_at(self): def created_at(self):
@ -213,6 +216,7 @@ class PartialInviteGuild:
""" """
return Asset._from_guild_image(self._state, self.id, self.splash, 'splashes', format=format, size=size) return Asset._from_guild_image(self._state, self.id, self.splash, 'splashes', format=format, size=size)
class Invite(Hashable): class Invite(Hashable):
r"""Represents a Discord :class:`Guild` or :class:`abc.GuildChannel` invite. r"""Represents a Discord :class:`Guild` or :class:`abc.GuildChannel` invite.
@ -291,9 +295,21 @@ class Invite(Hashable):
The channel the invite is for. The channel the invite is for.
""" """
__slots__ = ('max_age', 'code', 'guild', 'revoked', 'created_at', 'uses', __slots__ = (
'temporary', 'max_uses', 'inviter', 'channel', '_state', 'max_age',
'approximate_member_count', 'approximate_presence_count' ) 'code',
'guild',
'revoked',
'created_at',
'uses',
'temporary',
'max_uses',
'inviter',
'channel',
'_state',
'approximate_member_count',
'approximate_presence_count',
)
BASE = 'https://discord.gg' BASE = 'https://discord.gg'
@ -361,9 +377,11 @@ class Invite(Hashable):
return self.url return self.url
def __repr__(self): def __repr__(self):
return '<Invite code={0.code!r} guild={0.guild!r} ' \ return (
'online={0.approximate_presence_count} ' \ f'<Invite code={self.code!r} guild={self.guild!r} '
'members={0.approximate_member_count}>'.format(self) f'online={self.approximate_presence_count} '
f'members={self.approximate_member_count}>'
)
def __hash__(self): def __hash__(self):
return hash(self.code) return hash(self.code)

View File

@ -26,6 +26,7 @@ __all__ = (
'AllowedMentions', 'AllowedMentions',
) )
class _FakeBool: class _FakeBool:
def __repr__(self): def __repr__(self):
return 'True' return 'True'
@ -36,8 +37,10 @@ class _FakeBool:
def __bool__(self): def __bool__(self):
return True return True
default = _FakeBool() default = _FakeBool()
class AllowedMentions: class AllowedMentions:
"""A class that represents what mentions are allowed in a message. """A class that represents what mentions are allowed in a message.
@ -126,4 +129,7 @@ class AllowedMentions:
return AllowedMentions(everyone=everyone, roles=roles, users=users, replied_user=replied_user) return AllowedMentions(everyone=everyone, roles=roles, users=users, replied_user=replied_user)
def __repr__(self): def __repr__(self):
return '{0.__class__.__qualname__}(everyone={0.everyone}, users={0.users}, roles={0.roles}, replied_user={0.replied_user})'.format(self) return (
f'{self.__class__.__name__}(everyone={self.everyone}, '
f'users={self.users}, roles={self.roles}, replied_user={self.replied_user})'
)

View File

@ -132,7 +132,7 @@ class Attachment(Hashable):
return self.filename.startswith('SPOILER_') return self.filename.startswith('SPOILER_')
def __repr__(self): def __repr__(self):
return '<Attachment id={0.id} filename={0.filename!r} url={0.url!r}>'.format(self) return f'<Attachment id={self.id} filename={self.filename!r} url={self.url!r}>'
def __str__(self): def __str__(self):
return self.url or '' return self.url or ''
@ -380,10 +380,10 @@ class MessageReference:
.. versionadded:: 1.7 .. versionadded:: 1.7
""" """
guild_id = self.guild_id if self.guild_id is not None else '@me' guild_id = self.guild_id if self.guild_id is not None else '@me'
return 'https://discord.com/channels/{0}/{1.channel_id}/{1.message_id}'.format(guild_id, self) return f'https://discord.com/channels/{guild_id}/{self.channel_id}/{self.message_id}'
def __repr__(self): def __repr__(self):
return '<MessageReference message_id={0.message_id!r} channel_id={0.channel_id!r} guild_id={0.guild_id!r}>'.format(self) return f'<MessageReference message_id={self.message_id!r} channel_id={self.channel_id!r} guild_id={self.guild_id!r}>'
def to_dict(self): def to_dict(self):
result = {'message_id': self.message_id} if self.message_id is not None else {} result = {'message_id': self.message_id} if self.message_id is not None else {}
@ -580,7 +580,7 @@ class Message(Hashable):
continue continue
def __repr__(self): def __repr__(self):
return '<Message id={0.id} channel={0.channel!r} type={0.type!r} author={0.author!r} flags={0.flags!r}>'.format(self) return f'<Message id={self.id} channel={self.channel!r} type={self.type!r} author={self.author!r} flags={self.flags!r}>'
def _try_patch(self, data, key, transform=None): def _try_patch(self, data, key, transform=None):
try: try:
@ -849,7 +849,7 @@ class Message(Hashable):
def jump_url(self): def jump_url(self):
""":class:`str`: Returns a URL that allows the client to jump to this message.""" """:class:`str`: Returns a URL that allows the client to jump to this message."""
guild_id = getattr(self.guild, 'id', '@me') guild_id = getattr(self.guild, 'id', '@me')
return 'https://discord.com/channels/{0}/{1.channel.id}/{1.id}'.format(guild_id, self) return f'https://discord.com/channels/{guild_id}/{self.channel.id}/{self.id}'
def is_system(self): def is_system(self):
""":class:`bool`: Whether the message is a system message. """:class:`bool`: Whether the message is a system message.
@ -875,13 +875,13 @@ class Message(Hashable):
return f'{self.author.name} pinned a message to this channel.' return f'{self.author.name} pinned a message to this channel.'
if self.type is MessageType.recipient_add: if self.type is MessageType.recipient_add:
return '{0.name} added {1.name} to the group.'.format(self.author, self.mentions[0]) return f'{self.author.name} added {self.mentions[0].name} to the group.'
if self.type is MessageType.recipient_remove: if self.type is MessageType.recipient_remove:
return '{0.name} removed {1.name} from the group.'.format(self.author, self.mentions[0]) return f'{self.author.name} removed {self.mentions[0].name} from the group.'
if self.type is MessageType.channel_name_change: if self.type is MessageType.channel_name_change:
return '{0.author.name} changed the channel name: {0.content}'.format(self) return f'{self.author.name} changed the channel name: {self.content}'
if self.type is MessageType.channel_icon_change: if self.type is MessageType.channel_icon_change:
return f'{self.author.name} changed the channel icon.' return f'{self.author.name} changed the channel icon.'
@ -910,19 +910,19 @@ class Message(Hashable):
return f'{self.author.name} just boosted the server!' return f'{self.author.name} just boosted the server!'
if self.type is MessageType.premium_guild_tier_1: if self.type is MessageType.premium_guild_tier_1:
return '{0.author.name} just boosted the server! {0.guild} has achieved **Level 1!**'.format(self) return f'{self.author.name} just boosted the server! {self.guild} has achieved **Level 1!**'
if self.type is MessageType.premium_guild_tier_2: if self.type is MessageType.premium_guild_tier_2:
return '{0.author.name} just boosted the server! {0.guild} has achieved **Level 2!**'.format(self) return f'{self.author.name} just boosted the server! {self.guild} has achieved **Level 2!**'
if self.type is MessageType.premium_guild_tier_3: if self.type is MessageType.premium_guild_tier_3:
return '{0.author.name} just boosted the server! {0.guild} has achieved **Level 3!**'.format(self) return f'{self.author.name} just boosted the server! {self.guild} has achieved **Level 3!**'
if self.type is MessageType.channel_follow_add: if self.type is MessageType.channel_follow_add:
return '{0.author.name} has added {0.content} to this channel'.format(self) return f'{self.author.name} has added {self.content} to this channel'
if self.type is MessageType.guild_stream: if self.type is MessageType.guild_stream:
return '{0.author.name} is live! Now streaming {0.author.activity.name}'.format(self) return f'{self.author.name} is live! Now streaming {self.author.activity.name}'
if self.type is MessageType.guild_discovery_disqualified: if self.type is MessageType.guild_discovery_disqualified:
return 'This server has been removed from Server Discovery because it no longer passes all the requirements. Check Server Settings for more details.' return 'This server has been removed from Server Discovery because it no longer passes all the requirements. Check Server Settings for more details.'
@ -1394,7 +1394,7 @@ class PartialMessage(Hashable):
pinned = property(None, lambda x, y: ...) pinned = property(None, lambda x, y: ...)
def __repr__(self): def __repr__(self):
return '<PartialMessage id={0.id} channel={0.channel!r}>'.format(self) return f'<PartialMessage id={self.id} channel={self.channel!r}>'
@property @property
def created_at(self): def created_at(self):

View File

@ -108,7 +108,7 @@ class PartialEmoji(_EmojiTag):
return f'<:{self.name}:{self.id}>' return f'<:{self.name}:{self.id}>'
def __repr__(self): def __repr__(self):
return '<{0.__class__.__name__} animated={0.animated} name={0.name!r} id={0.id}>'.format(self) return f'<{self.__class__.__name__} animated={self.animated} name={self.name!r} id={self.id}>'
def __eq__(self, other): def __eq__(self, other):
if self.is_unicode_emoji(): if self.is_unicode_emoji():

View File

@ -144,7 +144,7 @@ class FFmpegAudio(AudioSource):
executable = args.partition(' ')[0] if isinstance(args, str) else args[0] executable = args.partition(' ')[0] if isinstance(args, str) else args[0]
raise ClientException(executable + ' was not found.') from None raise ClientException(executable + ' was not found.') from None
except subprocess.SubprocessError as exc: except subprocess.SubprocessError as exc:
raise ClientException('Popen failed: {0.__class__.__name__}: {0}'.format(exc)) from exc raise ClientException(f'Popen failed: {exc.__class__.__name__}: {exc}') from exc
else: else:
return process return process

View File

@ -93,7 +93,7 @@ class Reaction:
return str(self.emoji) return str(self.emoji)
def __repr__(self): def __repr__(self):
return '<Reaction emoji={0.emoji!r} me={0.me} count={0.count}>'.format(self) return f'<Reaction emoji={self.emoji!r} me={self.me} count={self.count}>'
async def remove(self, user): async def remove(self, user):
"""|coro| """|coro|
@ -158,14 +158,14 @@ class Reaction:
# I do not actually recommend doing this. # I do not actually recommend doing this.
async for user in reaction.users(): async for user in reaction.users():
await channel.send('{0} has reacted with {1.emoji}!'.format(user, reaction)) await channel.send(f'{user} has reacted with {reaction.emoji}!')
Flattening into a list: :: Flattening into a list: ::
users = await reaction.users().flatten() users = await reaction.users().flatten()
# users is now a list of User... # users is now a list of User...
winner = random.choice(users) winner = random.choice(users)
await channel.send('{} has won the raffle.'.format(winner)) await channel.send(f'{winner} has won the raffle.')
Parameters Parameters
------------ ------------
@ -191,7 +191,7 @@ class Reaction:
""" """
if self.custom_emoji: if self.custom_emoji:
emoji = '{0.name}:{0.id}'.format(self.emoji) emoji = f'{self.emoji.name}:{self.emoji.id}'
else: else:
emoji = self.emoji emoji = self.emoji

View File

@ -33,6 +33,7 @@ __all__ = (
'Role', 'Role',
) )
class RoleTags: class RoleTags:
"""Represents tags on a role. """Represents tags on a role.
@ -52,7 +53,11 @@ class RoleTags:
The integration ID that manages the role. The integration ID that manages the role.
""" """
__slots__ = ('bot_id', 'integration_id', '_premium_subscriber',) __slots__ = (
'bot_id',
'integration_id',
'_premium_subscriber',
)
def __init__(self, data): def __init__(self, data):
self.bot_id = _get_as_snowflake(data, 'bot_id') self.bot_id = _get_as_snowflake(data, 'bot_id')
@ -76,8 +81,11 @@ class RoleTags:
return self.integration_id is not None return self.integration_id is not None
def __repr__(self): def __repr__(self):
return '<RoleTags bot_id={0.bot_id} integration_id={0.integration_id} ' \ return (
'premium_subscriber={1}>'.format(self, self.is_premium_subscriber()) f'<RoleTags bot_id={self.bot_id} integration_id={self.integration_id} '
f'premium_subscriber={self.is_premium_subscriber()}>'
)
class Role(Hashable): class Role(Hashable):
"""Represents a Discord role in a :class:`Guild`. """Represents a Discord role in a :class:`Guild`.
@ -138,8 +146,19 @@ class Role(Hashable):
The role tags associated with this role. The role tags associated with this role.
""" """
__slots__ = ('id', 'name', '_permissions', '_colour', 'position', __slots__ = (
'managed', 'mentionable', 'hoist', 'guild', 'tags', '_state') 'id',
'name',
'_permissions',
'_colour',
'position',
'managed',
'mentionable',
'hoist',
'guild',
'tags',
'_state',
)
def __init__(self, *, guild, state, data): def __init__(self, *, guild, state, data):
self.guild = guild self.guild = guild
@ -151,7 +170,7 @@ class Role(Hashable):
return self.name return self.name
def __repr__(self): def __repr__(self):
return '<Role id={0.id} name={0.name!r}>'.format(self) return f'<Role id={self.id} name={self.name!r}>'
def __lt__(self, other): def __lt__(self, other):
if not isinstance(other, Role) or not isinstance(self, Role): if not isinstance(other, Role) or not isinstance(self, Role):
@ -346,7 +365,7 @@ class Role(Hashable):
'permissions': str(fields.get('permissions', self.permissions).value), 'permissions': str(fields.get('permissions', self.permissions).value),
'color': colour.value, 'color': colour.value,
'hoist': fields.get('hoist', self.hoist), 'hoist': fields.get('hoist', self.hoist),
'mentionable': fields.get('mentionable', self.mentionable) 'mentionable': fields.get('mentionable', self.mentionable),
} }
data = await self._state.http.edit_role(self.guild.id, self.id, reason=reason, **payload) data = await self._state.http.edit_role(self.guild.id, self.id, reason=reason, **payload)

View File

@ -88,7 +88,7 @@ class Sticker(Hashable):
self.preview_image = data.get('preview_asset') self.preview_image = data.get('preview_asset')
def __repr__(self): def __repr__(self):
return '<{0.__class__.__name__} id={0.id} name={0.name!r}>'.format(self) return f'<{self.__class__.__name__} id={self.id} name={self.name!r}>'
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -32,6 +32,7 @@ __all__ = (
'TeamMember', 'TeamMember',
) )
class Team: class Team:
"""Represents an application team for a bot provided by Discord. """Represents an application team for a bot provided by Discord.
@ -50,6 +51,7 @@ class Team:
.. versionadded:: 1.3 .. versionadded:: 1.3
""" """
__slots__ = ('_state', 'id', 'name', 'icon', 'owner_id', 'members') __slots__ = ('_state', 'id', 'name', 'icon', 'owner_id', 'members')
def __init__(self, state, data): def __init__(self, state, data):
@ -62,7 +64,7 @@ class Team:
self.members = [TeamMember(self, self._state, member) for member in data['members']] self.members = [TeamMember(self, self._state, member) for member in data['members']]
def __repr__(self): def __repr__(self):
return '<{0.__class__.__name__} id={0.id} name={0.name}>'.format(self) return f'<{self.__class__.__name__} id={self.id} name={self.name}>'
@property @property
def icon_url(self): def icon_url(self):
@ -105,6 +107,7 @@ class Team:
"""Optional[:class:`TeamMember`]: The team's owner.""" """Optional[:class:`TeamMember`]: The team's owner."""
return utils.get(self.members, id=self.owner_id) return utils.get(self.members, id=self.owner_id)
class TeamMember(BaseUser): class TeamMember(BaseUser):
"""Represents a team member in a team. """Represents a team member in a team.
@ -145,6 +148,7 @@ class TeamMember(BaseUser):
membership_state: :class:`TeamMembershipState` membership_state: :class:`TeamMembershipState`
The membership state of the member (e.g. invited or accepted) The membership state of the member (e.g. invited or accepted)
""" """
__slots__ = BaseUser.__slots__ + ('team', 'membership_state', 'permissions') __slots__ = BaseUser.__slots__ + ('team', 'membership_state', 'permissions')
def __init__(self, team, state, data): def __init__(self, team, state, data):
@ -154,5 +158,7 @@ class TeamMember(BaseUser):
super().__init__(state=state, data=data['user']) super().__init__(state=state, data=data['user'])
def __repr__(self): def __repr__(self):
return '<{0.__class__.__name__} id={0.id} name={0.name!r} ' \ return (
'discriminator={0.discriminator!r} membership_state={0.membership_state!r}>'.format(self) f'<{self.__class__.__name__} id={self.id} name={self.name!r} '
f'discriminator={self.discriminator!r} membership_state={self.membership_state!r}>'
)

View File

@ -30,12 +30,14 @@ __all__ = (
'Template', 'Template',
) )
class _FriendlyHttpAttributeErrorHelper: class _FriendlyHttpAttributeErrorHelper:
__slots__ = () __slots__ = ()
def __getattr__(self, attr): def __getattr__(self, attr):
raise AttributeError('PartialTemplateState does not support http methods.') raise AttributeError('PartialTemplateState does not support http methods.')
class _PartialTemplateState: class _PartialTemplateState:
def __init__(self, *, state): def __init__(self, *, state):
self.__state = state self.__state = state
@ -72,6 +74,7 @@ class _PartialTemplateState:
def __getattr__(self, attr): def __getattr__(self, attr):
raise AttributeError(f'PartialTemplateState does not support {attr!r}.') raise AttributeError(f'PartialTemplateState does not support {attr!r}.')
class Template: class Template:
"""Represents a Discord template. """Represents a Discord template.
@ -126,8 +129,10 @@ class Template:
self.source_guild = guild self.source_guild = guild
def __repr__(self): def __repr__(self):
return '<Template code={0.code!r} uses={0.uses} name={0.name!r}' \ return (
' creator={0.creator!r} source_guild={0.source_guild!r}>'.format(self) f'<Template code={self.code!r} uses={self.uses} name={self.name!r}'
f' creator={self.creator!r} source_guild={self.source_guild!r}>'
)
async def create_guild(self, name, region=None, icon=None): async def create_guild(self, name, region=None, icon=None):
"""|coro| """|coro|

View File

@ -36,6 +36,7 @@ __all__ = (
_BaseUser = discord.abc.User _BaseUser = discord.abc.User
class BaseUser(_BaseUser): class BaseUser(_BaseUser):
__slots__ = ('name', 'id', 'discriminator', 'avatar', 'bot', 'system', '_public_flags', '_state') __slots__ = ('name', 'id', 'discriminator', 'avatar', 'bot', 'system', '_public_flags', '_state')
@ -44,7 +45,7 @@ class BaseUser(_BaseUser):
self._update(data) self._update(data)
def __str__(self): def __str__(self):
return '{0.name}#{0.discriminator}'.format(self) return f'{self.name}#{self.discriminator}'
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, _BaseUser) and other.id == self.id return isinstance(other, _BaseUser) and other.id == self.id
@ -230,6 +231,7 @@ class BaseUser(_BaseUser):
return any(user.id == self.id for user in message.mentions) return any(user.id == self.id for user in message.mentions)
class ClientUser(BaseUser): class ClientUser(BaseUser):
"""Represents your Discord user. """Represents your Discord user.
@ -275,15 +277,17 @@ class ClientUser(BaseUser):
mfa_enabled: :class:`bool` mfa_enabled: :class:`bool`
Specifies if the user has MFA turned on and working. Specifies if the user has MFA turned on and working.
""" """
__slots__ = BaseUser.__slots__ + \
('locale', '_flags', 'verified', 'mfa_enabled', '__weakref__') __slots__ = BaseUser.__slots__ + ('locale', '_flags', 'verified', 'mfa_enabled', '__weakref__')
def __init__(self, *, state, data): def __init__(self, *, state, data):
super().__init__(state=state, data=data) super().__init__(state=state, data=data)
def __repr__(self): def __repr__(self):
return '<ClientUser id={0.id} name={0.name!r} discriminator={0.discriminator!r}' \ return (
' bot={0.bot} verified={0.verified} mfa_enabled={0.mfa_enabled}>'.format(self) f'<ClientUser id={self.id} name={self.name!r} discriminator={self.discriminator!r}'
f' bot={self.bot} verified={self.verified} mfa_enabled={self.mfa_enabled}>'
)
def _update(self, data): def _update(self, data):
super()._update(data) super()._update(data)
@ -293,7 +297,6 @@ class ClientUser(BaseUser):
self._flags = data.get('flags', 0) self._flags = data.get('flags', 0)
self.mfa_enabled = data.get('mfa_enabled', False) self.mfa_enabled = data.get('mfa_enabled', False)
async def edit(self, *, username=None, avatar=None): async def edit(self, *, username=None, avatar=None):
"""|coro| """|coro|
@ -330,6 +333,7 @@ class ClientUser(BaseUser):
data = await self._state.http.edit_profile(username=username, avatar=avatar) data = await self._state.http.edit_profile(username=username, avatar=avatar)
self._update(data) self._update(data)
class User(BaseUser, discord.abc.Messageable): class User(BaseUser, discord.abc.Messageable):
"""Represents a Discord user. """Represents a Discord user.
@ -370,7 +374,7 @@ class User(BaseUser, discord.abc.Messageable):
__slots__ = BaseUser.__slots__ + ('__weakref__',) __slots__ = BaseUser.__slots__ + ('__weakref__',)
def __repr__(self): def __repr__(self):
return '<User id={0.id} name={0.name!r} discriminator={0.discriminator!r} bot={0.bot}>'.format(self) return f'<User id={self.id} name={self.name!r} discriminator={self.discriminator!r} bot={self.bot}>'
async def _get_channel(self): async def _get_channel(self):
ch = await self.create_dm() ch = await self.create_dm()

View File

@ -904,7 +904,7 @@ class Webhook(Hashable):
if format not in ('png', 'jpg', 'jpeg'): if format not in ('png', 'jpg', 'jpeg'):
raise InvalidArgument("format must be one of 'png', 'jpg', or 'jpeg'.") raise InvalidArgument("format must be one of 'png', 'jpg', or 'jpeg'.")
url = '/avatars/{0.id}/{0.avatar}.{1}?size={2}'.format(self, format, size) url = f'/avatars/{self.id}/{self.avatar}.{format}?size={size}'
return Asset(self._state, url) return Asset(self._state, url)
def delete(self, *, reason=None): def delete(self, *, reason=None):

View File

@ -76,7 +76,7 @@ class WidgetChannel:
return self.name return self.name
def __repr__(self): def __repr__(self):
return '<WidgetChannel id={0.id} name={0.name!r} position={0.position!r}>'.format(self) return f'<WidgetChannel id={self.id} name={self.name!r} position={self.position!r}>'
@property @property
def mention(self): def mention(self):
@ -230,7 +230,7 @@ class Widget:
return self.id == other.id return self.id == other.id
def __repr__(self): def __repr__(self):
return '<Widget id={0.id} name={0.name!r} invite_url={0.invite_url!r}>'.format(self) return f'<Widget id={self.id} name={self.name!r} invite_url={self.invite_url!r}>'
@property @property
def created_at(self): def created_at(self):

View File

@ -128,7 +128,7 @@ bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"),
@bot.event @bot.event
async def on_ready(): async def on_ready():
print('Logged in as {0} ({0.id})'.format(bot.user)) print(f'Logged in as {bot.user} ({bot.user.id})')
print('------') print('------')
bot.add_cog(Music(bot)) bot.add_cog(Music(bot))

View File

@ -3,7 +3,7 @@ import discord
class MyClient(discord.Client): class MyClient(discord.Client):
async def on_ready(self): async def on_ready(self):
print('Connected!') print('Connected!')
print('Username: {0.name}\nID: {0.id}'.format(self.user)) print(f'Username: {self.user.name}\nID: {self.user.id}')
async def on_message(self, message): async def on_message(self, message):
if message.content.startswith('!deleteme'): if message.content.startswith('!deleteme'):
@ -14,8 +14,8 @@ class MyClient(discord.Client):
await message.channel.send('Goodbye in 3 seconds...', delete_after=3.0) await message.channel.send('Goodbye in 3 seconds...', delete_after=3.0)
async def on_message_delete(self, message): async def on_message_delete(self, message):
fmt = '{0.author} has deleted the message: {0.content}' msg = f'{message.author} has deleted the message: {message.content}'
await message.channel.send(fmt.format(message)) await message.channel.send(msg)
client = MyClient() client = MyClient()
client.run('token') client.run('token')

View File

@ -4,7 +4,7 @@ import asyncio
class MyClient(discord.Client): class MyClient(discord.Client):
async def on_ready(self): async def on_ready(self):
print('Connected!') print('Connected!')
print('Username: {0.name}\nID: {0.id}'.format(self.user)) print(f'Username: {self.user.name}\nID: {self.user.id}')
async def on_message(self, message): async def on_message(self, message):
if message.content.startswith('!editme'): if message.content.startswith('!editme'):
@ -13,8 +13,8 @@ class MyClient(discord.Client):
await msg.edit(content='40') await msg.edit(content='40')
async def on_message_edit(self, before, after): async def on_message_edit(self, before, after):
fmt = '**{0.author}** edited their message:\n{0.content} -> {1.content}' msg = f'**{before.author}** edited their message:\n{before.content} -> {after.content}'
await before.channel.send(fmt.format(before, after)) await before.channel.send(msg)
client = MyClient() client = MyClient()
client.run('token') client.run('token')