如何让机器人告诉用户它的错误? discord.py
How do I make the bot tell the user that its error? discord.py
我做一个随机数命令,例子:
我:{commandprefix}随机
Bot : 请输入两个参数,一个数字和一个更大的数字。
我:{commandprefix}随机 1
Bot : 请输入两个参数,一个数字和一个更大的数字。
我:{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")
这不是有条理和精确的,但这应该表明某种想法!希望这对您有所帮助!
这里有一个错误处理的例子,根据需要修改
@random.error
async def random_error(error, ctx):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Wrong arguments")
伙计们,我已经找到答案了!你不用再回答了。
@client.command()
async def random(ctx, arg1=None, arg2=None):
import random
if str(type(arg1)) and str(type(arg2)) == "<class 'NoneType'>":
await ctx.send("You are missing some arguments! The command is random <Number 1> <Number 2>")
else:
try:
import random
arg = random.randrange(float(arg1), float(arg2))
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)
except ValueError:
await ctx.send("Please type a valid number")
我做一个随机数命令,例子:
我:{commandprefix}随机
Bot : 请输入两个参数,一个数字和一个更大的数字。
我:{commandprefix}随机 1
Bot : 请输入两个参数,一个数字和一个更大的数字。
我:{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")
这不是有条理和精确的,但这应该表明某种想法!希望这对您有所帮助!
这里有一个错误处理的例子,根据需要修改
@random.error
async def random_error(error, ctx):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Wrong arguments")
伙计们,我已经找到答案了!你不用再回答了。
@client.command()
async def random(ctx, arg1=None, arg2=None):
import random
if str(type(arg1)) and str(type(arg2)) == "<class 'NoneType'>":
await ctx.send("You are missing some arguments! The command is random <Number 1> <Number 2>")
else:
try:
import random
arg = random.randrange(float(arg1), float(arg2))
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)
except ValueError:
await ctx.send("Please type a valid number")