Aimarekin
Aimarekin

Reputation: 330

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?

Upvotes: 0

Views: 699

Answers (1)

Taku
Taku

Reputation: 33744

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

Upvotes: 1

Related Questions