How to convert a string to a user discord.py

可紊 提交于 2021-02-11 13:23:33

问题


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?


回答1:


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 Members by using their convert coroutines:

from discord.ext.commands import MemberConverter

...

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


来源:https://stackoverflow.com/questions/55270496/how-to-convert-a-string-to-a-user-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!