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

Hey everyone! I need some help with my Discord bot. I’m pretty new to this so sorry if this seems obvious.

I want my bot to automatically delete messages but only in one specific channel. When someone posts there, the bot should delete their message, send them a DM with the content, and change their role.

Right now I have this code:

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

The issue is this breaks my other commands since the bot only responds to messages starting with “Helper:”. How can I make it only check for this prefix in a certain channel?

Could I use a command instead of an event for this? Any ideas would be great, thanks!

You need to add a channel check in your on_message event. Compare the channel ID directly by right-clicking your target channel in Discord and selecting ‘Copy ID’ (ensure developer mode is enabled first). Update your code like this: ```python
@bot.event
async def on_message(msg):
if msg.channel.id != YOUR_CHANNEL_ID_HERE: # Replace with actual ID
await bot.process_commands(msg) # This is important for other commands
return

author = msg.author
if msg.content.startswith("Helper:"):
    await msg.delete()
    await author.send("Hello there!")
    await author.remove_roles(get(author.guild.roles, "Detective"))

await bot.process_commands(msg)

``` Including await bot.process_commands(msg) is crucial as it allows the bot to continue processing regular commands. Without this line, your other commands will not work when you utilize on_message events.

Try using a command decorator with checks instead of on_message. It’s cleaner and won’t mess with your other bot functions:

@bot.command()
@commands.check(lambda ctx: ctx.channel.id == YOUR_CHANNEL_ID)
async def helper(ctx, *, content):
    await ctx.message.delete()
    await ctx.author.send(f"Your message: {content}")
    await ctx.author.remove_roles(get(ctx.guild.roles, "Detective"))

Users just type !helper their message here in that channel. The check decorator locks it to one channel automatically, so your other commands stay untouched.

you can check the channel name instead of ID if that’s easier. use if msg.channel.name == "your-channel-name": and you’re good. just make sure you add bot.process_commands(msg) at the end or your commands will break.