Discord bot permission setup issue for message editing feature

I’m working on a Discord bot for my server and I’m stuck with a permissions problem. I want the bot to censor certain words by replacing them with “[redacted]” in all channels except one. The code seems fine, but I keep getting a 403 Forbidden error when I try to edit messages.

I’ve already set up the bot permissions in the Discord Developer Portal. I gave it manage roles, view channels, send messages, manage messages, read message history, and mention everyone. I added the bot to my test server using the OAuth2 link, but it’s still not working.

Here’s a simplified version of my code:

import discord

client = discord.Client()

@client.event
a async def on_message(message):
    if message.author == client.user:
        return

    bad_words = ['example', 'test']
    if message.channel.name != 'no-rules-zone':
        for word in bad_words:
            if word in message.content.lower():
                edited = message.content.replace(word, '[redacted]')
                await message.edit(content=edited)
                await message.channel.send(f'{message.author.mention}, please check the rules.')

client.run('YOUR_TOKEN_HERE')

Any ideas on why I’m getting the Forbidden error and how to fix it?

hey, i had similar problems with my bot. check ur bot’s role in server settings. it needs to be higher than users’ roles to edit their messages. also make sure ‘manage messages’ is on for each channel. if that don’t work, try deleting messages and reposting censored versions instead. Good luck!

I encountered a similar issue when developing a moderation bot for my gaming community’s Discord server. The problem likely stems from Discord’s permission hierarchy. Even if you’ve given the bot the necessary permissions in the Developer Portal, it still needs to have a role in the server that’s higher than the roles of the users whose messages it’s trying to edit.

To resolve this, create a new role specifically for your bot and place it above other user roles in the server settings. Then, assign this role to your bot. This should grant it the ability to edit messages from users with lower roles.

Additionally, double-check that the bot has the ‘Manage Messages’ permission enabled for each specific channel where you want it to operate. Sometimes, channel-specific permissions can override the general server permissions.

If you’re still having trouble after trying these steps, you might want to consider using a different approach, such as deleting the original message and reposting a censored version. This method often requires fewer permissions and can be more reliable in complex server setups.

Having worked on several Discord bots, I can tell you that the 403 Forbidden error often crops up due to role hierarchy issues, not just permissions. Even with the correct permissions, your bot needs a role higher than the message author’s to edit their messages.

Here’s what I’d suggest:

Create a dedicated role for your bot in server settings. Position this role above all user roles and assign it to your bot; this should resolve most editing issues.

If problems continue, consider deleting the original message and posting a censored version. This alternative usually requires fewer permissions and can be more reliable in complex server setups.

Lastly, verify that you’re using the latest discord.py version and that your async handling is correct to avoid unexpected permission errors.