diff options
| author | Rapptz <[email protected]> | 2021-04-04 04:40:19 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2021-04-04 07:03:53 -0400 |
| commit | 9d39b135f4f84239787b0901d06a4f370a82d4bb (patch) | |
| tree | 8826845cfd47eafa5c9d2ef1fcbedd36382714f4 /examples | |
| parent | Bump minimum Python version to 3.8 (diff) | |
| download | discord.py-9d39b135f4f84239787b0901d06a4f370a82d4bb.tar.xz discord.py-9d39b135f4f84239787b0901d06a4f370a82d4bb.zip | |
Modernize code to use f-strings
This also removes the encoding on the top, since Python 3 does it by
default. It also changes some methods to use `yield from`.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/basic_bot.py | 2 | ||||
| -rw-r--r-- | examples/basic_voice.py | 14 | ||||
| -rw-r--r-- | examples/converters.py | 10 | ||||
| -rw-r--r-- | examples/guessing_game.py | 4 | ||||
| -rw-r--r-- | examples/new_member.py | 2 |
5 files changed, 16 insertions, 16 deletions
diff --git a/examples/basic_bot.py b/examples/basic_bot.py index 409d4c4a..1d8928bf 100644 --- a/examples/basic_bot.py +++ b/examples/basic_bot.py @@ -61,7 +61,7 @@ async def cool(ctx): In reality this just checks if a subcommand is being invoked. """ if ctx.invoked_subcommand is None: - await ctx.send('No, {0.subcommand_passed} is not cool'.format(ctx)) + await ctx.send(f'No, {ctx.subcommand_passed} is not cool') @cool.command(name='bot') async def _bot(ctx): diff --git a/examples/basic_voice.py b/examples/basic_voice.py index 42cea2c8..45387911 100644 --- a/examples/basic_voice.py +++ b/examples/basic_voice.py @@ -70,9 +70,9 @@ class Music(commands.Cog): """Plays a file from the local filesystem""" source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(query)) - ctx.voice_client.play(source, after=lambda e: print('Player error: %s' % e) if e else None) + ctx.voice_client.play(source, after=lambda e: print(f'Player error: {e}') if e else None) - await ctx.send('Now playing: {}'.format(query)) + await ctx.send(f'Now playing: {query}') @commands.command() async def yt(self, ctx, *, url): @@ -80,9 +80,9 @@ class Music(commands.Cog): async with ctx.typing(): player = await YTDLSource.from_url(url, loop=self.bot.loop) - ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None) + ctx.voice_client.play(player, after=lambda e: print(f'Player error: {e}') if e else None) - await ctx.send('Now playing: {}'.format(player.title)) + await ctx.send(f'Now playing: {player.title}') @commands.command() async def stream(self, ctx, *, url): @@ -90,9 +90,9 @@ class Music(commands.Cog): async with ctx.typing(): player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True) - ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None) + ctx.voice_client.play(player, after=lambda e: print(f'Player error: {e}') if e else None) - await ctx.send('Now playing: {}'.format(player.title)) + await ctx.send(f'Now playing: {player.title}') @commands.command() async def volume(self, ctx, volume: int): @@ -102,7 +102,7 @@ class Music(commands.Cog): return await ctx.send("Not connected to a voice channel.") ctx.voice_client.source.volume = volume / 100 - await ctx.send("Changed volume to {}%".format(volume)) + await ctx.send(f"Changed volume to {volume}%") @commands.command() async def stop(self, ctx): diff --git a/examples/converters.py b/examples/converters.py index 74b9c3c4..41aba498 100644 --- a/examples/converters.py +++ b/examples/converters.py @@ -29,7 +29,7 @@ async def userinfo(ctx: commands.Context, user: discord.User): user_id = user.id username = user.name avatar = user.avatar_url - await ctx.send('User found: {} -- {}\n{}'.format(user_id, username, avatar)) + await ctx.send(f'User found: {user_id} -- {username}\n{avatar}') @userinfo.error async def userinfo_error(ctx: commands.Context, error: commands.CommandError): @@ -70,7 +70,7 @@ class ChannelOrMemberConverter(commands.Converter): # If the value could not be converted we can raise an error # so our error handlers can deal with it in one place. # The error has to be CommandError derived, so BadArgument works fine here. - raise commands.BadArgument('No Member or TextChannel could be converted from "{}"'.format(argument)) + raise commands.BadArgument(f'No Member or TextChannel could be converted from "{argument}"') @@ -81,7 +81,7 @@ async def notify(ctx: commands.Context, target: ChannelOrMemberConverter): # the `argument` parameter of the `ChannelOrMemberConverter.convert` method and # the conversion will go through the process defined there. - await target.send('Hello, {}!'.format(target.name)) + await target.send(f'Hello, {target.name}!') @bot.command() async def ignore(ctx: commands.Context, target: typing.Union[discord.Member, discord.TextChannel]): @@ -95,9 +95,9 @@ async def ignore(ctx: commands.Context, target: typing.Union[discord.Member, dis # To check the resulting type, `isinstance` is used if isinstance(target, discord.Member): - await ctx.send('Member found: {}, adding them to the ignore list.'.format(target.mention)) + await ctx.send(f'Member found: {target.mention}, adding them to the ignore list.') elif isinstance(target, discord.TextChannel): # this could be an `else` but for completeness' sake. - await ctx.send('Channel found: {}, adding it to the ignore list.'.format(target.mention)) + await ctx.send(f'Channel found: {target.mention}, adding it to the ignore list.') # Built-in type converters. @bot.command() diff --git a/examples/guessing_game.py b/examples/guessing_game.py index a8f09c63..7620d7af 100644 --- a/examples/guessing_game.py +++ b/examples/guessing_game.py @@ -25,12 +25,12 @@ class MyClient(discord.Client): try: guess = await self.wait_for('message', check=is_correct, timeout=5.0) except asyncio.TimeoutError: - return await message.channel.send('Sorry, you took too long it was {}.'.format(answer)) + return await message.channel.send(f'Sorry, you took too long it was {answer}.') if int(guess.content) == answer: await message.channel.send('You are right!') else: - await message.channel.send('Oops. It is actually {}.'.format(answer)) + await message.channel.send(f'Oops. It is actually {answer}.') client = MyClient() client.run('token') diff --git a/examples/new_member.py b/examples/new_member.py index aa67c46e..6a9a530f 100644 --- a/examples/new_member.py +++ b/examples/new_member.py @@ -12,7 +12,7 @@ class MyClient(discord.Client): async def on_member_join(self, member): guild = member.guild if guild.system_channel is not None: - to_send = 'Welcome {0.mention} to {1.name}!'.format(member, guild) + to_send = f'Welcome {member.mention} to {guild.name}!' await guild.system_channel.send(to_send) |