How to Make Python Discord Bot Remove Messages from Particular Channel Only

Hey everyone! I need some help with my Discord bot coding. I’m pretty new to this stuff so sorry if this seems like a basic question.

I want my bot to automatically delete messages but only when they’re posted in one specific channel. When someone posts there, the bot should delete their message, send them a private message, and also change their role.

Right now I’m using the on_message event but it’s causing problems with my other bot commands because they don’t work anymore.

@bot.event
async def on_message(msg):
    sender = msg.author
    if msg.content.startswith("Helper:"):
        await msg.delete()
        await sender.send("Hello there!")
        await sender.remove_roles(get(sender.guild.roles, "Detective"))

The issue is that now my bot commands stopped working since it only responds to messages starting with “Helper:”. Is there a way to make this trigger only happen in a certain channel? Or maybe I should use a command instead of an event?

Any ideas would be really helpful. Thanks!

You could also approach this differently by using a command decorator with a custom check instead of relying solely on the on_message event. This gives you more control and cleaner code structure. Create a custom check function that verifies both the channel and message content, then apply it to a command. Something like this works well:

def helper_channel_only():
    def predicate(ctx):
        return ctx.channel.id == YOUR_CHANNEL_ID
    return commands.check(predicate)

@bot.command()
@helper_channel_only()
async def helper(ctx, *, content):
    await ctx.message.delete()
    await ctx.author.send("Hello there!")
    await ctx.author.remove_roles(get(ctx.guild.roles, "Detective"))

This method keeps your message handling separate from command processing and makes debugging easier. Users would type something like !helper their message here in the designated channel. The advantage is that you won’t accidentally break other functionality and the code is more maintainable in the long run.

The main issue with your current setup is that you’re missing await bot.process_commands(msg) at the end of your on_message event. Without this line, the bot won’t process any regular commands because the event handler intercepts all messages but doesn’t pass them along to the command processor. To restrict this to a specific channel, add a channel ID check before your existing logic. You can get the channel ID by right-clicking the channel in Discord and selecting “Copy ID” (make sure developer mode is enabled in settings). Then modify your code like this:

@bot.event
async def on_message(msg):
    if msg.channel.id == YOUR_CHANNEL_ID_HERE:
        sender = msg.author
        if msg.content.startswith("Helper:"):
            await msg.delete()
            await sender.send("Hello there!")
            await sender.remove_roles(get(sender.guild.roles, "Detective"))
    
    await bot.process_commands(msg)

This way your bot will only check for the “Helper:” prefix in that specific channel, and all your regular commands will continue working normally in other channels.

honestly you can just add a simple if statement to check the channel name or id before doing anything. like if msg.channel.name == "your-channel-name": then put all your helper logic inside that block. way simpler than making custom checks imo, just dont forget the process_commands line at the end