Change Discord User Avatar with Python

  • Share this:

Code introduction


This code defines a function that can be used to change the avatar of a Discord user. It takes the user's ID and the URL of the new avatar image as arguments, and changes the user's avatar through the Discord API.


Technology Stack : Discord.py

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def change_avatar(user_id, avatar_url):
    """
    Change the avatar of a Discord user.

    Args:
        user_id (int): The ID of the Discord user.
        avatar_url (str): The URL of the new avatar image.
    """
    import discord
    client = discord.Client()

    @client.event
    async def on_ready():
        member = client.get_user(user_id)
        if member:
            await member.edit(avatar=discord.Asset(avatar_url))
            print(f"Avatar changed for user {user_id}")
        else:
            print(f"User with ID {user_id} not found.")

    client.run('your_token_here')                
              
Tags: