问题
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 Member
s 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