我在Discord.py中使用内联转换器,它是贪婪的(这是文件,更多演示代码)
正如您从这些演示代码中看到的,它也可以接受int这样的类型,但是当我使用str时,它会引发此错误。
TypeError: Greedy[str] is invalid命令的代码(顺便说一句,它是Cog命令)
@commands.command()
async def temp(self, ctx:commands.Context, message:commands.Greedy[str]='None', user:discord.Member=None):
await ctx.send(f'{message = }, {user.mention}')现在它只是一个临时命令,但是Greedy[str]根本不起作用,但它适用于int,discord.Member类型的值
我也知道这一点(星参数法)
async def function_name(self, ctx, *, arg)我知道它也这样做,但只有当我希望它将所有其余的参数值都作为字符串传递给一个变量时,它才能工作,但我不希望这样,我想在中间传递参数值,如下所示
temp temporary text @user因为我想在以后对许多命令实现这个方法。
有办法让它发挥作用吗?
我在Replit中使用Pythonv3.8.12和Discord.py v1.7.3
编辑:我目前正在使用此代码作为替代方案。
@commands.command()
async def temp(self, ctx: commands.Context, *arg):
user_id = re.findall(r'(?<=<@)[!&]?(\d{17,22})(?=> *$)', arg[-1])
if len(user_id ) == 0:
raise TypeError('Command "temp" missing 1 required positional argument: "user"')
user = ctx.guild.get_member(int(user_id[0]))
message = ' '.join(arg[:-1])
await ctx.send(f'{message = }, {user.mention}')如果你找到了一种使用贪婪转换器的方法,那会很有帮助的
发布于 2022-01-07 11:59:44
这是解决这个问题的方法之一,但我想还有另外一种方法来解决这个问题。
@commands.command()
async def temp(self,ctx:commands.Context,*message):
message=list(message)
user=message[len(message)-1]
message.pop()
await ctx.send(message)
await ctx.send(user)以这种方式获取用户的值之后,您可以检查它是否是成员,然后可以执行进一步的代码。
https://stackoverflow.com/questions/70618400
复制相似问题