【问题标题】:How to include a user's name in a random message [Python]如何在随机消息中包含用户名 [Python]
【发布时间】:2021-05-08 04:07:05
【问题描述】:

我是编码新手。我希望我的机器人使用随机消息响应用户命令,其中他们的姓名在消息中的位置会有所不同

@bot.command()
async def hello(ctx):
    responses = ["hi, {ctx.author.name}!",
                 "{ctx.author.name}, how are you?",
                 "Good day, {ctx.author.name}, how's the weather there?"]
    await ctx.send(f'{random.choice(responses)}')

这是我部署机器人时发生的情况:

/hello
hi, {ctx.author.name}!

/hello
{ctx.author.name}, how are you?

但是,例如,我希望机器人以用户名进行响应

/hello
hi, Bob!

/hello
Bob, how are you?

我将非常感谢任何帮助!

【问题讨论】:

    标签: python discord bots discord.py


    【解决方案1】:

    你必须使用 f-strings

    responses = [
        f"hi, {ctx.author.name}!",
        f"{ctx.author.name}, how are you?",
        f"Good day, {ctx.author.name}, how's the weather there?"
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-31
      • 2019-01-07
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多