python - 从提到的用户那里获取成员对象
问题描述
我有一个用 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 两次。一次在命令启动期间和一次执行。我查看了其他问题,但找不到适合我的答案。
解决方案
我忽略了您从文件中读取的代码,只关注您需要更改的部分:
@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以了解您可以使用它做什么。
推荐阅读
- android - 如何在移动设备上重新训练 TensorFlow lite 模型以进行 MLKit 对象检测?我选择重新训练什么 Tensorflow Hub 模型?
- python - 尝试制作石头剪刀布游戏我如何获得用户输入?
- jdbc - Kafka Connect MySQL“无法识别服务器时区值‘EDT’”
- kotlin - Intellij-idea 调试器 - 协程选项卡:“获取信息时发生错误”
- swift - 第二个控制器没有接收到数据
- angular - Angular 更新错误为“@angular/core”获取元数据:无效的 URL:127.0.0.1:4200
- c# - StackExchange.Redis 崩溃 .NET webapi
- java - 执行我的 Jar 文件时找不到文件
- installation - 使用工作 SS7 成功安装 RESTCOMM-SMSC 需要哪些命令?
- django - 如何在此列表中捕获图书的名称或 reg_no?