-1

Currently having an issue with my Twitch chat bot, this error occurs when the bot picks up JTV sending a flag to op somebody in the channel (Giving them mod permissions).

The problem I'm having is that this error sometimes occurs and sometime doesn't. Due to this, I cannot have a stable process of this running on my VPS. Any help?

message = ' '.join(line)
x = re.findall('^:jtv MODE (.*?) \+o (.*)$', message) # Find the message
if (len(x) > 0):
    channel = x[0][0]
    if (channel not in mods): # If the channel isn't already in the list
        mods[channel] = []
    list = mods.get(channel)
    list.append(x[0][1])
    print(mods) # Print updated list with new mods

Here is where I remove them as well, Not sure if this might incur an error or not. But I will post it nonetheless...

# Removing mods
y = re.findall('^:jtv MODE (.*?) \-o (.*)$', message)
if (len(y) > 0):
    channel = y[0][0]
    if (channel in mods):
        mods.get(channel).remove(y[0][1])
        print(mods) 
Lynn
  • 3
  • 5
  • 1
    You shouldn't use `list` as a variable name. It's one of the inbuilt type names. – Paul Rooney Jun 09 '15 at 01:53
  • 2
    1. `list` is not a good name as it masks the built-in function `list()`. 2. At some point or other, that `list` is not a `list` at all but a `str`. Since that comes from `mods.get(channel)`, then the result of that is sometimes a `str`, possibly because `channel` is in `mods` and `mods[channel]` doesn't get assigned an empty `list`. 3. The duct-tape fix would be to put the `append()` into a `try..except` and see if that improves the situation. – TigerhawkT3 Jun 09 '15 at 01:54
  • @PaulRooney What do you recommend I use then? The program picks up chat from multiple channels and I would like to have a list of the mods from each one of them. – Lynn Jun 09 '15 at 01:55
  • 1
    Anything but `list` (or the other built-ins/keywords). – TigerhawkT3 Jun 09 '15 at 01:59
  • Even list_. I believe lst is the preferred name for a generic list. – Alex Huszagh Jun 09 '15 at 02:24

3 Answers3

2

From what I can see here the 'list' list.append(x[0][1]) must sometimes be a string, not a list. So maybe mods.get(channel) sometimes returns a string. One solution might be to check if you got a string this time, type(list) == str and don't do the append. Unfortunately that's all I can tell you. Maybe look inside mods.get() and see why it would do that.

  • Looks like this solves the error. I had to amend removing mods as well since it triggers the same error with '.remove'. – Lynn Jun 09 '15 at 07:49
1

First, call the list something like my_list. Second, whenever channel is not in mods, mods[channel] will not be assigned a new list. The fact that it says it's a str means that you're assigning a string into that somewhere in your code. You should probably look into that. But you can try to sidestep all that by asking for forgiveness rather than permission:

try:
    my_list.append(x[0][1])
except AttributeError:
    pass # ideally, you shouldn't let errors pass silently

Also, instead of if (len(x) > 0):, you can do if x:. Give x a more descriptive variable name like message or something while you're at it.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

You should use logging to debug your code. After that issue occurred, you can check the log and find out what happened.

BTW, Don't use list as a variable name. It's very confusing.

For example:

import logging

try:
    list.append(x[0][1])
except:
    logging.error(type(x))
    logging.error(x)
meneldal
  • 1,717
  • 1
  • 21
  • 30
Dken
  • 1
  • 1