pythonsocketsloggingirctwitch

Ban logs in IRC channels, on the example of twitch


I am writing a bot in Python + Sockets to collect logs on several twitch channels at the same time. Actually, there are no problems with the messages themselves, but I would like to collect ban / timeout logs for a wider functionality, the code:

IRC = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
IRC.connect ((SERVER, PORT))
IRC.send (f "PASS {PASSWORD} \ n" .encode ('utf-8'))
IRC.send (f "NICK {NICKNAME} \ n" .encode ('utf-8'))
IRC.send (f "JOIN {CHANNEL} \ n" .encode ('utf-8'))

while True:
     response = IRC.recv (1024) .decode ('utf-8')
     if response == "PING: tmi.twitch.tv \ r \ n":
         IRC.send ("PONG: tmi.twitch.tv \ r \ n" .encode ('utf-8'))

     print (response)
     time.sleep (.5)

In this (yet unfinished) form, he collects messages, but not bans, etc.


Solution

  • Have you considered setting up Twitch-IO by chance? If you set up a bot that just connects to your chat and configure logging, it will automatically log to a file, as seen below:

    from twitchio.ext import commands
    import logging
    
    logging.basicConfig(filename='irclog.txt', encoding='utf-8', level=logging.DEBUG)
    
    bot = commands.Bot(token="irc token", nickname="your username", prefix="doesn't really matter, can be anything", initial_channels=["your channel"])
    
    @bot.event
    async def event_ready():
        print("Bot ready, beginning logging")
    
    bot.run()
    

    I hope that this was helpful!