简体   繁体   中英

How to convert a string to a user discord.py

I'm trying to convert a string to a user so I can dm them. Here's my current code:

    @bot.command(pass_context=True)
async def partnerwarn(ctx):
file_names = glob.glob("p*")
for file in file_names:
    f = open(file, 'r')
    content = f.read()
    f.close()
    member = file[1:]      
    await bot.send_message(member : discord.User, "You've had under 7 partners! This is a warning, please make sure you actively partner!")
    print("Warned!")
    await bot.reply("**" + file[1:] + " was warned!**")

It doesn't work because member : discord.User is invalid syntax where it currently is. How should I fix this?

When the command decorator sees an annotation on one of the arguments to the decorated coroutine, it knows to either use the correct converter or directly apply the annotation as a callable before the argument is passed to the underlying coroutine.

You can create your own MemberConverter objects and use them to convert strings to Member s by using their convert coroutines:

from discord.ext.commands import MemberConverter

...

converter = MemberConverter()
member = await converter.convert(ctx, file[1:])

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