我想限制可以作为
spam
命令参数输入的数量。我怎样才能做到这一点?这是我正在使用的代码:
@client.command()
async def spam(ctx, message=None,* ,amount : int):
for _ in range(amount):
await ctx.send(message)
一个简单的 if 语句就可以做到。此外,您的命令参数排序不正确。下面的代码是比较理想的。尝试一下并相应地修改垃圾邮件限制数量:
@client.command()
async def spam(ctx, amount : int, *, message=None):
limit = 5
if amount > limit:
await ctx.send("exceeds spam limit")
return
else:
for _ in range(amount):
await ctx.send(message)
要运行此程序,您必须执行
spam <amount> <message>
。例如,!spam 4 hello this is a spam
。
从discord.ext导入命令
def is_valid_spam_amount(金额): 返回 1 <= amount <= 10 # Set the desired range for the amount (1 to 10 in this example)
@client.command()
async def spam(ctx, message=None, *, amount: int):
if is_valid_spam_amount(amount):
for _ in range(amount):
await ctx.send(message)
else:
await ctx.send("Invalid amount. Please provide a value between 1 and 10.")