mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-09-05 09:26:10 +00:00
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`.
This commit is contained in:
@ -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):
|
||||
|
@ -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):
|
||||
|
@ -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()
|
||||
|
@ -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')
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user