Python 有没有办法阻止discord.py机器人提及角色?

Python 有没有办法阻止discord.py机器人提及角色?,python,discord,discord.py,Python,Discord,Discord.py,我正在用discord.py制作一个discord机器人,我有一个say命令,但是你可以让机器人提到任何角色。我已经阻止它在这里提及@everyone和@here,但我不知道如何阻止它提及角色。这是密码 async def say(ctx, *, message=None): message = message or "You have to type a message" message_components = message.split() if "@ever

我正在用discord.py制作一个discord机器人,我有一个say命令,但是你可以让机器人提到任何角色。我已经阻止它在这里提及@everyone和@here,但我不知道如何阻止它提及角色。这是密码

async def say(ctx, *, message=None):
message = message or "You have to type a message"
message_components = message.split()
if "@everyone" in message_components or "@here" in message_components:
    await ctx.send("You can not ping everyone")
    return

await ctx.message.delete()
await ctx.send(message)

您可以使用正则表达式。假设有效用户名/角色只允许使用大写/小写字母数字字符:

import re
user_regex = r"@[a-zA-Z0-9]+"
message = "I'm tagging @you and @you2 in this message!"
match = re.findall(user_regex, message)
if match:
    await ctx.send("You can not ping everyone")
    return

当然,如果您愿意,您可以使用非常复杂的用户名。或者,您可以根据自己的要求找出角色的正则表达式,并尝试进行相应的匹配。

您可以使用正则表达式。假设有效用户名/角色只允许使用大写/小写字母数字字符:

import re
user_regex = r"@[a-zA-Z0-9]+"
message = "I'm tagging @you and @you2 in this message!"
match = re.findall(user_regex, message)
if match:
    await ctx.send("You can not ping everyone")
    return

当然,如果您愿意,您可以使用非常复杂的用户名。或者,您可以根据自己的要求找出角色的正则表达式,并尝试进行相应的匹配。

您可以使用
消息。角色\u提到

提及=message.role\u提及
我的角色=ctx.guild.get角色(一些id)
如果我在中的角色提到:
等待ctx.send(“您不能提及该角色”)
如果您有多个角色

my_roles=[]#`discord.Role`对象列表
提及=message.role\u提及
如果有(我的大学角色中提到的角色):
等待ctx.send(“您不能提及该角色”)
还有一种更好的检查消息内容本身是否提到了
@everyone
@here
,您可以使用
消息。提及每个人
属性

if message.notify_每个人:
等待ctx.send(“你不能提及所有人”)
参考文献


您可以使用
消息。角色\u提到

提及=message.role\u提及
我的角色=ctx.guild.get角色(一些id)
如果我在中的角色提到:
等待ctx.send(“您不能提及该角色”)
如果您有多个角色

my_roles=[]#`discord.Role`对象列表
提及=message.role\u提及
如果有(我的大学角色中提到的角色):
等待ctx.send(“您不能提及该角色”)
还有一种更好的检查消息内容本身是否提到了
@everyone
@here
,您可以使用
消息。提及每个人
属性

if message.notify_每个人:
等待ctx.send(“你不能提及所有人”)
参考文献


您知道从哪里开始吗?问题解决了吗?如果是,请将任何答案标记为已接受@temp84323您知道从哪里开始吗?问题解决了吗?如果是,请将任何答案标记为已接受@temp84323实际上角色提及看起来不像这个
@role
(只有
每个人
这里
),但是像这样
实际上角色提及看起来不像这个
@role
(只有
每个人
这里
),而是像这样