简体   繁体   English

如何将字符串转换为用户discord.py

[英]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. 我正在尝试将字符串转换为用户,因此我可以将它们转换为dm。 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. 它不起作用,因为member : discord.User当前是无效的语法。 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. command装饰器在装饰协程的其中一个参数上看到注释时,它知道要么使用正确的转换器 ,要么在将参数传递给基础协程之前直接将注释应用为可调用。

You can create your own MemberConverter objects and use them to convert strings to Member s by using their convert coroutines: 您可以创建自己的MemberConverter对象,并使用它们通过使用convert协程将字符串转换为Member

from discord.ext.commands import MemberConverter

...

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

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

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