【问题标题】:How to get ID of a mentioned user (Discord.py)如何获取提及用户的 ID (Discord.py)
【发布时间】:2019-04-01 05:44:12
【问题描述】:
@bot.command()
async def id(ctx, a:str): #a = @user

如何获取命令中提到的用户ID,并将其输出为:

await ctx.send(id)

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    使用converter 获取User 对象:

    @bot.command(name="id")
    async def id_(ctx, user: discord.User):
        await ctx.send(user.id)
    

    或者获取作者的id

    @bot.command(name="id")
    async def id_(ctx):
        await ctx.send(ctx.author.id)
    

    【讨论】:

      【解决方案2】:

      刚刚意识到,当您@someone 并将其存储到变量“a”中时,它包含 '' 形式的用户 ID。所以稍微清理一下就可以得到用户 ID

      代码如下:

      @bot.command()
      async def id(ctx, a:str):
          a = a.replace("<","")
          a = a.replace(">","")
          a = a.replace("@","")
          await ctx.send(a)
      

      由于我的命令由“rev id @someone”组成,@someone 作为字符串“”而不是“@someone”存储在“a”中。

      【讨论】:

        猜你喜欢
        • 2021-09-12
        • 2020-09-07
        • 1970-01-01
        • 2016-12-20
        • 2019-04-02
        • 2017-10-13
        • 2023-04-04
        • 2021-05-07
        • 1970-01-01
        相关资源
        最近更新 更多