如何将值合并到 discord.py 中的一条消息中
How do I merge values into one message in discord.py
我希望我的机器人能够在同一行中说出消息、提及用户,然后回复一条随机消息,而不是分三行。
这是我以前的方法:
if msg.startswith('respond')
await message.channel.send('alright then,')
await message.channel.send(message.author.mention)
await message.channel.send(random.choice(responses))
然而,这使得它们都出现在同一行中,但我不知道如何将它们合并为一条单行。这是我多次失败的尝试之一:
if msg.startswith('respond'):
await message.channel.send ('alright then, <message.author.mention> <random.choice(responses)>')
(请不要取笑我的业余编码技能lmao)
假设您使用的是 python3+,您可以使用 f-strings
if msg.startswith('respond'):
await message.channel.send(f'alright then, {message.author.mention} {random.choice(responses)}')
还有其他替代方法,例如格式字符串
if msg.startswith('respond'):
await message.channel.send('alright then, {} {}'.format(message.author.mention, random.choice(responses))
连接方法如
if msg.startswith('respond'):
await message.channel.send('alright then, ' + message.author.mention + ' ' + random.choice(responses))
f-string
Inzer Lee 的回答是正确的,但在 Python 3.8+ 中,PEP 文档建议使用 f-string 方法连接字符串:
if msg.startswith("respond"):
await message.channel.send(f"alright then, {message.author.mention} {random.choice(responses)}")
PEP-8
所以,如果你想遵守 PEP-8 缩进标准,这段代码超过 72 个字符,你应该这样做:
await message.channel.send("alright then,"
f"{message.author.mention} {random.choice(responses)}"
)
我希望我的机器人能够在同一行中说出消息、提及用户,然后回复一条随机消息,而不是分三行。
这是我以前的方法:
if msg.startswith('respond')
await message.channel.send('alright then,')
await message.channel.send(message.author.mention)
await message.channel.send(random.choice(responses))
然而,这使得它们都出现在同一行中,但我不知道如何将它们合并为一条单行。这是我多次失败的尝试之一:
if msg.startswith('respond'):
await message.channel.send ('alright then, <message.author.mention> <random.choice(responses)>')
(请不要取笑我的业余编码技能lmao)
假设您使用的是 python3+,您可以使用 f-strings
if msg.startswith('respond'):
await message.channel.send(f'alright then, {message.author.mention} {random.choice(responses)}')
还有其他替代方法,例如格式字符串
if msg.startswith('respond'):
await message.channel.send('alright then, {} {}'.format(message.author.mention, random.choice(responses))
连接方法如
if msg.startswith('respond'):
await message.channel.send('alright then, ' + message.author.mention + ' ' + random.choice(responses))
f-string
Inzer Lee 的回答是正确的,但在 Python 3.8+ 中,PEP 文档建议使用 f-string 方法连接字符串:if msg.startswith("respond"):
await message.channel.send(f"alright then, {message.author.mention} {random.choice(responses)}")
PEP-8
所以,如果你想遵守 PEP-8 缩进标准,这段代码超过 72 个字符,你应该这样做:await message.channel.send("alright then,"
f"{message.author.mention} {random.choice(responses)}"
)