简体   繁体   中英

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 .

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?

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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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