mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-18 11:45:37 +00:00
Use f-strings in more places that were missed
This commit is contained in:
parent
99fc950510
commit
05c123f3ab
@ -338,7 +338,7 @@ Converters
|
|||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def test(ctx, numbers: Greedy[int], reason: str):
|
async def test(ctx, numbers: Greedy[int], reason: str):
|
||||||
await ctx.send("numbers: {}, reason: {}".format(numbers, reason))
|
await ctx.send(f"numbers: {numbers}, reason: {reason}")
|
||||||
|
|
||||||
An invocation of ``[p]test 1 2 3 4 5 6 hello`` would pass ``numbers`` with
|
An invocation of ``[p]test 1 2 3 4 5 6 hello`` would pass ``numbers`` with
|
||||||
``[1, 2, 3, 4, 5, 6]`` and ``reason`` with ``hello``\.
|
``[1, 2, 3, 4, 5, 6]`` and ``reason`` with ``hello``\.
|
||||||
|
@ -33,16 +33,16 @@ This example cog defines a ``Greetings`` category for your commands, with a sing
|
|||||||
async def on_member_join(self, member):
|
async def on_member_join(self, member):
|
||||||
channel = member.guild.system_channel
|
channel = member.guild.system_channel
|
||||||
if channel is not None:
|
if channel is not None:
|
||||||
await channel.send('Welcome {0.mention}.'.format(member))
|
await channel.send(f'Welcome {member.mention}.')
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def hello(self, ctx, *, member: discord.Member = None):
|
async def hello(self, ctx, *, member: discord.Member = None):
|
||||||
"""Says hello"""
|
"""Says hello"""
|
||||||
member = member or ctx.author
|
member = member or ctx.author
|
||||||
if self._last_member is None or self._last_member.id != member.id:
|
if self._last_member is None or self._last_member.id != member.id:
|
||||||
await ctx.send('Hello {0.name}~'.format(member))
|
await ctx.send(f'Hello {member.name}~')
|
||||||
else:
|
else:
|
||||||
await ctx.send('Hello {0.name}... This feels familiar.'.format(member))
|
await ctx.send(f'Hello {member.name}... This feels familiar.')
|
||||||
self._last_member = member
|
self._last_member = member
|
||||||
|
|
||||||
A couple of technical notes to take into consideration:
|
A couple of technical notes to take into consideration:
|
||||||
|
@ -99,7 +99,7 @@ Since positional arguments are just regular Python arguments, you can have as ma
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def test(ctx, arg1, arg2):
|
async def test(ctx, arg1, arg2):
|
||||||
await ctx.send('You passed {} and {}'.format(arg1, arg2))
|
await ctx.send(f'You passed {arg1} and {arg2}')
|
||||||
|
|
||||||
Variable
|
Variable
|
||||||
++++++++++
|
++++++++++
|
||||||
@ -111,7 +111,8 @@ similar to how variable list parameters are done in Python:
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def test(ctx, *args):
|
async def test(ctx, *args):
|
||||||
await ctx.send('{} arguments: {}'.format(len(args), ', '.join(args)))
|
arguments = ', '.join(args)
|
||||||
|
await ctx.send(f'{len(args)} arguments: {arguments}')
|
||||||
|
|
||||||
This allows our user to accept either one or many arguments as they please. This works similar to positional arguments,
|
This allows our user to accept either one or many arguments as they please. This works similar to positional arguments,
|
||||||
so multi-word parameters should be quoted.
|
so multi-word parameters should be quoted.
|
||||||
@ -256,7 +257,7 @@ An example converter:
|
|||||||
class Slapper(commands.Converter):
|
class Slapper(commands.Converter):
|
||||||
async def convert(self, ctx, argument):
|
async def convert(self, ctx, argument):
|
||||||
to_slap = random.choice(ctx.guild.members)
|
to_slap = random.choice(ctx.guild.members)
|
||||||
return '{0.author} slapped {1} because *{2}*'.format(ctx, to_slap, argument)
|
return f'{ctx.author} slapped {to_slap} because *{argument}*'
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def slap(ctx, *, reason: Slapper):
|
async def slap(ctx, *, reason: Slapper):
|
||||||
@ -365,7 +366,7 @@ For example, to receive a :class:`Member` you can just pass it as a converter:
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def joined(ctx, *, member: discord.Member):
|
async def joined(ctx, *, member: discord.Member):
|
||||||
await ctx.send('{0} joined on {0.joined_at}'.format(member))
|
await ctx.send(f'{member} joined on {member.joined_at}')
|
||||||
|
|
||||||
When this command is executed, it attempts to convert the string given into a :class:`Member` and then passes it as a
|
When this command is executed, it attempts to convert the string given into a :class:`Member` and then passes it as a
|
||||||
parameter for the function. This works by checking if the string is a mention, an ID, a nickname, a username + discriminator,
|
parameter for the function. This works by checking if the string is a mention, an ID, a nickname, a username + discriminator,
|
||||||
@ -489,7 +490,7 @@ Consider the following example:
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def bottles(ctx, amount: typing.Optional[int] = 99, *, liquid="beer"):
|
async def bottles(ctx, amount: typing.Optional[int] = 99, *, liquid="beer"):
|
||||||
await ctx.send('{} bottles of {} on the wall!'.format(amount, liquid))
|
await ctx.send(f'{amount} bottles of {liquid} on the wall!')
|
||||||
|
|
||||||
|
|
||||||
.. image:: /images/commands/optional1.png
|
.. image:: /images/commands/optional1.png
|
||||||
@ -515,7 +516,7 @@ Consider the following example:
|
|||||||
@bot.command()
|
@bot.command()
|
||||||
async def slap(ctx, members: commands.Greedy[discord.Member], *, reason='no reason'):
|
async def slap(ctx, members: commands.Greedy[discord.Member], *, reason='no reason'):
|
||||||
slapped = ", ".join(x.name for x in members)
|
slapped = ", ".join(x.name for x in members)
|
||||||
await ctx.send('{} just got slapped for {}'.format(slapped, reason))
|
await ctx.send(f'{slapped} just got slapped for {reason}')
|
||||||
|
|
||||||
When invoked, it allows for any number of members to be passed in:
|
When invoked, it allows for any number of members to be passed in:
|
||||||
|
|
||||||
@ -586,8 +587,8 @@ handlers that allow us to do just that. First we decorate an error handler funct
|
|||||||
@bot.command()
|
@bot.command()
|
||||||
async def info(ctx, *, member: discord.Member):
|
async def info(ctx, *, member: discord.Member):
|
||||||
"""Tells you some info about the member."""
|
"""Tells you some info about the member."""
|
||||||
fmt = '{0} joined on {0.joined_at} and has {1} roles.'
|
msg = f'{member} joined on {member.joined_at} and has {len(member.roles)} roles.'
|
||||||
await ctx.send(fmt.format(member, len(member.roles)))
|
await ctx.send(msg)
|
||||||
|
|
||||||
@info.error
|
@info.error
|
||||||
async def info_error(ctx, error):
|
async def info_error(ctx, error):
|
||||||
|
@ -22,7 +22,7 @@ An example extension looks like this:
|
|||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def hello(ctx):
|
async def hello(ctx):
|
||||||
await ctx.send('Hello {0.display_name}.'.format(ctx.author))
|
await ctx.send(f'Hello {ctx.author.display_name}.')
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
bot.add_command(hello)
|
bot.add_command(hello)
|
||||||
|
@ -388,7 +388,7 @@ Example: ::
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def length(ctx):
|
async def length(ctx):
|
||||||
await ctx.send('Your message is {} characters long.'.format(len(ctx.message.content)))
|
await ctx.send(f'Your message is {len(ctx.message.content)} characters long.')
|
||||||
|
|
||||||
How do I make a subcommand?
|
How do I make a subcommand?
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -405,6 +405,6 @@ Example: ::
|
|||||||
|
|
||||||
@git.command()
|
@git.command()
|
||||||
async def push(ctx, remote: str, branch: str):
|
async def push(ctx, remote: str, branch: str):
|
||||||
await ctx.send('Pushing to {} {}'.format(remote, branch))
|
await ctx.send(f'Pushing to {remote} {branch}')
|
||||||
|
|
||||||
This could then be used as ``?git push origin master``.
|
This could then be used as ``?git push origin master``.
|
||||||
|
@ -102,10 +102,10 @@ A quick example to showcase how events work:
|
|||||||
|
|
||||||
class MyClient(discord.Client):
|
class MyClient(discord.Client):
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
print('Logged on as {0}!'.format(self.user))
|
print(f'Logged on as {self.user}!')
|
||||||
|
|
||||||
async def on_message(self, message):
|
async def on_message(self, message):
|
||||||
print('Message from {0.author}: {0.content}'.format(message))
|
print(f'Message from {messsage.author}: {message.content}')
|
||||||
|
|
||||||
client = MyClient()
|
client = MyClient()
|
||||||
client.run('my token goes here')
|
client.run('my token goes here')
|
||||||
|
@ -23,7 +23,7 @@ It looks something like this:
|
|||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print('We have logged in as {0.user}'.format(client))
|
print(f'We have logged in as {client.user}')
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_message(message):
|
async def on_message(message):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user