如何将字符串转换为用户discord.py [英] How to convert a string to a user discord.py

查看:21
本文介绍了如何将字符串转换为用户discord.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串转换为用户,以便我可以对它们进行dm处理.这是我当前的代码:

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!**")

它不起作用,因为 member:discord.User 当前所在的语法无效.我该如何解决?

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

推荐答案

command 装饰器看到装饰的协程的一个参数的注释时,它知道使用正确的转换器,或直接在参数前将注释作为可调用对象应用被传递给底层协程.

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.

您可以创建自己的 MemberConverter 对象,并使用它们的 convert 协程将它们转换为 Member 的字符串:

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:])

这篇关于如何将字符串转换为用户discord.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆