I’ve created a Discord utility bot for logging messages and migrating channels between servers. I need to ensure it has the right permissions to function correctly, but my current code isn’t working as expected. I’m using @client.event and would prefer not to switch to @bot.command. Here’s how I’m trying to check the permissions:
if not client.user.guild_permissions.read_messages:
await message.channel.send("Please provide permission to read messages!")
if not client.user.guild_permissions.manage_messages:
await message.channel.send("Please provide permission to manage messages!")
if not client.user.guild_permissions.read_message_history:
await message.channel.send("Please provide permission to read message history!")
if not client.user.guild_permissions.attach_files:
await message.channel.send("Please provide permission to attach files!")
Unfortunately, I’m encountering the error: AttributeError: 'User' object has no attribute 'guild_permissions' when I test it. Can someone explain what I’m doing wrong?
yeah, client.user won’t work since it’s just a user object w/o guild perms. use message.guild.me.guild_permissions instead - that’s the bot’s member object for that specific guild. way easier than fetching the member manually and works perfectly with your @client.event setup.
You’re mixing up User and Member objects in discord.py. client.user gives you a global User object that doesn’t have guild permissions - that’s why you’re getting the error. You need the Member object for your bot in that specific guild instead. I’ve hit this same issue when building mod bots. Just swap your permission checks to message.guild.me.guild_permissions and you’re good. The message.guild.me gives you your bot as a Member object for that guild, so you’ll have access to all the permission attributes you need.
You can’t access guild_permissions directly on client.user - that’s a User object, not a guild member. You need the bot’s member object for that specific guild first. Try this: bot_member = message.guild.get_member(client.user.id), then check permissions with bot_member.guild_permissions.read_messages. Even easier - just use message.guild.me since it’s a shortcut to the bot’s member object in that guild. That’ll give you the right permissions for that specific server.