1

When I have on_message() in my code, it stops every other @bot.command commands from working. I've tried to await bot.process_commands(message), but that doesn't work either. Here is my code that I have:

@bot.event
@commands.has_role("Owner")
async def on_message(message):
    if message.content.startswith('/lockdown'):
        await bot.process_commands(message)
        embed = discord.Embed(title=":warning: Do you want to activate Lock Down?", description="Type 'confirm' to activate Lock Down mode", color=0xFFFF00)
        embed.add_field(name="\u200b", value="Lock Down mode is still in early development, expect some issues")
        channel = message.channel
        await bot.send_message(message.channel, embed=embed)
        msg = await bot.wait_for_message(author=message.author, content='confirm')
        embed = discord.Embed(title=":white_check_mark: Lock Down mode successfully activated", description="To deactivate type '/lockdownstop'", color=0x00ff00)
        embed.add_field(name="\u200b", value="Lock Down mode is still in early development, expect some issues")
        await bot.send_message(message.channel, embed=embed)
Taku
  • 31,927
  • 11
  • 74
  • 85
Sir Cinnamon
  • 13
  • 1
  • 1
  • 4

1 Answers1

10

You have to place await bot.process_commands(message) outside of the if statement scope, process_command should be run regardless if the message startswith ”/lockdown”.

@bot.event
async def on_message(message):
    if message.content.startswith('/lockdown'):
       ...
    await bot.process_commands(message)

By the way, @commands.has_role(...) cannot be applied to on_message. Although there aren't any errors (because there’s checking in place), has_role wouldn't actually work as you would've expected.

An alternative to the @has_role decorator would be:

@bot.event
async def on_message(message):
    if message.channel.is_private or discord.utils.get(message.author.roles, name="Admin") is None:
        return False

    if message.content.startswith('/lockdown'):
       ...
    await bot.process_commands(message)

    

Taku
  • 31,927
  • 11
  • 74
  • 85
  • It makes sense that `@commands.has_role` will not work with `on_message`, as the `on_message` event is not a command. It's an event that gets called every time a message is sent to a server. – Benjin Apr 15 '18 at 07:26
  • One more thing, seeing as @commands.has_role("...") doesn't work for on_message(), do you know how I could have it role only? – Sir Cinnamon Apr 15 '18 at 09:30
  • @SirCinnamon you can access `message.author.roles` from the `message` object. You can then do something like `for role in message.author.roles: if role.name == 'Owner'` http://discordpy.readthedocs.io/en/latest/api.html#member http://discordpy.readthedocs.io/en/latest/api.html#discord.Role – Benjin Apr 15 '18 at 09:45
  • @SirCinnamon I'd probably iterate over the roles, save the result to a variable (something like `owner_role = True`) and then make it part if your already existing `if` condition. If you need further with code examples, make a new question or edit your existing one. – Benjin Apr 15 '18 at 12:12
  • The alternative to the @has_role decorator will exit the function before any commands are processed unless the first `if` conditions are met – Benjin Apr 16 '18 at 12:18