Mitchell
Mitchell

Reputation: 27

Grab User Info With Discord.py

I'm new to coding and need a discord python command that can return the user's avatar and name that requested it. I've tried looking at the documentation but don't really understand it. Any help is appreciated.

Upvotes: 1

Views: 679

Answers (1)

Benjamin Sommer
Benjamin Sommer

Reputation: 1248

You can use this code snippet to get the user's name and avatar. From here you could then process it and return a message using.

import discord

client = discord.Client()

@client.event
async def on_message(message):
    if message.content.startswith('!user'):
        sender = message.author
        name = sender.display_name
        avatar_url = sender.avatar_url
        await message.channel.send(f'Hello, {name}. Your avatar URL is {avatar_url}.')

client.run('your token here')

If you don't have the discord python module installed, you can do so by running pip install discord.py and you should be good.

If you haven't yet set up your app in the discord developer console, you must do that prior to running this code snippet, adding in the bot token on the last line. If you haven't done this already, a good tutorial I can recommend can be found here.

Upvotes: 1

Related Questions