如何让不和谐机器人输出用户输入的所有内容,而不仅仅是第一个输入?

2022-01-15 00:00:00 python python-3.7 discord discord.py repeat

问题描述

我正在尝试让机器人重复用户输入的内容,重复用户指定的次数.
我遇到的问题是,如果用户输入:!repeat 5 xy,机器人只会重复 x 5 次,而不是 xy 5 次.

I'm trying to get a bot that will repeat what a user inputs, as many times as the user specifies.
The issue I'm running into is that if the user types: !repeat 5 x y, the bot will only repeat x 5 times, and not x y 5 times.

这是我要运行的代码:

@bot.command()
async def repeat(times: int, content="Repeating..."):
    for i in range(times):
        if times > 10:
            await bot.say("Cannot spam more than 10 messages at a time.")
            return
        else:
            await bot.say(content)


解决方案

您可以使用 仅关键字参数语法 并执行类似

You can use the keyword-only argument syntax and do something like

@bot.command()
async def repeat(times: int, *,content="Repeating..."):
  for i in range(times):
    if times > 10:
      await bot.say("Cannot spam more than 10 messages at a time.")
      return
    else:
      await bot.say(content)

相关文章