discord.py 从名称中获取成员实例#discriminator [英] discord.py get member instance from name#discriminator

查看:83
本文介绍了discord.py 从名称中获取成员实例#discriminator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户名的字符串 user = 'somename#1111' 我怎么能提到他?(我从谷歌表格中得到名字,这就是为什么它是一个字符串)

I have a string with the name of an user user = 'somename#1111' how can I mention him? (I get the name from google sheets that's why it's a string)

我尝试过:

@client.command()
async def mention(ctx):
    user : discord.Member = 'somename#1111'
    await ctx.send(f"{user.mention} hello!")

但它不起作用

推荐答案

虽然这个方法是有效的,我还是建议改用保存用户 ID 而不是用户名 + 标识符,因为用户 ID 是不变的,但用户可以改变他们的名字和鉴别器.

Although this method is valid, I'd recommend switching to saving user IDs instead of usernames + discriminators, as user IDs are constant, but users are able to change their names and discriminators.

@client.command()
async def mention(ctx):
    user = discord.utils.get(ctx.guild.members, name="somename", discriminator="1111")
    await ctx.send(f"{user.mention} hello!")

还有身份证:

@client.command()
async def mention(ctx):
    user = ctx.guild.get_member(112233445566778899) # replace user ID there
    try:
        await ctx.send(f"{user.mention} hello!")
    except:
        await ctx.send("Sorry, that user has left the server!")

<小时>

参考:

这篇关于discord.py 从名称中获取成员实例#discriminator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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