python - 如何让机器人告诉用户它的错误?不和谐.py
问题描述
我一个人做一个随机数命令,例如:
我:{commandprefix}random
机器人:请输入两个参数,一个数字和一个更大的数字。
我:{commandprefix}random 1
机器人:请输入两个参数,一个数字和一个更大的数字。
我:{commandprefix}随机 0.5 1.3
机器人:0.8
我:{commandprefix}随机 1 10
机器人:3
等等
就像我的例子一样,如果有任何错误(比如字符不是数字,只有 1 个参数等),机器人会自动告诉我们有错误。
这是我的代码:)
@client.command()
async def random(ctx, arg1, arg2):
import random
try:
import random
arg = random.randrange(float(arg1), float(arg2))
else:
embed = discord.Embed(title = "Random Number",
description = f"Here is your random number : {arg}",
color = discord.Color.red())
now = datetime.now()
current_time = now.strftime("%H:%M")
embed.set_footer(text=f"Requested by {ctx.author} at {current_time}")
await ctx.send(embed=embed)
它只能发送不是小数的随机数并且还不能检测到错误:(
解决方案
我想我明白你在做什么!从我的角度来看,如果缺少参数,您似乎正试图让机器人停止!这是我想出的:
@client.command()
async def random(ctx, arg1, arg2):
import random
if(arg1 and args2):
try:
import random
arg = random.randrange(float(arg1), float(arg2))
else:
embed = discord.Embed(title = "Random Number",
description = f"Here is your random number : {arg}",
color = discord.Color.red())
now = datetime.now()
current_time = now.strftime("%H:%M")
embed.set_footer(text=f"Requested by {ctx.author} at {current_time}")
await ctx.send(embed=embed)
else:
ctx.send("You are missing some arguments! The command is random <Number 1> <Number 2")
这没有组织和精确,但这应该表明某种想法!希望这可以帮助!
推荐阅读
- unit-testing - UserManager 不在单元测试中创建用户
- java - xxx 上的 @OneToOne 或 @ManyToOne 引用了一个未知实体:java.util.Set
- ruby-on-rails - 如何测试使用 Rails 5 创建的经过身份验证的 API?
- scala - 使用 es-hadoop 连接器和基于 aws 角色的签名将 Spark 数据帧写入 Elastic Search
- tensorflow - ValueError:检查输入时出错:预期 lstm_1_input 有 3 个维度,但得到了形状为 (6782、36) 的数组
- javascript - IntersectionObserver:滚动回顶部时触发事件
- jquery - 向 jQuery 中的 datepicker 小部件注入新方法
- javascript - 试图将 JSON.stringify 转换为 PHP 数组
- r - r中的Groupby时间戳数据
- selenium - PC被锁定时填写代理表格