How to pin messages using Python Discord bot

I’m struggling with getting my Discord bot to pin messages properly. I’ve been trying different approaches but keep running into errors.

First, I attempted this method:

if "pin_msg" == user_message.content.lower():
    # attempt to pin the message
    user_message.channel.pin_message(msg_id)

This gives me an error saying that TextChannel doesn’t have a pin_message attribute.

Then I tried using the pins method instead:

if "pin_msg" == user_message.content.lower():
    # second attempt at pinning
    channel_pins = user_message.channel.pins(msg_id)

But now I get an error about pins() taking only one argument when I’m passing two.

I’m pretty new to both Python and Discord bot development, so I’m probably missing something obvious. What’s the correct way to pin a message in a Discord channel using discord.py?

grab the msg obj first, then just use msg.pin(). do it like this: message = await channel.fetch_message(msg_id) and after that await message.pin(). also, pins() is for fetching existing pins, not for pinning new ones!

You’re getting this error because you’re trying to pin the message ID instead of the actual message object. Discord.py needs the message object to pin anything. Also check that your bot has “Manage Messages” permission in that channel. I ran into the same thing and fixed it like this:

if "pin_msg" == user_message.content.lower():
    try:
        message_to_pin = await user_message.channel.fetch_message(msg_id)
        await message_to_pin.pin()
    except discord.NotFound:
        # handle case where message doesn't exist
        pass
    except discord.Forbidden:
        # handle permission errors
        pass

Double-check your bot’s permissions in server settings or you’ll keep getting forbidden errors even with working code.

You’re mixing up message objects with message IDs. Don’t use user_message.channel.pin_message(msg_id) - work with the actual message object instead.

The pins() method just fetches messages that are already pinned. It won’t create new pins. Use fetch_message() with your ID to get the message, then call pin() on it.

Watch out for rate limits - Discord caps pinned messages at 50 per channel. Hit that limit and it’ll auto-unpin the oldest one. Also, don’t forget to use await since these calls are async.