Reputation: 13
I am working on a bot for a Twitch.tv channel and one of the commands is supposed to work like this:
user inputs: !riot (text)
bot outputs: ヽ༼ຈل͜ຈ༽ノ(text) OR RIOT ヽ༼ຈل͜ຈ༽ノ
The problem is whenever someone tries to input the command an error gets displayed in the cmd window which says: "ascii codec can't decode byte 0xe3 in position 0: ordinal not in range(128)". Nothing is displayed in the chat.
I have the coding set to UTF-8 at the beginning of the file so i'm not sure why its giving me this error.
Heres the snippet of code for the command itself and the defining of doCommandProcess: http://imgur.com/a/R1G1e
Heres the snippet of code for the command and the defining of doCommandProcess in the previous version of the bot that works just fine: http://imgur.com/a/0tQpE
I'm just really confused because both versions are encoded with UTF-8 and have nearly identical code yet one refuses to work and gives me an ascii error.
EDIT: Heres the code in question:
def doCommandProcess( text, irc ):
try:
usernamearray = text.split('!')
username = usernamearray[0]
username = username[1:]
messagearray = text.split(':')
message = messagearray[2]
if(len(usernamearray) >= 0 and len(messagearray) >= 2):
sqlcommands.doCommand(message, irc)
customcommands.docustomcommands(message, irc)
print username + ": " + message
if commandStartsWith(message, '!riot'):
print len(message)
if(len(message) > 8):
riotmessage = message[6:]
riotmessage = riotmessage.strip().upper()
respond(irc, 'ヽ༼ຈل͜ຈ༽ノ ' + riotmessage + ' OR RIOT ヽ༼ຈل͜ຈ༽ノ')
else:
respond(irc, 'You need to give a reason to riot!')
except Exception,e:
#If an error happens
#Print a new line for visibility
print text
print ""
print str(e)
Upvotes: 1
Views: 1969
Reputation: 1175
So you're declaring the file to be parsed as unicode, but your strings still need to be declared as unicode strings. In order to do this, prefix the opening quotation mark with a lowercase u
:
respond(irc, u'ヽ༼ຈل͜ຈ༽ノ ' + riotmessage + u' OR RIOT ヽ༼ຈل͜ຈ༽ノ')
You may want to be safe and ensure the riotmessage can be parsed properly by converting it unicode as well.
Upvotes: 2