如何将值合并到 discord.py 中的一条消息中

我希望我的机器人能够说出一条消息,提及一个用户,然后用一条随机消息响应,所有这些都在同一行中,而不是在 3 个单独的行中。

这是我以前的方法:

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))

以上是如何将值合并到 discord.py 中的一条消息中的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>