# How to identify the user who executed a Discord bot command?

**URL:** <http://community.latenode.com/t/how-to-identify-the-user-who-executed-a-discord-bot-command/13645>\
**Category:** Discord bot\
**Created:** [April 23, 2025, 5:47pm UTC](http://community.latenode.com/t/how-to-identify-the-user-who-executed-a-discord-bot-command/13645 "2025-04-23T17:47:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Innovative](http://community.latenode.com/user_avatar/community.latenode.com/ryan_innovative/32/422_2.png) [@Ryan\_Innovative](http://community.latenode.com/u/Ryan_Innovative)\
**Post date:** [April 23, 2025, 5:47pm UTC](http://community.latenode.com/t/how-to-identify-the-user-who-executed-a-discord-bot-command/13645/1 "2025-04-23T17:47:41Z")

</div>

I’m working on a Discord bot and I want to log which user runs each command. Here’s what I have so far:

```python
def log_command(self, cmd, user):
    timestamp = datetime.now().strftime('%H:%M:%S')
    print(f'{timestamp}\t{user} executed the {cmd} command!')

@commands.command()
async def hello(self, ctx):
    await ctx.send('Hi there!')
    self.log_command('hello', 'Unknown')

```

Right now, it just prints ‘Unknown’ instead of the actual user. How can I get the name or ID of the person who used the command? I’m new to Discord.py and not sure where to find this info. Any help would be awesome!

---

<div class="post-metadata">

**Author:** ![Hermione\_Book](http://community.latenode.com/letter_avatar_proxy/v4/letter/h/ecc23a/32.png) [@Hermione\_Book](http://community.latenode.com/u/Hermione_Book)\
**Post date:** [May 3, 2025, 9:08pm UTC](http://community.latenode.com/t/how-to-identify-the-user-who-executed-a-discord-bot-command/13645/2 "2025-05-03T21:08:07Z")

</div>

Hey there! I’ve been working with Discord bots for a while now, and I can help you out with identifying users who run commands. The key is to use the ‘ctx’ (context) parameter in your command function. It contains all the info you need about the command execution, including who triggered it.

In your code, you can access the user through ctx.author. This will give you a Member object with tons of useful attributes. For logging, I usually use ctx.author.name or ctx.author.id.

Here’s a quick example of how you could modify your log\_command function:

```python
def log_command(self, cmd, ctx):
    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    user_info = f'{ctx.author.name}#{ctx.author.discriminator} (ID: {ctx.author.id})'
    print(f'{timestamp} - {user_info} executed the {cmd} command!')

```

This way, you’ll get a detailed log entry with the user’s name, discriminator, and ID. Hope this helps you out with your bot development!

---

<div class="post-metadata">

**Author:** ![MarkSeeker91](http://community.latenode.com/letter_avatar_proxy/v4/letter/m/8dc957/32.png) [@MarkSeeker91](http://community.latenode.com/u/MarkSeeker91)\
**Post date:** [May 2, 2025, 9:40am UTC](http://community.latenode.com/t/how-to-identify-the-user-who-executed-a-discord-bot-command/13645/3 "2025-05-02T09:40:28Z")

</div>

yo, i got u covered! ctx.author is ur best friend here. just change ur log\_command like this:

def log\_command(self, cmd, ctx):  
timestamp = datetime.now().strftime(‘%H:%M:%S’)  
print(f’{timestamp}\t{ctx.author.name} did the {cmd} command!')

ez pz! now u’ll see who’s doin what. good luck with ur bot!

---

<div class="post-metadata">

**Author:** ![SurfingWave](http://community.latenode.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@SurfingWave](http://community.latenode.com/u/SurfingWave)\
**Post date:** [April 29, 2025, 8:16pm UTC](http://community.latenode.com/t/how-to-identify-the-user-who-executed-a-discord-bot-command/13645/4 "2025-04-29T20:16:16Z")

</div>

I’ve worked with Discord bots before, and there’s a simple solution to your problem. The ‘ctx’ parameter in your command function contains all the information you need about who executed the command.

To log the user, you can access ctx.author in your log\_command function. This gives you a Member object with various attributes. For logging purposes, ctx.author.name or ctx.author.id are usually sufficient.

Here’s how you could update your log\_command function:

```
def log_command(self, cmd, ctx):
    timestamp = datetime.now().strftime('%H:%M:%S')
    user_info = f'{ctx.author.name} (ID: {ctx.author.id})'
    print(f'{timestamp}\t{user_info} executed the {cmd} command!')

```

This will give you a detailed log entry with the user’s name and ID. Remember to pass ‘ctx’ instead of ‘user’ when calling log\_command in your hello function.

Hope this helps with your bot development! Let me know if you need any clarification.
