user17723683
user17723683

Reputation:

Discord.py - Handling user to mention roles

My Goals:

When the user tried to use the command but mentioning a roles, it will sends an errors to the users.

My Current Codes:

@bot.command()
async def say(ctx, *, message: str):
    try:
        take_roles = ctx.guild
        roles = [role.mention for role in take_roles.roles]

        if message in roles:
            await ctx.send(
                f"{ctx.author.mention}, Hm? Trying to Abusing Me? Mention `@roles` is **NOT** Allowed on Say Command!"
            )
        else:
            await ctx.message.delete()
            await ctx.send(message)

Upvotes: 0

Views: 341

Answers (1)

RiveN
RiveN

Reputation: 2653

You were trying to find the whole message in roles. A better idea is to look for role mentions inside of the message.

@bot.command()
async def say(ctx, *, message: str):
    for role in ctx.guild.roles:
        if role.mention in message:
            await ctx.send(f"{ctx.author.mention}, Hm? Trying to Abusing Me? Mention `@roles` is **NOT** Allowed on Say Command!")
            return # stops function if the role mention is in message
    
    # if it didn't match any roles:
    await ctx.message.delete()
    await ctx.send(message)

Upvotes: 1

Related Questions