Reputation: 62
I'm making a discord bot with discord.py, and I have a say command, but you can make the bot mention any role. I have already stopped it from mentioning @everyone and @here, but I can't figure out how to stop it from mentioning roles. Here's the code
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)
Upvotes: 0
Views: 1639
Reputation: 15689
You can use message.role_mentions
mentions = message.role_mentions
my_role = ctx.guild.get_role(some_id)
if my_role in mentions:
await ctx.send("You can't mention that role")
If you have multiple roles
my_roles = [] # a list of `discord.Role` objects
mentions = message.role_mentions
if any(role in mentions for role in my_roles):
await ctx.send("You can't mention that role")
Also a better way of checking if @everyone
and @here
is mentioned in the message content itself, you can use the message.mention_everyone
attribute
if message.mention_everyone:
await ctx.send("You can't mention everyone")
Reference
Upvotes: 1
Reputation: 8564
You can use regexes. Assuming only uppercase/lowercase alphanumeric characters are allowed for a valid username/role:
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
Of-course, you can use a well-sophisticated regex for your username, if you desire. Or you can figure out the regex for role as per your requirements and try matching accordingly.
Upvotes: 1