(Python) How can I get the username of someone who sent a command to my chatbot?


#1

I’m trying to learn the basics of creating a chatbot. (I’m using the Python example bot as a basis) For one of the bot’s functions, it needs to be able to get the username of whoever entered a command. For instance,

def do_command(self, e, cmd):
    c = self.connection
    
    if cmd == "hello":
        message = "Hello, " + username + "!"
        c.privmsg(self.channel, message)

In this example function, how can I put the name of the user who entered the ‘!hello’ command into the ‘username’ string?

Sorry if this question is too simplistic, but I’m struggling to find documentation for this stuff that I’m actually able to understand.


#2

This chat example uses, jaraco/irc as a basic IRC library

https://python-irc.readthedocs.io/en/latest/

Yon can log out the contents of e to see what it has exposed.

It’s probably in e.login, I don’t use python or this library myself

But this is more a IRC parsing question in python than a TwitchAPI question.


#3

Thank you! I’ll look into that documentation. I didn’t think this was the right category for it either, but I didn’t know what the correct category should have been.


#4

e.source.nick for the user’s logon name

If you need the displayed name, you’ll have to get it from e.tags (key=“display_name”)