【发布时间】: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
@bot.command()
async def id(ctx, a:str): #a = @user
如何获取命令中提到的用户ID,并将其输出为:
await ctx.send(id)
【问题讨论】:
标签: python-3.x discord.py
使用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)
【讨论】:
刚刚意识到,当您@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”中。
【讨论】: