How to get username of person who executed Discord bot command?

I’m working on a Discord bot and I want to track which user runs specific commands. Right now my logging function just says “someone” but I need it to show the actual username.

def LogCommand(self, command_name):
    print((datetime.now().strftime('%H:%M:%S')), f"\tSomeone executed the '{command_name}' command!")

@commands.command()
async def hello(self, ctx):
    await ctx.send('Hello there!')
    self.LogCommand(self, "hello")

How can I modify this to display the real username instead of just “someone”? I tried a few things but nothing worked. Any help would be great!

Both solutions work, but manual tracking becomes a nightmare once you’re running bots across multiple servers. Trust me - I’ve been there.

Skip the logging fixes and automate everything instead. Build a workflow that captures command executions, dumps them into a database, and alerts you when something’s up.

Here’s your fixed code:

def LogCommand(self, ctx, command_name):
    username = ctx.author.name
    user_id = ctx.author.id
    print(f"{datetime.now().strftime('%H:%M:%S')} User {username} (ID: {user_id}) executed '{command_name}'")

But honestly? Automate the whole thing. Hook into your Discord bot events, log everything to a real database, generate daily reports, and get notifications when specific users run certain commands.

No more copying logging code everywhere. No more lost data after restarts. Just clean automation handling user tracking, command analytics, and monitoring while you focus on other stuff.

This saved me hours of debugging and gave way better insights into how people actually use my bots.

You’re passing the wrong parameters to LogCommand. Instead of self.LogCommand(self, "hello"), you need to pass the context object - that’s where all the user info lives.

def LogCommand(self, ctx, command_name):
    print((datetime.now().strftime('%H:%M:%S')), f"\t{ctx.author.name} executed the '{command_name}' command!")

@commands.command()
async def hello(self, ctx):
    await ctx.send('Hello there!')
    self.LogCommand(ctx, "hello")

Also, you might want to use ctx.author.global_name instead of ctx.author.name. Discord’s been moving away from discriminators, and global_name shows the actual display name users see - way better for logging.

The context object has everything you need about who ran the command. Don’t hardcode ‘someone’ - just grab the user data from ctx.author. Here’s what I use:

def LogCommand(self, ctx, command_name):
    user = ctx.author
    print((datetime.now().strftime('%H:%M:%S')), f"\t{user.name}#{user.discriminator} executed the '{command_name}' command!")

@commands.command()
async def hello(self, ctx):
    await ctx.send('Hello there!')
    self.LogCommand(ctx, "hello")

I add the discriminator because Discord usernames aren’t unique. You can also use ctx.author.id if you need the unique user ID for database stuff or better tracking.

You’re passing self twice in your function call - should be the context object instead. Your LogCommand method needs ctx to grab user info.

def LogCommand(self, ctx, command_name):
    print((datetime.now().strftime('%H:%M:%S')), f"\t{ctx.author.name} executed the '{command_name}' command!")

@commands.command()
async def hello(self, ctx):
    await ctx.send('Hello there!')
    self.LogCommand(ctx, "hello")

I’ve run Discord bots for years and always throw ctx.guild.name into my logs too - super handy when you’re running across multiple servers. The ctx object has all the metadata you need about where and how the command was triggered.

you’ve got an extra self parameter in your LogCommand call. change self.LogCommand(self, "hello") to self.LogCommand(ctx, "hello"). also make sure your function signature accepts ctx like the other examples.

Everyone’s fixing the syntax, but you’re missing the bigger picture. Once your bot grows beyond a few commands, you’ll copy-paste that LogCommand call everywhere.

Sure, pass ctx instead of self for the username:

def LogCommand(self, ctx, command_name):
    print((datetime.now().strftime('%H:%M:%S')), f"\t{ctx.author.name} executed the '{command_name}' command!")

@commands.command()
async def hello(self, ctx):
    await ctx.send('Hello there!')
    self.LogCommand(ctx, "hello")

But think ahead. Want command usage stats? User analytics? Rate limiting per user? Error tracking?

I learned this running bots for thousands of users - manual logging becomes a nightmare.

Build an automation workflow that captures every Discord event, processes the data, stores it properly, and gives you dashboards without touching your bot code. Hook into Discord’s API events and let automation handle user tracking, command analytics, and monitoring.

Your bot stays clean. Data stays consistent. You get way better insights than print statements.

To retrieve the username of the user who executed a command, pass the ctx variable to your logging function. Modify your LogCommand method to accept ctx as a parameter and fetch the username like this:

def LogCommand(self, ctx, command_name):
    username = ctx.author.name
    print((datetime.now().strftime('%H:%M:%S')), f"\t{username} executed the '{command_name}' command!")

@commands.command()
async def hello(self, ctx):
    await ctx.send('Hello there!')
    self.LogCommand(ctx, "hello")

Using ctx.author allows you to access the user’s details. If you prefer, you can use ctx.author.display_name for their display name. Remember to remove the extra self in the LogCommand call to avoid issues.