我有一个关于 discord.py 的问题。我目前正在为我的服务器制作一个 mod-mail。机器人的想法是在每次收到消息时创建一个频道。这一切都有效。它在作者姓名后以小写形式创建一个频道名称,并用 - 替换空格。例如 BOB IS MAD 会变成bob-is-mad
.
现在我正在努力增强响应系统。从只是提及您想要向其发送消息的用户,到机器人根据您发送消息的bob-is-mad
频道获取您想要发送的成员。例如,如果我要在频道中发送消息,它会自动将其发送给 BOB IS MAD。不过,这就是我遇到问题的地方。
我遇到的问题是机器人正在寻找小写字母,但一些 Discord 用户的名字中有大写字母。我怎样才能让机器人找到成员,即使名称可以是小写和大写。
这是我的代码:
(我知道这不在回复命令中,如果我能找到获取用户 ID 的方法,我只是将其用作测试命令)
# this commands is grabs the id of the user the channel is from
@bot.command()
async def id(ctx):
# getting the channels name
channel = ctx.channel
current = str(channel)
guild = ctx.guild
# replacing the - for spaces
user = current.replace("-", " ")
# testing the output
print (user)
# finding the member in the server
member = discord.utils.get(guild.members, name=f"{user}")
# printing if it found the member
print (member)
我尝试制作它,member = discord.utils.get(guild.members, name=f"{user.upper()}")
但这当然不起作用。我找不到任何关于此的信息
而不是使用utils.get
我会使用utils.find
@bot.command()
async def id(ctx):
channel_name = ctx.channel.name
user_name = channel_name.replace("-", " ")
predicate = lambda member: member.name.lower() == user_name
member = discord.utils.find(predicate, ctx.guild.members)
print(member)
尽管使用成员名称是一个坏主意(多个成员可以具有相同的名称),但我会将成员 ID 放在频道的主题中并从那里获取它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句