首页 > 解决方案 > 从提到的用户那里获取成员对象

问题描述

我有一个用 python 编写的不和谐机器人,其中一个命令会为您 ping 的用户提取统计信息>stats @user。我希望机器人重复提到的用户名而不 ping 他们,但不知道如何。

@client.command()
async def stats(ctx,a):
  b = str(a)
  b = b.replace("<","")
  b = b.replace(">","")
  b = b.replace("!","")
  user = b.replace("@","")
  path = 'users/'+user+'.txt'
  file = open(path,'r')
  lines = file.read()
  lines2 = lines.splitlines()
  file.close()
  await ctx.channel.send('Stats for '+a+'\nExp: '+lines2[0]+'\nMessages Sent: '+lines2[1])

目前,机器人确实会说出用户的姓名,但会 ping 他们,这会导致用户被 ping 两次。一次在命令启动期间和一次执行。我查看了其他问题,但找不到适合我的答案。

标签: pythondiscorddiscord.py

解决方案


我忽略了您从文件中读取的代码,只关注您需要更改的部分:

@client.command()
async def stats(ctx, a : discord.Member): #here you identify "a" as discord.Member object
  await ctx.channel.send(f'Stats for {a.name}') #then you can get name

查看文档中的 discord.Member以了解您可以使用它做什么。


推荐阅读