[英]Discord.py - Handling user to mentioning other users
当用户尝试使用该命令但提及其他成员时,它将向用户发送错误。
@bot.command()
async def tell(ctx, *, text):
if text in ctx.guild.members:
await ctx.send("Mentioning other's members not allowed")
return
有很多方法可以解决这个问题,在这里我将向您展示您可能感兴趣的两种可能的方法。 正如Anu所说,第一种方法是使用Regex 。 您可以检查是否有任何内容以<@
开头并以>
结尾,因为这是大多数用户和角色提及的布局方式,也是机器人查看提及的方式。 第二种方法不如使用正则表达式,您可以通过message.mentions
检查ctx.message
中是否有任何提及。 请查看这两种方法以及下面的一些进一步说明。
# Method 1: Recommended and Regex
# Checks if 'tell' has mentions via regex
# I will link other questions on regex below to help you
import re # Don't forget to import this!
@bot.command()
async def tell(ctx, *, tell):
x = re.search('<@(.*)>', tell)
# User mentions are usually in the form of
# <@USERID> or <@!USERID>
if not x:
await ctx.send(tell)
return
await ctx.send("Don't mention other users")
# Method 2: Not recommended but working
# Check if the original command message has mentions in it via message.mentions
# Will backfire if the user uses a mention prefix (@Bot tell)
@bot.command()
async def tell(ctx, *, text):
if ctx.message.mentions:
await ctx.send("Mentioning other's members not allowed")
return
await ctx.send(text)
[方法 1 的图像]
[方法 2 的图像]
[关于 Bots 如何看到提及的图片(出于隐私原因,id 被屏蔽)]
其他链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.