0
votes

Complicated question, I'm making a twitch bot that gets the top chatter every x minutes and displays it onto a widget. Every time someone writes something into chat, it's logged into a file how many messages they've made. My thing is I need it to check the list returned by twitch of active users, see which ones have a file, compare file values, and return the file name with the highest value.

Here's my code so far (working):

from twitchio.ext import commands
import json
from datetime import datetime
import os


new = {}
orig = {}
cAuthor = ""

def listToString(s):  
    
    # initialize an empty string 
    str1 = ""  
    
    # traverse in the string   
    for ele in s:  
        str1 += ele   
    
    # return string   
    return str1

def appendFile():
    intToChange = 0
    global cAuthor
    if os.path.exists(cAuthor) == True:
        with open(cAuthor, "r") as f:
            # Read lines and remove newline at the end of each line
            lines = [l.strip() for l in f.readlines()]
        l = int(listToString(lines))
    else:
        l = 0
    with open(cAuthor, "w+") as f:
        l += 1
        intToChange = str(l)
        f.write(str(l))
        f.close
        
class Bot(commands.Bot):

    def __init__(self):
        super().__init__(irc_token='token', nick='twitchbot', prefix='!',
                         initial_channels=['#almostsomebody'])

    # Events don't need decorators when subclassed
    async def event_ready(self):
        print(f'Ready | {self.nick}')

    async def event_message(self, message):
        now = datetime.now() # current date and time
        time = now.strftime("%H:%M:%S")
        print(time, message.author.name, "-", message.content)
        global cAuthor
        cAuthor = message.author.name
        appendFile()  

bot = Bot()
bot.run()

Edit: I have figured out a solution to this problem, and it works. Here's the solution for anybody who might have this problem in the future:

import os
import sys

users = []

def listToString(s):  
    
    # initialize an empty string 
    str1 = ""  
    
    # traverse in the string   
    for ele in s:  
        str1 += ele   
    
    # return string   
    return str1

def main():
    global users
    activeUsersExample = ["almostsomebody", "example1", "example2"]

    for item in activeUsersExample:
        print(item)
        with open(item) as f:
            for line in f:
                int_list = [int(i) for i in line.split()]
            print(int_list)
            count = int(listToString(line))
            line.strip("")
        users.append([item, count])
        print(users)