简体   繁体   English

如何将字符串解析为不一致的数据类型?

[英]How to parse a string into discord data types?

I am running into some issues because I would like to convert a value into a discord.Member . 我遇到了一些问题,因为我想将值转换为discord.Member

The problem is I know I can do: 问题是我知道我可以做到:

@commands.command
async def example(self, ctx, arg: discord.Member):

To cast arg to be a Member , but how would I do this directly from a string? 要将arg转换为Member ,但是我将如何直接从字符串执行此操作?

value = other_value: discord.Member

This returns a syntax error, how would I get to do this correctly? 这会返回语法错误,我将如何正确执行此操作?

The syntax is called function annotation . 该语法称为函数注释 Force casting the type of the parameter is not a feature of Python, instead, this is defined by discord.py. 强制转换参数的类型不是Python的功能,而是由discord.py定义。 The arguments were converted prior to calling the function. 在调用函数之前先对参数进行了转换。

You can't use local-variable annotation to achieve the same thing (actually you might be able to...but it'll be extremely tedious), but you can achieve a similar effect by manually calling the convert method of discord.py's Converter : 不能使用局部变量批注来实现相同的目的(实际上,您可以...但是这将非常繁琐),但是可以通过手动调用discord.py的convert方法来实现类似的效果。 Converter

value = commands.MemberConverter().convert(ctx, other_value)

A list of available converters: 可用转换器的列表:

https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#ext-commands-api-converters https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#ext-commands-api-converters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM